use super::InstallCtx;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Facts<'a> {
pub tag: Option<&'a str>,
pub legacy_marker: bool,
pub names_my_exe: bool,
pub claimed_by: Option<&'a str>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Owner {
Mine,
Adopt,
Other(String),
Orphan(Option<String>),
Legacy,
Foreign,
}
pub fn decide(facts: &Facts, ctx: &InstallCtx) -> Owner {
let alive = |id: &str| ctx.known.iter().any(|k| k.id == id && k.exists);
if let Some(tag) = facts.tag {
let tag = tag.trim();
return if tag.is_empty() {
Owner::Orphan(None)
} else if tag == ctx.id {
Owner::Mine
} else if alive(tag) {
Owner::Other(tag.to_string())
} else {
Owner::Orphan(Some(tag.to_string()))
};
}
if facts.legacy_marker {
if let Some(claimant) = facts.claimed_by {
return if claimant == ctx.id {
Owner::Adopt
} else if alive(claimant) {
Owner::Other(claimant.to_string())
} else {
Owner::Orphan(Some(claimant.to_string()))
};
}
return match ctx.primary.as_deref() {
None => Owner::Legacy,
Some(p) if p == ctx.id => Owner::Adopt,
Some(p) if alive(p) => Owner::Other(p.to_string()),
Some(p) => Owner::Orphan(Some(p.to_string())),
};
}
if facts.names_my_exe {
Owner::Adopt
} else {
Owner::Foreign
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::desktop::KnownInstall;
fn ctx(primary: Option<&str>) -> InstallCtx {
InstallCtx {
id: "aaaa".into(),
short: "iaaaa".into(),
name: "GitPoulpi (dev)".into(),
primary: primary.map(str::to_string),
alias_claims: Vec::new(),
known: vec![
KnownInstall {
id: "aaaa".into(),
exists: true,
},
KnownInstall {
id: "bbbb".into(),
exists: true,
},
KnownInstall {
id: "cccc".into(),
exists: false,
},
],
}
}
fn tagged(tag: &str) -> Facts<'_> {
Facts {
tag: Some(tag),
..Facts::default()
}
}
#[test]
fn a_tagged_artifact_is_mine_only_with_my_id() {
let c = ctx(None);
assert_eq!(decide(&tagged("aaaa"), &c), Owner::Mine);
assert_eq!(decide(&tagged("bbbb"), &c), Owner::Other("bbbb".into()));
assert_ne!(decide(&tagged("bbbb"), &c), Owner::Mine);
let both = Facts {
tag: Some("bbbb"),
legacy_marker: true,
names_my_exe: true,
claimed_by: None,
};
assert_eq!(decide(&both, &c), Owner::Other("bbbb".into()));
}
#[test]
fn an_untagged_legacy_artifact_is_legacy_without_a_primary() {
let legacy = Facts {
legacy_marker: true,
..Facts::default()
};
assert_eq!(decide(&legacy, &ctx(None)), Owner::Legacy);
assert_eq!(
decide(&legacy, &ctx(Some("bbbb"))),
Owner::Other("bbbb".into())
);
}
#[test]
fn the_primary_adopts_untagged_legacy_artifacts() {
let legacy = Facts {
legacy_marker: true,
..Facts::default()
};
assert_eq!(decide(&legacy, &ctx(Some("aaaa"))), Owner::Adopt);
let pin = Facts {
names_my_exe: true,
..Facts::default()
};
assert_eq!(decide(&pin, &ctx(None)), Owner::Adopt);
}
#[test]
fn an_untagged_legacy_artifact_goes_to_the_installation_that_claims_its_name() {
let claimed = |by| Facts {
legacy_marker: true,
claimed_by: Some(by),
..Facts::default()
};
let c = ctx(Some("bbbb"));
assert_eq!(decide(&claimed("aaaa"), &c), Owner::Adopt);
assert_eq!(decide(&claimed("bbbb"), &c), Owner::Other("bbbb".into()));
assert_eq!(
decide(&claimed("cccc"), &c),
Owner::Orphan(Some("cccc".into()))
);
assert_eq!(
decide(&claimed("bbbb"), &ctx(Some("aaaa"))),
Owner::Other("bbbb".into())
);
}
#[test]
fn an_artifact_of_a_vanished_installation_is_an_orphan() {
let c = ctx(None);
assert_eq!(
decide(&tagged("cccc"), &c),
Owner::Orphan(Some("cccc".into()))
);
assert_eq!(
decide(&tagged("zzzz"), &c),
Owner::Orphan(Some("zzzz".into()))
);
assert_eq!(decide(&tagged(" "), &c), Owner::Orphan(None));
let legacy = Facts {
legacy_marker: true,
..Facts::default()
};
assert_eq!(
decide(&legacy, &ctx(Some("cccc"))),
Owner::Orphan(Some("cccc".into()))
);
}
#[test]
fn no_marker_no_tag_is_foreign() {
assert_eq!(
decide(&Facts::default(), &ctx(Some("aaaa"))),
Owner::Foreign
);
assert_eq!(decide(&Facts::default(), &ctx(None)), Owner::Foreign);
}
}