pub enum LeaseFile {
Absent,
Empty,
Corrupt(String),
Document(LeaseDocument),
}Expand description
What a lease file holds, read once. Every state of the file is a variant, so that a reader decides what to do about each rather than learning it from an error.
use majordomus_cli::lease::LeaseFile;
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("server.json");
assert_eq!(LeaseFile::read(&path), LeaseFile::Absent);
assert!(LeaseFile::read(&path).document().is_none(), "no document to hand back");Variants§
Absent
There is no file.
Empty
A file with nothing in it: its owner created it and has not written it yet.
Corrupt(String)
A file that is not a lease document; the reason, for the log.
Document(LeaseDocument)
A lease document.
Implementations§
Source§impl LeaseFile
impl LeaseFile
Sourcepub fn read(path: &Path) -> LeaseFile
pub fn read(path: &Path) -> LeaseFile
Read and type the file at path. Never fails: every state of the file is one of the
variants, and what to do about each is the reader’s decision.
use majordomus_cli::lease::{LeaseFile, SCHEMA};
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("server.json");
assert_eq!(LeaseFile::read(&path), LeaseFile::Absent);
std::fs::write(&path, "").unwrap();
assert_eq!(LeaseFile::read(&path), LeaseFile::Empty);
std::fs::write(&path, "{not json").unwrap();
assert!(matches!(LeaseFile::read(&path), LeaseFile::Corrupt(r) if r.starts_with("not JSON")));
std::fs::write(&path, r#"{"schema":"other/v1"}"#).unwrap();
assert!(matches!(LeaseFile::read(&path), LeaseFile::Corrupt(r) if r.contains(SCHEMA)));
std::fs::write(&path, format!(r#"{{"schema":"{SCHEMA}","token":"x","root":"/r","url":null}}"#)).unwrap();
match LeaseFile::read(&path) {
LeaseFile::Document(d) => { assert_eq!(d.token, "x"); assert!(d.url.is_none()); assert!(d.version.is_none()); }
other => panic!("{other:?}"),
}Sourcepub fn document(&self) -> Option<&LeaseDocument>
pub fn document(&self) -> Option<&LeaseDocument>
The document, when the file holds one; None for an absent, empty or corrupt file,
for a reader that has no different answer for those three.
use majordomus_cli::lease::{LeaseDocument, LeaseFile, SCHEMA};
let doc = LeaseDocument {
schema: SCHEMA.into(), pid: 7, token: "t".into(), root: "/r".into(), url: None,
started_at: "2026-09-10T00:00:00Z".into(), executable: None, version: None,
};
assert_eq!(LeaseFile::Document(doc.clone()).document(), Some(&doc));
assert_eq!(LeaseFile::Corrupt("not JSON".into()).document(), None);Trait Implementations§
impl Eq for LeaseFile
impl StructuralPartialEq for LeaseFile
Auto Trait Implementations§
impl Freeze for LeaseFile
impl RefUnwindSafe for LeaseFile
impl Send for LeaseFile
impl Sync for LeaseFile
impl Unpin for LeaseFile
impl UnsafeUnpin for LeaseFile
impl UnwindSafe for LeaseFile
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.