onetaskgraph_linear/
live_artifact.rs1use crate::LIVE_ARTIFACT_TITLE_PREFIX;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum Slot {
8 Filed,
10 Loose,
12 FollowUp,
14 Assets,
16 Parent,
18}
19impl Slot {
20 fn name(self) -> &'static str {
21 match self {
22 Self::Filed => "filed",
23 Self::Loose => "loose",
24 Self::FollowUp => "follow-up",
25 Self::Assets => "assets",
26 Self::Parent => "parent",
27 }
28 }
29 fn read(value: &str) -> Option<Self> {
30 match value {
31 "filed" => Some(Self::Filed),
32 "loose" => Some(Self::Loose),
33 "follow-up" => Some(Self::FollowUp),
34 "assets" => Some(Self::Assets),
35 "parent" => Some(Self::Parent),
36 _ => None,
37 }
38 }
39}
40
41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
43pub struct Scope<'a> {
44 team: &'a str,
45 stamp: Option<&'a str>,
46}
47impl<'a> Scope<'a> {
48 pub fn ci(team: &'a str) -> Result<Self, String> {
50 if !team_valid(team) {
51 return Err("invalid retained Linear team identity".into());
52 }
53 Ok(Self { team, stamp: None })
54 }
55
56 pub fn local(team: &'a str, stamp: &'a str) -> Result<Self, String> {
58 let scope = Self::ci(team)?;
59 if !stamp_valid(stamp) {
60 return Err("invalid retained Linear local ownership stamp".into());
61 }
62 Ok(Self {
63 stamp: Some(stamp),
64 ..scope
65 })
66 }
67
68 #[must_use]
70 pub fn team(self) -> &'a str {
71 self.team
72 }
73
74 #[must_use]
76 pub fn local_stamp(self) -> Option<&'a str> {
77 self.stamp
78 }
79
80 #[must_use]
82 pub fn is_ci(self) -> bool {
83 self.stamp.is_none()
84 }
85}
86
87#[derive(Clone, Copy, Debug, PartialEq, Eq)]
89pub struct Retained<'a> {
90 pub scope: Scope<'a>,
92 pub slot: Slot,
94}
95
96fn team_valid(team: &str) -> bool {
97 !team.is_empty()
98 && team
99 .bytes()
100 .all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == b'_' || c == b'-')
101}
102fn stamp_valid(stamp: &str) -> bool {
103 let parts: Vec<_> = stamp.split('-').collect();
104 parts.len() == 3
105 && parts.iter().all(|part| {
106 !part.starts_with('0')
107 && part.bytes().all(|c| c.is_ascii_digit())
108 && part.parse::<u64>().is_ok_and(|value| value > 0)
109 })
110 && parts[..2].iter().all(|part| part.parse::<u32>().is_ok())
111}
112
113pub fn retained_title(scope: Scope<'_>, slot: Slot) -> Result<String, String> {
115 let suffix = match scope.local_stamp() {
116 None => format!("ci {} {}", scope.team(), slot.name()),
117 Some(stamp) => format!("local {} {} {stamp}", scope.team(), slot.name()),
118 };
119 Ok(format!("{LIVE_ARTIFACT_TITLE_PREFIX}retained-v1 {suffix}"))
120}
121
122#[must_use]
125pub fn classify_retained(title: &str) -> Option<Retained<'_>> {
126 let suffix = title
127 .strip_prefix(LIVE_ARTIFACT_TITLE_PREFIX)?
128 .strip_prefix("retained-v1 ")?;
129 let parts: Vec<_> = suffix.split(' ').collect();
130 let (scope, slot) = match parts.as_slice() {
131 ["ci", team, slot] => (Scope::ci(team).ok()?, Slot::read(slot)?),
132 ["local", team, slot, stamp] => (Scope::local(team, stamp).ok()?, Slot::read(slot)?),
133 _ => return None,
134 };
135 Some(Retained { scope, slot })
136}