Debian Codegraph @

src/fields.rs

raw symbols SCIP
1 use std::borrow::Cow;
2
3 /// Whether the patch has been forwarded to the upstream project.
4 #[derive(Debug, PartialEq, Eq, Clone)]
5 pub enum Forwarded {
6 /// The patch has not been forwarded to the upstream project.
7 No,
8
9 /// The patch does not need to be forwarded to the upstream project.
10 NotNeeded,
11
12 /// The patch has been forwarded to the upstream project, and the value
13 /// provides some reference to the forwarded patch.
14 Yes(Cow<'static, str>),
15 }
16
17 impl std::fmt::Display for Forwarded {
18 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 match self {
20 Forwarded::No => f.write_str("no"),
21 Forwarded::NotNeeded => f.write_str("not-needed"),
22 Forwarded::Yes(s) => f.write_str(s),
23 }
24 }
25 }
26
27 impl std::str::FromStr for Forwarded {
28 type Err = &'static str;
29
30 fn from_str(s: &str) -> Result<Self, Self::Err> {
31 match s {
32 "no" => Ok(Forwarded::No),
33 "not-needed" => Ok(Forwarded::NotNeeded),
34 s => Ok(Forwarded::Yes(Cow::Owned(s.to_string()))),
35 }
36 }
37 }
38
39 /// The category of the origin
40 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
41 pub enum OriginCategory {
42 /// an upstream patch that had to be modified to apply on the current version
43 Backport,
44 /// a patch created by Debian or another distribution vendor
45 Vendor,
46 /// a patch cherry-picked from the upstream VCS
47 Upstream,
48
49 /// a patch that does not fit in any of the above categories
50 Other,
51 }
52
53 impl std::fmt::Display for OriginCategory {
54 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 match self {
56 OriginCategory::Backport => f.write_str("backport"),
57 OriginCategory::Vendor => f.write_str("vendor"),
58 OriginCategory::Upstream => f.write_str("upstream"),
59 OriginCategory::Other => f.write_str("other"),
60 }
61 }
62 }
63
64 impl std::str::FromStr for OriginCategory {
65 type Err = &'static str;
66
67 fn from_str(s: &str) -> Result<Self, Self::Err> {
68 match s {
69 "backport" => Ok(OriginCategory::Backport),
70 "vendor" => Ok(OriginCategory::Vendor),
71 "upstream" => Ok(OriginCategory::Upstream),
72 "other" => Ok(OriginCategory::Other),
73 _ => Err("invalid origin category"),
74 }
75 }
76 }
77
78 /// The origin of the patch
79 #[derive(Debug, PartialEq, Eq, Clone)]
80 pub enum Origin {
81 /// The patch was cherry-picked from the upstream VCS
82 Commit(Cow<'static, str>),
83
84 /// Some other origin
85 Other(Cow<'static, str>),
86 }
87
88 impl std::fmt::Display for Origin {
89 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90 match self {
91 Origin::Commit(s) => write!(f, "commit:{}", s),
92 Origin::Other(s) => f.write_str(s),
93 }
94 }
95 }
96
97 impl std::str::FromStr for Origin {
98 type Err = &'static str;
99
100 fn from_str(s: &str) -> Result<Self, Self::Err> {
101 if let Some(rest) = s.strip_prefix("commit:") {
102 Ok(Origin::Commit(Cow::Owned(rest.to_string())))
103 } else {
104 Ok(Origin::Other(Cow::Owned(s.to_string())))
105 }
106 }
107 }
108
109 /// Whether the patch has been applied in the upstream project.
110 #[derive(Debug, PartialEq, Eq, Clone)]
111 pub enum AppliedUpstream {
112 /// The patch has not been applied in the upstream project, in the specified commit.
113 Commit(Cow<'static, str>),
114
115 /// The patch has been applied in the upstream project, in the specified reference.
116 Other(Cow<'static, str>),
117 }
118
119 impl std::fmt::Display for AppliedUpstream {
120 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121 match self {
122 AppliedUpstream::Commit(s) => write!(f, "commit:{}", s),
123 AppliedUpstream::Other(s) => f.write_str(s),
124 }
125 }
126 }
127
128 impl std::str::FromStr for AppliedUpstream {
129 type Err = &'static str;
130
131 fn from_str(s: &str) -> Result<Self, Self::Err> {
132 if let Some(rest) = s.strip_prefix("commit:") {
133 Ok(AppliedUpstream::Commit(Cow::Owned(rest.to_string())))
134 } else {
135 Ok(AppliedUpstream::Other(Cow::Owned(s.to_string())))
136 }
137 }
138 }
139
140 pub(crate) fn parse_origin(s: &str) -> (Option<OriginCategory>, Origin) {
141 // if origin starts with "<category>, " then it is a category
142
143 let mut parts = s.splitn(2, ", ");
144 let (category, s) = match parts.next() {
145 Some("backport") => (Some(OriginCategory::Backport), parts.next().unwrap_or("")),
146 Some("vendor") => (Some(OriginCategory::Vendor), parts.next().unwrap_or("")),
147 Some("upstream") => (Some(OriginCategory::Upstream), parts.next().unwrap_or("")),
148 Some("other") => (Some(OriginCategory::Other), parts.next().unwrap_or("")),
149 None | Some(_) => (None, s),
150 };
151
152 if let Some(rest) = s.strip_prefix("commit:") {
153 (category, Origin::Commit(Cow::Owned(rest.to_string())))
154 } else {
155 (category, Origin::Other(Cow::Owned(s.to_string())))
156 }
157 }
158
159 pub(crate) fn format_origin(category: &Option<OriginCategory>, origin: &Origin) -> String {
160 format!(
161 "{}{}",
162 category.map(|c| c.to_string() + ", ").unwrap_or_default(),
163 origin
164 )
165 }
166
167 /// Parse a Debian BTS bug-id value into a number, accepting:
168 /// - a bare decimal number (`123456`)
169 /// - a `#`-prefixed number
170 /// - a `https://bugs.debian.org/NNNNNN` URL
171 pub fn parse_debian_bug_id(value: &str) -> Option<u32> {
172 let trimmed = value.trim();
173 let candidate = trimmed
174 .trim_start_matches("https://bugs.debian.org/")
175 .trim_start_matches("http://bugs.debian.org/")
176 .trim_start_matches('#');
177 candidate.parse().ok()
178 }
179
180 #[cfg(test)]
181 mod tests {
182 use super::*;
183
184 #[test]
185 fn test_forwarded_display() {
186 assert_eq!(Forwarded::No.to_string(), "no");
187 assert_eq!(Forwarded::Yes(Cow::Borrowed("url")).to_string(), "url");
188 assert_eq!(Forwarded::NotNeeded.to_string(), "not-needed");
189 }
190
191 #[test]
192 fn test_applied_upstream_display() {
193 let commit = AppliedUpstream::Commit(Cow::Borrowed("abc123"));
194 assert_eq!(commit.to_string(), "commit:abc123");
195
196 let other = AppliedUpstream::Other(Cow::Borrowed("merged"));
197 assert_eq!(other.to_string(), "merged");
198 }
199
200 #[test]
201 fn test_parse_debian_bug_id_bare_number() {
202 assert_eq!(parse_debian_bug_id("123456"), Some(123456));
203 }
204
205 #[test]
206 fn test_parse_debian_bug_id_hash_prefix() {
207 assert_eq!(parse_debian_bug_id("#123456"), Some(123456));
208 }
209
210 #[test]
211 fn test_parse_debian_bug_id_https_url() {
212 assert_eq!(
213 parse_debian_bug_id("https://bugs.debian.org/510219"),
214 Some(510219)
215 );
216 }
217
218 #[test]
219 fn test_parse_debian_bug_id_http_url() {
220 assert_eq!(
221 parse_debian_bug_id("http://bugs.debian.org/510219"),
222 Some(510219)
223 );
224 }
225
226 #[test]
227 fn test_parse_debian_bug_id_trims_whitespace() {
228 assert_eq!(parse_debian_bug_id(" 42 "), Some(42));
229 assert_eq!(parse_debian_bug_id("\t#42\n"), Some(42));
230 }
231
232 #[test]
233 fn test_parse_debian_bug_id_rejects_garbage() {
234 assert_eq!(parse_debian_bug_id(""), None);
235 assert_eq!(parse_debian_bug_id("abc"), None);
236 assert_eq!(parse_debian_bug_id("12abc"), None);
237 // Non-Debian BTS URL is not stripped, so the parse fails.
238 assert_eq!(
239 parse_debian_bug_id("https://bugzilla.example.com/9697"),
240 None
241 );
242 // A negative number is not a valid u32.
243 assert_eq!(parse_debian_bug_id("-1"), None);
244 }
245 }