diff options
| author | tv <tv@krebsco.de> | 2026-11-29 14:21:34 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-30 14:59:15 +0000 |
| commit | eaac60ba88728bb582778c726e5798156dddfc45 (patch) | |
| tree | 557f4709762f95a7a440d1fa919dcce3bbf83cb4 /src/media.rs | |
| parent | 70367d17613ebe501fb2d3e9fa72b16c2eb9c2bd (diff) | |
enstate: the receptor and the provider, a first version
One program on a library: --plan and --apply of submissions (complete and
partial, items required and optional, groups, removal, the chain as
order), validators of types and across slots, views put in place by
replacing a link, their reactions by systemctl; --status, --materialize;
and --pack, a submission from a source list in JSON or TOML, by the
config crate's sources. Programs by the protocol of design.md 5.2;
tests with shell scripts as validators and renderers.
Diffstat (limited to 'src/media.rs')
| -rw-r--r-- | src/media.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/media.rs b/src/media.rs new file mode 100644 index 0000000..c6b1c22 --- /dev/null +++ b/src/media.rs @@ -0,0 +1,92 @@ +//! Types, as media types (docs/design.md, 5.1): compared in a normal form, and keyed +//! (holding records) where their top level is an object, which to start with means +//! JSON. + +use anyhow::{Result, bail}; + +/// `media_type` in its normal form: type and subtype and parameter names in lower +/// case, parameters sorted by name, `; ` between them. Parameter values stay as they +/// are, but for the quotes around a quoted one that needs none. +pub fn normalize(media_type: &str) -> Result<String> { + let mut parts = media_type.split(';'); + let essence = parts.next().unwrap_or_default().trim().to_ascii_lowercase(); + let Some((kind, subtype)) = essence.split_once('/') else { + bail!("type {media_type:?} is not a media type (TYPE/SUBTYPE)"); + }; + if !is_token(kind) || !is_token(subtype) { + bail!("type {media_type:?} is not a media type (TYPE/SUBTYPE)"); + } + let mut parameters = Vec::new(); + for parameter in parts { + let Some((name, value)) = parameter.split_once('=') else { + bail!("type {media_type:?}: parameter {parameter:?} is not NAME=VALUE"); + }; + let name = name.trim().to_ascii_lowercase(); + let mut value = value.trim().to_owned(); + if value.len() >= 2 && value.starts_with('"') && value.ends_with('"') { + let inner = &value[1..value.len() - 1]; + if is_token(inner) { + value = inner.to_owned(); + } + } + if !is_token(&name) || value.is_empty() { + bail!("type {media_type:?}: parameter {parameter:?} is not NAME=VALUE"); + } + if parameters.iter().any(|(other, _)| *other == name) { + bail!("type {media_type:?}: parameter {name} is given twice"); + } + parameters.push((name, value)); + } + parameters.sort(); + let mut normal = essence; + for (name, value) in parameters { + normal.push_str(&format!("; {name}={value}")); + } + Ok(normal) +} + +/// Whether entries of `media_type` (normalized) are keyed: JSON objects, whose keys are +/// records. +pub fn keyed(media_type: &str) -> bool { + let essence = media_type.split(';').next().unwrap_or_default(); + let subtype = essence + .split_once('/') + .map(|(_, subtype)| subtype) + .unwrap_or_default(); + subtype == "json" || subtype.ends_with("+json") +} + +fn is_token(text: &str) -> bool { + !text.is_empty() + && text + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || b"!#$&-^_.+".contains(&byte)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn normal_forms() { + assert_eq!( + normalize("Application/X-OpenSSH-Private-Key;Algorithm=\"ed25519\"").unwrap(), + "application/x-openssh-private-key; algorithm=ed25519" + ); + assert_eq!( + normalize("text/plain; b=2; a=1").unwrap(), + "text/plain; a=1; b=2" + ); + assert!(normalize("text").is_err()); + assert!(normalize("text/plain; a").is_err()); + assert!(normalize("text/plain; a=1; A=2").is_err()); + } + + #[test] + fn keyed_types() { + assert!(keyed("application/x-enstate-hosts+json")); + assert!(keyed("application/json")); + assert!(!keyed("application/octet-stream")); + assert!(!keyed("application/x-pem-file; x=json")); + } +} |
