diff options
Diffstat (limited to 'src/federation/revision.rs')
| -rw-r--r-- | src/federation/revision.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/federation/revision.rs b/src/federation/revision.rs index 3c6261b..7be6254 100644 --- a/src/federation/revision.rs +++ b/src/federation/revision.rs @@ -111,6 +111,22 @@ impl Revision { } } +/// The revision a version's `revision` names (`named`, none for the base revision), as +/// people read it: with its ID where it names one, and where this continuum doesn't know +/// it, so said. +pub(crate) fn described(named: Option<&Json>) -> String { + let Some(named) = named else { + return Revision::default().to_string(); + }; + match named.as_str().and_then(Id::from_hex) { + Some(id) => match Revision::of(&id) { + Some(revision) => format!("{revision}, {id}"), + None => format!("{id}, a revision this continuum doesn't know"), + }, + None => "no revision's ID".to_owned(), + } +} + /// As people read it: "the base revision", or "revision 2 (merge-events, …)". impl fmt::Display for Revision { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -148,5 +164,13 @@ mod tests { for revision in Revision::known() { assert_eq!(Revision::of(&revision.id()), Some(revision)); } + assert_eq!(described(None), "the base revision"); + let one = Json::Str(Revision(1).id().to_string()); + assert_eq!( + described(Some(&one)), + format!("revision 1 (merge-events), {}", Revision(1).id()) + ); + let unknown = Json::Str(Id::of(b"elsewhere").to_string()); + assert!(described(Some(&unknown)).ends_with("a revision this continuum doesn't know")); } } |
