use crate::LIVE_ARTIFACT_TITLE_PREFIX;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Slot {
Filed,
Loose,
FollowUp,
Assets,
Parent,
}
impl Slot {
fn name(self) -> &'static str {
match self {
Self::Filed => "filed",
Self::Loose => "loose",
Self::FollowUp => "follow-up",
Self::Assets => "assets",
Self::Parent => "parent",
}
}
fn read(value: &str) -> Option<Self> {
match value {
"filed" => Some(Self::Filed),
"loose" => Some(Self::Loose),
"follow-up" => Some(Self::FollowUp),
"assets" => Some(Self::Assets),
"parent" => Some(Self::Parent),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Scope<'a> {
team: &'a str,
stamp: Option<&'a str>,
}
impl<'a> Scope<'a> {
pub fn ci(team: &'a str) -> Result<Self, String> {
if !team_valid(team) {
return Err("invalid retained Linear team identity".into());
}
Ok(Self { team, stamp: None })
}
pub fn local(team: &'a str, stamp: &'a str) -> Result<Self, String> {
let scope = Self::ci(team)?;
if !stamp_valid(stamp) {
return Err("invalid retained Linear local ownership stamp".into());
}
Ok(Self {
stamp: Some(stamp),
..scope
})
}
#[must_use]
pub fn team(self) -> &'a str {
self.team
}
#[must_use]
pub fn local_stamp(self) -> Option<&'a str> {
self.stamp
}
#[must_use]
pub fn is_ci(self) -> bool {
self.stamp.is_none()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Retained<'a> {
pub scope: Scope<'a>,
pub slot: Slot,
}
fn team_valid(team: &str) -> bool {
!team.is_empty()
&& team
.bytes()
.all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == b'_' || c == b'-')
}
fn stamp_valid(stamp: &str) -> bool {
let parts: Vec<_> = stamp.split('-').collect();
parts.len() == 3
&& parts.iter().all(|part| {
!part.starts_with('0')
&& part.bytes().all(|c| c.is_ascii_digit())
&& part.parse::<u64>().is_ok_and(|value| value > 0)
})
&& parts[..2].iter().all(|part| part.parse::<u32>().is_ok())
}
pub fn retained_title(scope: Scope<'_>, slot: Slot) -> Result<String, String> {
let suffix = match scope.local_stamp() {
None => format!("ci {} {}", scope.team(), slot.name()),
Some(stamp) => format!("local {} {} {stamp}", scope.team(), slot.name()),
};
Ok(format!("{LIVE_ARTIFACT_TITLE_PREFIX}retained-v1 {suffix}"))
}
#[must_use]
pub fn classify_retained(title: &str) -> Option<Retained<'_>> {
let suffix = title
.strip_prefix(LIVE_ARTIFACT_TITLE_PREFIX)?
.strip_prefix("retained-v1 ")?;
let parts: Vec<_> = suffix.split(' ').collect();
let (scope, slot) = match parts.as_slice() {
["ci", team, slot] => (Scope::ci(team).ok()?, Slot::read(slot)?),
["local", team, slot, stamp] => (Scope::local(team, stamp).ok()?, Slot::read(slot)?),
_ => return None,
};
Some(Retained { scope, slot })
}