1use schemars::JsonSchema;
2use serde::Serialize;
3
4const MAX_SLUG_CHARS: usize = 80;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, JsonSchema)]
8#[serde(rename_all = "lowercase")]
9pub enum Lane {
10 Todo,
11 Doing,
12 Done,
13 Dropped,
14}
15
16impl Lane {
17 pub fn all() -> &'static [Lane] {
18 &[Self::Todo, Self::Doing, Self::Done, Self::Dropped]
19 }
20
21 pub fn as_str(self) -> &'static str {
22 match self {
23 Self::Todo => "todo",
24 Self::Doing => "doing",
25 Self::Done => "done",
26 Self::Dropped => "dropped",
27 }
28 }
29
30 pub fn dir_name(self) -> &'static str {
32 match self {
33 Self::Todo => "1-todo",
34 Self::Doing => "2-doing",
35 Self::Done => "3-done",
36 Self::Dropped => "4-dropped",
37 }
38 }
39
40 pub fn parse(value: &str) -> Option<Self> {
41 match value {
42 "todo" => Some(Self::Todo),
43 "doing" => Some(Self::Doing),
44 "done" => Some(Self::Done),
45 "dropped" => Some(Self::Dropped),
46 _ => None,
47 }
48 }
49}
50
51#[derive(Debug, Clone, Copy, PartialEq, Eq)]
53pub enum StatusFilter {
54 Todo,
55 Doing,
56 Done,
57 Dropped,
58 All,
59}
60
61impl StatusFilter {
62 pub fn parse(value: &str) -> Option<Self> {
63 match value {
64 "todo" => Some(Self::Todo),
65 "doing" => Some(Self::Doing),
66 "done" => Some(Self::Done),
67 "dropped" => Some(Self::Dropped),
68 "all" => Some(Self::All),
69 _ => None,
70 }
71 }
72
73 pub fn lanes(self) -> &'static [Lane] {
74 match self {
75 Self::Todo => &[Lane::Todo],
76 Self::Doing => &[Lane::Doing],
77 Self::Done => &[Lane::Done],
78 Self::Dropped => &[Lane::Dropped],
79 Self::All => Lane::all(),
80 }
81 }
82}
83
84#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
85pub struct TaskSummary {
86 pub id: String,
87 pub title: String,
88 pub status: Lane,
89 pub path: String,
90}
91
92#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
93pub struct TaskRecord {
94 pub id: String,
95 pub title: String,
96 pub status: Lane,
97 pub path: String,
98 pub body: String,
99 pub agents_path: Option<String>,
100 pub agents: Option<String>,
101}
102
103pub fn is_valid_id(id: &str) -> bool {
104 if id.is_empty() {
105 return false;
106 }
107 let mut chars = id.chars().peekable();
108 loop {
109 let Some(start) = chars.next() else {
110 return true;
111 };
112 if !start.is_ascii_lowercase() && !start.is_ascii_digit() {
113 return false;
114 }
115 while matches!(chars.peek(), Some(c) if c.is_ascii_lowercase() || c.is_ascii_digit()) {
116 chars.next();
117 }
118 match chars.peek() {
119 None => return true,
120 Some('-') => {
121 chars.next();
122 if !matches!(chars.peek(), Some(c) if c.is_ascii_lowercase() || c.is_ascii_digit())
123 {
124 return false;
125 }
126 }
127 Some(_) => return false,
128 }
129 }
130}
131
132pub fn slugify(title: &str) -> String {
133 let mut slug = String::new();
134 let mut prev_dash = false;
135 for ch in title.chars() {
136 let c = ch.to_ascii_lowercase();
137 if c.is_ascii_alphanumeric() {
138 slug.push(c);
139 prev_dash = false;
140 } else if !slug.is_empty() && !prev_dash {
141 slug.push('-');
142 prev_dash = true;
143 }
144 }
145 while slug.ends_with('-') {
146 slug.pop();
147 }
148 if slug.chars().count() > MAX_SLUG_CHARS {
149 slug = slug.chars().take(MAX_SLUG_CHARS).collect();
150 while slug.ends_with('-') {
151 slug.pop();
152 }
153 }
154 if slug.is_empty() {
155 "task".to_string()
156 } else {
157 slug
158 }
159}
160
161pub fn title_from_body(body: &str) -> Option<String> {
162 for line in body.lines() {
163 let trimmed = line.trim();
164 let Some(rest) = trimmed.strip_prefix("# ") else {
165 continue;
166 };
167 let title = rest.trim();
168 if !title.is_empty() {
169 return Some(title.to_string());
170 }
171 }
172 None
173}
174
175pub fn default_body(title: &str) -> String {
176 format!("# {title}\n")
177}
178
179pub fn body_with_optional_extra(title: &str, extra: Option<&str>) -> String {
180 match extra {
181 Some(extra) if extra.trim_start().starts_with("# ") => {
182 if extra.ends_with('\n') {
183 extra.to_string()
184 } else {
185 format!("{extra}\n")
186 }
187 }
188 Some(extra) if extra.is_empty() => default_body(title),
189 Some(extra) => {
190 let extra = extra.trim_end_matches('\n');
191 format!("# {title}\n\n{extra}\n")
192 }
193 None => default_body(title),
194 }
195}
196
197#[cfg(test)]
198mod tests {
199 use super::*;
200
201 #[test]
202 fn status_strings_stay_unnumbered_and_dirs_are_numbered() {
203 assert_eq!(Lane::Todo.as_str(), "todo");
204 assert_eq!(Lane::Doing.as_str(), "doing");
205 assert_eq!(Lane::Done.as_str(), "done");
206 assert_eq!(Lane::Dropped.as_str(), "dropped");
207 assert_eq!(Lane::Todo.dir_name(), "1-todo");
208 assert_eq!(Lane::Doing.dir_name(), "2-doing");
209 assert_eq!(Lane::Done.dir_name(), "3-done");
210 assert_eq!(Lane::Dropped.dir_name(), "4-dropped");
211 assert_eq!(Lane::parse("todo"), Some(Lane::Todo));
212 assert_eq!(Lane::parse("1-todo"), None);
213 assert_eq!(StatusFilter::parse("doing"), Some(StatusFilter::Doing));
214 assert_eq!(StatusFilter::parse("2-doing"), None);
215 }
216
217 #[test]
218 fn slugify_table() {
219 assert_eq!(slugify("Fix auth refresh"), "fix-auth-refresh");
220 assert_eq!(slugify(" Hello, World! "), "hello-world");
221 assert_eq!(slugify("!!!"), "task");
222 assert_eq!(slugify("Café au lait"), "caf-au-lait");
223 assert_eq!(slugify("a".repeat(90).as_str()).len(), 80);
224 }
225
226 #[test]
227 fn title_from_body_table() {
228 assert_eq!(
229 title_from_body("# Fix auth\n\nDetails"),
230 Some("Fix auth".into())
231 );
232 assert_eq!(title_from_body("no heading"), None);
233 assert_eq!(
234 title_from_body("intro\n# Later heading\n"),
235 Some("Later heading".into())
236 );
237 }
238
239 #[test]
240 fn id_pattern() {
241 assert!(is_valid_id("fix-auth"));
242 assert!(is_valid_id("a"));
243 assert!(is_valid_id("ab2"));
244 assert!(!is_valid_id(""));
245 assert!(!is_valid_id("-a"));
246 assert!(!is_valid_id("a-"));
247 assert!(!is_valid_id("Fix"));
248 assert!(!is_valid_id("a--b"));
249 }
250}