Skip to main content

version_of

Function version_of 

Source
pub fn version_of(o: &Object) -> Option<String>
Expand description

The version or date an object states about itself, for the answer’s version field.

One field, filled from whichever of four keys the object happens to declare, in a fixed order of preference: updated_at, version, date, created_at. The order is “when was this last true” before “which revision is it” before “when was it first written”, because the answer’s version exists so that a reader can spot a stale entry, and the freshest self-description is the one that serves that.

The kinds of the layer disagree about which key they carry — a rule has a version, an ADR a date, an issue an updated_at — so this is a fallback chain rather than a lookup. None is ordinary: plenty of objects say nothing about their own age.

use majordomus_cli::devcontext::select::version_of;
use majordomus_cli::model::{Object, Provenance};
use serde_json::json;

// the object as the index holds it; only its metadata differs between the cases
let with = |metadata: serde_json::Value| -> Option<String> {
    let o = Object {
        kind: "rule".into(),
        identity: "project.x@2".into(),
        uri: "majordomus://rule/project.x@2".into(),
        title: None,
        description: None,
        metadata,
        body: String::new(),
        content: String::new(),
        media_type: "text/markdown",
        provenance: Provenance {
            path: ".ai/repo/rules/project/x.v2.md".into(),
            directory: ".ai/repo/rules/project".into(),
            source_class: "rule".into(),
            section: Some("rules".into()),
            bytes: 2048,
            member: None,
        },
    };
    version_of(&o)
};

// the freshest self-description wins, whichever keys are present
assert_eq!(
    with(json!({"updated_at": "2026-09-12", "version": 2, "created_at": "2026-08-01"})),
    Some("2026-09-12".into()),
);
assert_eq!(with(json!({"version": 2, "date": "2026-08-01"})), Some("2".into()));
assert_eq!(with(json!({"date": "2026-08-01"})), Some("2026-08-01".into()));

// and saying nothing about its own age is ordinary, not a fault
assert_eq!(with(json!({})), None);