summaryrefslogtreecommitdiff
path: root/src/tag.rs
blob: a66e0a1a3819af0bc7af33eb4387d8f666618b50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use crate::errors::BumperError;

/// Name git tags after a pattern.
///
/// A `Tag` is created from a text template, which can embed the
/// following for values to be filled in later:
///
/// - `%%` – a single percent character
/// - `%n` – the name of the project
/// - `%v` – the release version number
///
/// The template is parsed by `Tag::new`, which can fail, and the
/// values for project name and release version are filled in by
/// `Tag::apply`.
pub struct Tag {
    patterns: Vec<Pattern>,
}

impl Tag {
    pub fn new(template: &str) -> Result<Self, BumperError> {
        Ok(Self {
            patterns: parse_template(template)?,
        })
    }

    pub fn apply(&self, name: &str, version: &str) -> String {
        let mut result = String::new();
        for p in self.patterns.iter() {
            match p {
                Pattern::Fixed(s) => result.push_str(s),
                Pattern::Name => result.push_str(name),
                Pattern::Version => result.push_str(version),
            }
        }
        result
    }
}

#[derive(Debug)]
enum Pattern {
    Fixed(String),
    Name,
    Version,
}

impl PartialEq for &Pattern {
    fn eq(&self, other: &Self) -> bool {
        match self {
            Pattern::Fixed(s) => match other {
                Pattern::Fixed(t) => s == t,
                _ => false,
            },
            Pattern::Name => matches!(other, Pattern::Name),
            Pattern::Version => matches!(other, Pattern::Version),
        }
    }
}

fn parse_template(t: &str) -> Result<Vec<Pattern>, BumperError> {
    if !t.is_ascii() {
        return Err(BumperError::TagPatternNotAscii(t.to_string()));
    }

    let mut result = vec![];
    let mut t = t.to_string();

    while !t.is_empty() {
        let p = if t.starts_with("%%") {
            t.drain(..2);
            Pattern::Fixed("%".to_string())
        } else if t.starts_with("%v") {
            t.drain(..2);
            Pattern::Version
        } else if t.starts_with("%n") {
            t.drain(..2);
            Pattern::Name
        } else {
            let c = t.get(..1).unwrap().to_string();
            t.drain(..1);
            Pattern::Fixed(c)
        };

        // Combine fixed string with previous fixed string.
        if let Pattern::Fixed(b) = &p {
            if let Some(last) = result.pop() {
                // There was a previous pattern. Is it a fixed string?
                if let Pattern::Fixed(a) = last {
                    let mut ab = a.clone();
                    ab.push_str(b);
                    result.push(Pattern::Fixed(ab))
                } else {
                    result.push(last);
                    result.push(p);
                }
            } else {
                // No previous pattern.
                result.push(p);
            }
        } else {
            result.push(p);
        };
    }

    Ok(result)
}

#[cfg(test)]
mod test {
    use super::{parse_template, Pattern, Tag};

    fn vecs_eq(this: &[Pattern], that: &[Pattern]) -> bool {
        println!();
        println!("this: {:?}", this);
        println!("that: {:?}", that);
        this.iter().eq(that.iter())
    }

    #[test]
    fn empty_template() {
        assert!(vecs_eq(&parse_template("").unwrap(), &[]));
    }

    #[test]
    fn fixed_string() {
        assert!(vecs_eq(
            &parse_template("foo").unwrap(),
            &[Pattern::Fixed("foo".to_string())],
        ));
    }

    #[test]
    fn percent() {
        assert!(vecs_eq(
            &parse_template("%%").unwrap(),
            &[Pattern::Fixed("%".to_string())]
        ));
    }

    #[test]
    fn version() {
        assert!(vecs_eq(&parse_template("%v").unwrap(), &[Pattern::Version]));
    }

    #[test]
    fn name() {
        assert!(vecs_eq(&parse_template("%n").unwrap(), &[Pattern::Name]));
    }

    #[test]
    fn many_parts() {
        assert!(vecs_eq(
            &parse_template("this-is-a-%n-%v-RELEASE").unwrap(),
            &[
                Pattern::Fixed("this-is-a-".to_string()),
                Pattern::Name,
                Pattern::Fixed("-".to_string()),
                Pattern::Version,
                Pattern::Fixed("-RELEASE".to_string())
            ]
        ));
    }

    #[test]
    fn apply() {
        let tag = Tag::new("%n-%v").unwrap();
        assert_eq!(tag.apply("foo", "1.2.3"), "foo-1.2.3");
    }
}