Skip to main content

standing_of

Function standing_of 

Source
pub fn standing_of(
    file: &LeaseFile,
    age: Duration,
    answers: impl Fn(&str) -> bool,
    version: &str,
) -> (ServerStanding, Option<String>)
Expand description

Decide where a server stands from what its lease file holds, how old the file is, whether the server it names answers for the checkout, and this executable’s version.

Pure, so that every branch of the decision is a test rather than a deployment:

use majordomus_cli::capability::builtin::server::{standing_of, ServerStanding};
use majordomus_cli::lease::{LeaseDocument, LeaseFile, SCHEMA};
use std::time::Duration;

let young = Duration::ZERO;
let never = |_: &str| false;
let always = |_: &str| true;
assert_eq!(standing_of(&LeaseFile::Absent, young, never, "1.0.0").0, ServerStanding::Absent);
assert_eq!(standing_of(&LeaseFile::Empty, young, never, "1.0.0").0, ServerStanding::Starting);
assert_eq!(standing_of(&LeaseFile::Corrupt("not JSON".into()), young, never, "1.0.0").0, ServerStanding::Stale);

let lease = |url: Option<&str>, version: Option<&str>| LeaseDocument {
    schema: SCHEMA.into(), pid: 1, token: "t".into(), root: "/r".into(),
    url: url.map(str::to_string), started_at: "2026-09-10T00:00:00Z".into(),
    executable: None, version: version.map(str::to_string),
};
// still binding, then abandoned
assert_eq!(standing_of(&LeaseFile::Document(lease(None, None)), young, never, "1.0.0").0, ServerStanding::Starting);
assert_eq!(standing_of(&LeaseFile::Document(lease(None, None)), Duration::from_secs(3600), never, "1.0.0").0, ServerStanding::Stale);
// names a server that does not answer
assert_eq!(standing_of(&LeaseFile::Document(lease(Some("http://127.0.0.1:1"), Some("1.0.0"))), young, never, "1.0.0").0, ServerStanding::Stale);
// answers from this version
assert_eq!(standing_of(&LeaseFile::Document(lease(Some("http://127.0.0.1:1"), Some("1.0.0"))), young, always, "1.0.0").0, ServerStanding::Ready);
// answers from another version, or from before versions were written down
assert_eq!(standing_of(&LeaseFile::Document(lease(Some("http://127.0.0.1:1"), Some("0.9.0"))), young, always, "1.0.0").0, ServerStanding::Outdated);
assert_eq!(standing_of(&LeaseFile::Document(lease(Some("http://127.0.0.1:1"), None)), young, always, "1.0.0").0, ServerStanding::Outdated);