aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/version.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation/version.rs')
-rw-r--r--src/federation/version.rs113
1 files changed, 81 insertions, 32 deletions
diff --git a/src/federation/version.rs b/src/federation/version.rs
index 1547ec1..d714a47 100644
--- a/src/federation/version.rs
+++ b/src/federation/version.rs
@@ -119,31 +119,46 @@ fn since(json: &Json) -> Result<u64, String> {
.ok_or_else(|| "a `since` that is no number".to_owned())
}
-/// Whether `json` has these names and no others; `optional` ones it may leave out.
-fn names(json: &Json, required: &[&str], optional: &[&str]) -> bool {
- json.is_object()
- && required.iter().all(|name| json.get(name).is_some())
- && json
- .names()
- .iter()
- .all(|name| required.contains(name) || optional.contains(name))
+/// That `json`, `what`, has these names and no others, `optional` ones it may leave out;
+/// else which it lacks, or which it has more, that this continuum doesn't know.
+fn fields(json: &Json, what: &str, required: &[&str], optional: &[&str]) -> Result<(), String> {
+ if !json.is_object() {
+ return Err(format!("{what} that is no object"));
+ }
+ if let Some(name) = required.iter().find(|name| json.get(name).is_none()) {
+ return Err(format!("{what} without `{name}`"));
+ }
+ match json
+ .names()
+ .into_iter()
+ .find(|name| !required.contains(name) && !optional.contains(name))
+ {
+ Some(name) => Err(format!(
+ "{what} with `{name}`, a field this continuum doesn't know"
+ )),
+ None => Ok(()),
+ }
}
impl Version {
/// The version `json` is, where it is one by itself: a field missing or more, or any of
- /// the rest of what makes a version none, is an error. Whether it changes the version
- /// it says is asked apart (`changes_validly`).
+ /// the rest of what makes a version none, is an error, saying which. Whether it changes
+ /// the version it says is asked apart (`changes_validly`).
pub(crate) fn read(json: &Json) -> Result<Version, String> {
- if !names(
+ fields(
json,
+ "a version",
&["federation", "number", "changes", "keys", "nodes", "rules"],
&["checkpoint"],
- ) {
- return Err("a field missing or more".to_owned());
- }
- let federation = json.get("federation").and_then(Json::as_int);
- if federation != Some(FEDERATION) {
- return Err(format!("of federation {federation:?}, not {FEDERATION}"));
+ )?;
+ match json.get("federation").and_then(Json::as_int) {
+ Some(FEDERATION) => {}
+ Some(federation) => {
+ return Err(format!(
+ "of federation {federation}, and this continuum knows {FEDERATION} alone"
+ ));
+ }
+ None => return Err("a federation that is no number".to_owned()),
}
let number = json
.get("number")
@@ -204,23 +219,26 @@ impl Version {
let (Some(key_json), Some(node_json)) = (keys.get(name), nodes.get(name)) else {
return Err("keys and node lists not of the same parties".to_owned());
};
- if !names(key_json, &["since", "keys"], &[])
- || !names(node_json, &["since", "nodes"], &[])
- {
- return Err(format!(
- "{name}'s keys or node list: a field missing or more"
- ));
- }
+ fields(key_json, &format!("{name}'s keys"), &["since", "keys"], &[])?;
+ fields(
+ node_json,
+ &format!("{name}'s node list"),
+ &["since", "nodes"],
+ &[],
+ )?;
let party_keys = key_json
.get("keys")
.and_then(distinct_strings)
.ok_or_else(|| format!("{name}'s keys aren't a list of keys"))?;
if party_keys.is_empty() || party_keys.len() > KEYS {
- return Err(format!("{name} with {} keys", party_keys.len()));
+ return Err(format!(
+ "{name} with {} keys, not 1 to {KEYS}",
+ party_keys.len()
+ ));
}
for key in &party_keys {
- if keys::key(key).is_none() {
- return Err(format!("{name}'s key {key:?} isn't spelled as a key"));
+ if let Err(why) = keys::spelled(key) {
+ return Err(format!("{name}'s key {key:?}: {why}"));
}
if !every_key.insert(key.clone()) {
return Err(format!("{key:?} twice in the version"));
@@ -230,8 +248,14 @@ impl Version {
.get("nodes")
.and_then(distinct_strings)
.ok_or_else(|| format!("{name}'s node list isn't a list of node IDs"))?;
- if party_nodes.len() > NODES || !party_nodes.iter().all(|id| keys::node_id(id)) {
- return Err(format!("{name}'s node list"));
+ if party_nodes.len() > NODES {
+ return Err(format!(
+ "{name}'s node list of {} nodes, more than {NODES}",
+ party_nodes.len()
+ ));
+ }
+ if let Some(id) = party_nodes.iter().find(|id| !keys::node_id(id)) {
+ return Err(format!("{name}'s node {id:?}, no node ID"));
}
parties.insert(
name.to_owned(),
@@ -315,9 +339,7 @@ impl Version {
impl Rule {
fn read(json: &Json, parties: &BTreeMap<String, Party>) -> Result<Rule, String> {
- if !names(json, &["path", "writer"], &[]) {
- return Err("a rule: a field missing or more".to_owned());
- }
+ fields(json, "a rule", &["path", "writer"], &[])?;
let text = json
.get("path")
.and_then(Json::as_str)
@@ -485,6 +507,33 @@ mod tests {
assert!(with_rule(&vec!["a"; SEGMENTS + 1].join("/"), tv()).is_err());
}
+ /// What makes a version none, said: a field it lacks or one more, which this
+ /// continuum doesn't know, a federation it doesn't know, a key of a type it doesn't
+ /// take.
+ #[test]
+ fn a_version_that_is_none_says_why() {
+ let edited = |edit: &dyn Fn(&str) -> String| {
+ let text = String::from_utf8(first().bytes()).expect("UTF-8");
+ Version::read(&jcs::read(edit(&text).as_bytes()).expect("JCS")).unwrap_err()
+ };
+ let why = edited(&|text| text.replacen(r#""nodes":"#, r#""later":true,"nodes":"#, 1));
+ assert_eq!(
+ why,
+ "a version with `later`, a field this continuum doesn't know"
+ );
+ let why = edited(&|text| text.replace(r#""federation":1,"#, ""));
+ assert_eq!(why, "a version without `federation`");
+ let why = edited(&|text| text.replace(r#""federation":1"#, r#""federation":2"#));
+ assert_eq!(why, "of federation 2, and this continuum knows 1 alone");
+ let why = edited(&|text| text.replace("ssh-ed25519 ", "sk-ssh-ed25519@openssh.com "));
+ assert!(why.contains("a sk-ssh-ed25519@openssh.com key"), "{why}");
+ let why = edited(&|text| text.replacen(r#""since":0"#, r#""since":0,"x":1"#, 1));
+ assert!(
+ why.ends_with("with `x`, a field this continuum doesn't know"),
+ "{why}"
+ );
+ }
+
#[test]
fn a_version_changes_another_by_its_number_hash_and_sinces() {
let first = Version::read(&first()).expect("a version");