debian_watch/
mangle.rs

1//! Functions for parsing and applying version and URL mangling expressions.
2//!
3//! Debian watch files use sed-style expressions for transforming versions and URLs.
4
5use regex::Regex;
6
7/// Error type for mangling expression parsing
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum MangleError {
10    /// Not a substitution or translation expression
11    NotMangleExpr(String),
12    /// Invalid substitution expression
13    InvalidSubstExpr(String),
14    /// Invalid translation expression
15    InvalidTranslExpr(String),
16    /// Regex compilation error
17    RegexError(String),
18}
19
20impl std::fmt::Display for MangleError {
21    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
22        match self {
23            MangleError::NotMangleExpr(s) => {
24                write!(f, "not a substitution or translation expression: {}", s)
25            }
26            MangleError::InvalidSubstExpr(s) => write!(f, "invalid substitution expression: {}", s),
27            MangleError::InvalidTranslExpr(s) => write!(f, "invalid translation expression: {}", s),
28            MangleError::RegexError(s) => write!(f, "regex error: {}", s),
29        }
30    }
31}
32
33impl std::error::Error for MangleError {}
34
35/// Type of mangling expression
36#[derive(Debug, Clone, PartialEq, Eq)]
37pub enum MangleExprKind {
38    /// Substitution (s/pattern/replacement/flags)
39    Subst,
40    /// Translation (tr/pattern/replacement/flags or y/pattern/replacement/flags)
41    Transl,
42}
43
44/// A parsed mangling expression
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct MangleExpr {
47    /// The kind of expression
48    pub kind: MangleExprKind,
49    /// The pattern to match
50    pub pattern: String,
51    /// The replacement string
52    pub replacement: String,
53    /// Optional flags
54    pub flags: Option<String>,
55}
56
57/// Parse a mangling expression
58///
59/// # Examples
60///
61/// ```
62/// use debian_watch::mangle::parse_mangle_expr;
63///
64/// let expr = parse_mangle_expr("s/foo/bar/g").unwrap();
65/// assert_eq!(expr.pattern, "foo");
66/// assert_eq!(expr.replacement, "bar");
67/// assert_eq!(expr.flags.as_deref(), Some("g"));
68/// ```
69pub fn parse_mangle_expr(vm: &str) -> Result<MangleExpr, MangleError> {
70    if vm.starts_with('s') {
71        parse_subst_expr(vm)
72    } else if vm.starts_with("tr") {
73        parse_transl_expr(vm)
74    } else if vm.starts_with('y') {
75        parse_transl_expr(vm)
76    } else {
77        Err(MangleError::NotMangleExpr(vm.to_string()))
78    }
79}
80
81/// Parse a substitution expression (s/pattern/replacement/flags)
82///
83/// # Examples
84///
85/// ```
86/// use debian_watch::mangle::parse_subst_expr;
87///
88/// let expr = parse_subst_expr("s/foo/bar/g").unwrap();
89/// assert_eq!(expr.pattern, "foo");
90/// assert_eq!(expr.replacement, "bar");
91/// assert_eq!(expr.flags.as_deref(), Some("g"));
92///
93/// let expr = parse_subst_expr("s|foo|bar|").unwrap();
94/// assert_eq!(expr.pattern, "foo");
95/// assert_eq!(expr.replacement, "bar");
96/// ```
97pub fn parse_subst_expr(vm: &str) -> Result<MangleExpr, MangleError> {
98    if !vm.starts_with('s') {
99        return Err(MangleError::InvalidSubstExpr(
100            "not a substitution expression".to_string(),
101        ));
102    }
103
104    if vm.len() < 2 {
105        return Err(MangleError::InvalidSubstExpr(
106            "expression too short".to_string(),
107        ));
108    }
109
110    let delimiter = vm.chars().nth(1).unwrap();
111    let rest = &vm[2..];
112
113    // Split by unescaped delimiter
114    let parts = split_by_unescaped_delimiter(rest, delimiter);
115
116    if parts.len() < 2 {
117        return Err(MangleError::InvalidSubstExpr(
118            "not enough parts".to_string(),
119        ));
120    }
121
122    let pattern = parts[0].clone();
123    let replacement = parts[1].clone();
124    let flags = if parts.len() > 2 && !parts[2].is_empty() {
125        Some(parts[2].clone())
126    } else {
127        None
128    };
129
130    Ok(MangleExpr {
131        kind: MangleExprKind::Subst,
132        pattern,
133        replacement,
134        flags,
135    })
136}
137
138/// Parse a translation expression (tr/pattern/replacement/flags or y/pattern/replacement/flags)
139///
140/// # Examples
141///
142/// ```
143/// use debian_watch::mangle::parse_transl_expr;
144///
145/// let expr = parse_transl_expr("tr/a-z/A-Z/").unwrap();
146/// assert_eq!(expr.pattern, "a-z");
147/// assert_eq!(expr.replacement, "A-Z");
148/// ```
149pub fn parse_transl_expr(vm: &str) -> Result<MangleExpr, MangleError> {
150    let rest = if vm.starts_with("tr") {
151        &vm[2..]
152    } else if vm.starts_with('y') {
153        &vm[1..]
154    } else {
155        return Err(MangleError::InvalidTranslExpr(
156            "not a translation expression".to_string(),
157        ));
158    };
159
160    if rest.is_empty() {
161        return Err(MangleError::InvalidTranslExpr(
162            "expression too short".to_string(),
163        ));
164    }
165
166    let delimiter = rest.chars().next().unwrap();
167    let rest = &rest[1..];
168
169    // Split by unescaped delimiter
170    let parts = split_by_unescaped_delimiter(rest, delimiter);
171
172    if parts.len() < 2 {
173        return Err(MangleError::InvalidTranslExpr(
174            "not enough parts".to_string(),
175        ));
176    }
177
178    let pattern = parts[0].clone();
179    let replacement = parts[1].clone();
180    let flags = if parts.len() > 2 && !parts[2].is_empty() {
181        Some(parts[2].clone())
182    } else {
183        None
184    };
185
186    Ok(MangleExpr {
187        kind: MangleExprKind::Transl,
188        pattern,
189        replacement,
190        flags,
191    })
192}
193
194/// Split a string by an unescaped delimiter
195fn split_by_unescaped_delimiter(s: &str, delimiter: char) -> Vec<String> {
196    let mut parts = Vec::new();
197    let mut current = String::new();
198    let mut escaped = false;
199
200    for c in s.chars() {
201        if escaped {
202            current.push(c);
203            escaped = false;
204        } else if c == '\\' {
205            current.push(c);
206            escaped = true;
207        } else if c == delimiter {
208            parts.push(current.clone());
209            current.clear();
210        } else {
211            current.push(c);
212        }
213    }
214
215    // Don't forget the last part
216    parts.push(current);
217
218    parts
219}
220
221/// Apply a mangling expression to a string
222///
223/// # Examples
224///
225/// ```
226/// use debian_watch::mangle::apply_mangle;
227///
228/// let result = apply_mangle("s/foo/bar/", "foo baz foo").unwrap();
229/// assert_eq!(result, "bar baz foo");
230///
231/// let result = apply_mangle("s/foo/bar/g", "foo baz foo").unwrap();
232/// assert_eq!(result, "bar baz bar");
233/// ```
234pub fn apply_mangle(vm: &str, orig: &str) -> Result<String, MangleError> {
235    let expr = parse_mangle_expr(vm)?;
236
237    match expr.kind {
238        MangleExprKind::Subst => {
239            let re =
240                Regex::new(&expr.pattern).map_err(|e| MangleError::RegexError(e.to_string()))?;
241
242            // Check if 'g' flag is present for global replacement
243            let global = expr.flags.as_ref().map_or(false, |f| f.contains('g'));
244
245            if global {
246                Ok(re.replace_all(orig, expr.replacement.as_str()).to_string())
247            } else {
248                Ok(re.replace(orig, expr.replacement.as_str()).to_string())
249            }
250        }
251        MangleExprKind::Transl => {
252            // Translation: character-by-character replacement
253            apply_translation(&expr.pattern, &expr.replacement, orig)
254        }
255    }
256}
257
258/// Apply character-by-character translation
259fn apply_translation(pattern: &str, replacement: &str, orig: &str) -> Result<String, MangleError> {
260    // Expand ranges like a-z
261    let from_chars = expand_char_range(pattern);
262    let to_chars = expand_char_range(replacement);
263
264    if from_chars.len() != to_chars.len() {
265        return Err(MangleError::InvalidTranslExpr(
266            "pattern and replacement must have same length".to_string(),
267        ));
268    }
269
270    let mut result = String::new();
271    for c in orig.chars() {
272        if let Some(pos) = from_chars.iter().position(|&fc| fc == c) {
273            result.push(to_chars[pos]);
274        } else {
275            result.push(c);
276        }
277    }
278
279    Ok(result)
280}
281
282/// Expand character ranges like a-z to actual characters
283fn expand_char_range(s: &str) -> Vec<char> {
284    let mut result = Vec::new();
285    let chars: Vec<char> = s.chars().collect();
286    let mut i = 0;
287
288    while i < chars.len() {
289        if i + 2 < chars.len() && chars[i + 1] == '-' {
290            // Range found
291            let start = chars[i];
292            let end = chars[i + 2];
293            for c in (start as u32)..=(end as u32) {
294                if let Some(ch) = char::from_u32(c) {
295                    result.push(ch);
296                }
297            }
298            i += 3;
299        } else {
300            result.push(chars[i]);
301            i += 1;
302        }
303    }
304
305    result
306}
307
308#[cfg(test)]
309mod tests {
310    use super::*;
311
312    #[test]
313    fn test_parse_subst_expr() {
314        let expr = parse_subst_expr("s/foo/bar/g").unwrap();
315        assert_eq!(expr.pattern, "foo");
316        assert_eq!(expr.replacement, "bar");
317        assert_eq!(expr.flags.as_deref(), Some("g"));
318
319        let expr = parse_subst_expr("s|foo|bar|").unwrap();
320        assert_eq!(expr.pattern, "foo");
321        assert_eq!(expr.replacement, "bar");
322        assert_eq!(expr.flags, None);
323
324        let expr = parse_subst_expr("s#a/b#c/d#").unwrap();
325        assert_eq!(expr.pattern, "a/b");
326        assert_eq!(expr.replacement, "c/d");
327    }
328
329    #[test]
330    fn test_parse_transl_expr() {
331        let expr = parse_transl_expr("tr/a-z/A-Z/").unwrap();
332        assert_eq!(expr.pattern, "a-z");
333        assert_eq!(expr.replacement, "A-Z");
334
335        let expr = parse_transl_expr("y/abc/xyz/").unwrap();
336        assert_eq!(expr.pattern, "abc");
337        assert_eq!(expr.replacement, "xyz");
338    }
339
340    #[test]
341    fn test_apply_mangle_subst() {
342        let result = apply_mangle("s/foo/bar/", "foo baz foo").unwrap();
343        assert_eq!(result, "bar baz foo");
344
345        let result = apply_mangle("s/foo/bar/g", "foo baz foo").unwrap();
346        assert_eq!(result, "bar baz bar");
347
348        // Test with regex
349        let result = apply_mangle("s/[0-9]+/X/g", "a1b2c3").unwrap();
350        assert_eq!(result, "aXbXcX");
351    }
352
353    #[test]
354    fn test_apply_mangle_transl() {
355        let result = apply_mangle("tr/a-z/A-Z/", "hello").unwrap();
356        assert_eq!(result, "HELLO");
357
358        let result = apply_mangle("y/abc/xyz/", "aabbcc").unwrap();
359        assert_eq!(result, "xxyyzz");
360    }
361
362    #[test]
363    fn test_expand_char_range() {
364        let result = expand_char_range("a-z");
365        assert_eq!(result.len(), 26);
366        assert_eq!(result[0], 'a');
367        assert_eq!(result[25], 'z');
368
369        let result = expand_char_range("a-c");
370        assert_eq!(result, vec!['a', 'b', 'c']);
371
372        let result = expand_char_range("abc");
373        assert_eq!(result, vec!['a', 'b', 'c']);
374    }
375
376    #[test]
377    fn test_split_by_unescaped_delimiter() {
378        let result = split_by_unescaped_delimiter("foo/bar/baz", '/');
379        assert_eq!(result, vec!["foo", "bar", "baz"]);
380
381        let result = split_by_unescaped_delimiter("foo\\/bar/baz", '/');
382        assert_eq!(result, vec!["foo\\/bar", "baz"]);
383    }
384
385    #[test]
386    fn test_real_world_examples() {
387        // Example from Python code: dversionmangle=s/\+ds//
388        let result = apply_mangle(r"s/\+ds//", "1.0+ds").unwrap();
389        assert_eq!(result, "1.0");
390
391        // Example: filenamemangle
392        let result = apply_mangle(
393            r"s/.+\/v?(\d\S+)\.tar\.gz/syncthing-gtk-$1.tar.gz/",
394            "https://github.com/syncthing/syncthing-gtk/archive/v0.9.4.tar.gz",
395        )
396        .unwrap();
397        assert_eq!(result, "syncthing-gtk-0.9.4.tar.gz");
398    }
399}