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.rs57
1 files changed, 55 insertions, 2 deletions
diff --git a/src/federation/version.rs b/src/federation/version.rs
index fa534b3..cebb83a 100644
--- a/src/federation/version.rs
+++ b/src/federation/version.rs
@@ -5,7 +5,7 @@ use std::collections::{BTreeMap, BTreeSet};
use super::jcs::Json;
use super::keys;
-use super::revision::Revision;
+use super::revision::{Feature, Revision};
use super::store::Id;
/// The federation this is: a version naming another is none here.
@@ -34,6 +34,10 @@ pub(crate) struct Version {
pub(crate) json: Json,
/// The revision of the federation it follows: the base revision where it names none.
pub(crate) revision: Revision,
+ /// The federation's name, where it gives one (`Feature::FederationName`): spelled as a
+ /// party's name. The first version's, in the root, parts the federation from one of a
+ /// first version alike otherwise; a later version's changes only what it is called.
+ pub(crate) name: Option<String>,
pub(crate) number: u64,
/// The version it changes; none for the first.
pub(crate) changes: Option<Id>,
@@ -157,7 +161,7 @@ impl Version {
json,
"a version",
&["federation", "number", "changes", "keys", "nodes", "rules"],
- &["checkpoint", "revision"],
+ &["checkpoint", "revision", "name"],
)?;
match json.get("federation").and_then(Json::as_int) {
Some(FEDERATION) => {}
@@ -192,6 +196,22 @@ impl Version {
}
}
};
+ let name = match json.get("name") {
+ None => None,
+ Some(_) if !revision.has(Feature::FederationName) => {
+ return Err(format!(
+ "a `name`, which {revision} doesn't give a version: {} does",
+ Feature::FederationName.revision()
+ ));
+ }
+ Some(Json::Str(name)) if is_party(name) => Some(name.clone()),
+ Some(_) => {
+ return Err(
+ "a `name` that is no name: 1 to 64 of A-Z a-z 0-9 . _ @ -, as a party's"
+ .to_owned(),
+ );
+ }
+ };
let number = json
.get("number")
.and_then(Json::as_int)
@@ -226,6 +246,7 @@ impl Version {
hash: Id::of(&json.bytes()),
json: json.clone(),
revision,
+ name,
number,
changes,
parties,
@@ -624,6 +645,38 @@ mod tests {
}
}
+ /// A version gives its federation a name only under a revision that has names, spelled
+ /// as a party's name; one without gives none.
+ #[test]
+ fn a_version_names_its_federation_under_a_revision_with_names() {
+ let named = Feature::FederationName.revision();
+ let with = |revision: Revision, name: &str| {
+ let text = String::from_utf8(first().bytes()).expect("UTF-8");
+ let text = text.replacen(r#""nodes":"#, &format!(r#""name":{name},"nodes":"#), 1);
+ let text = text.replacen(
+ r#""rules":"#,
+ &format!(r#""revision":"{}","rules":"#, revision.id()),
+ 1,
+ );
+ Version::read(&jcs::read(text.as_bytes()).expect("JCS"))
+ };
+ assert_eq!(
+ with(named, r#""krebs""#)
+ .expect("a version")
+ .name
+ .as_deref(),
+ Some("krebs")
+ );
+ let why = with(Revision(named.0 - 1), r#""krebs""#).unwrap_err();
+ assert!(why.starts_with("a `name`, which revision 1"), "{why}");
+ let long = format!(r#""{}""#, "x".repeat(65));
+ for none in [r#""""#, r#""a/b""#, "1", long.as_str()] {
+ let why = with(named, none).unwrap_err();
+ assert!(why.contains("no name"), "{none}: {why}");
+ }
+ assert_eq!(Version::read(&first()).expect("a version").name, None);
+ }
+
#[test]
fn a_version_changes_another_by_its_number_hash_and_sinces() {
let first = Version::read(&first()).expect("a version");