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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

/*! Debian package version string handling. */

use {
    crate::error::{DebianError, Result},
    std::{
        cmp::Ordering,
        fmt::{Display, Formatter},
        str::FromStr,
    },
};

/// A Debian package version.
///
/// Debian package versions consist of multiple sub-components and have rules about
/// sorting. The semantics are defined at
/// <https://www.debian.org/doc/debian-policy/ch-controlfields.html#version>. This type
/// attempts to implement all the details.
///
/// The concise version is the format is `[epoch:]upstream_version[-debian_revision]`
/// and each component has rules about what characters are allowed. Our
/// [Self::parse()] should be compliant with the specification and reject invalid
/// version strings and parse components to the appropriate field.
///
/// This type implements a custom ordering function that implements the complex rules
/// around Debian package version ordering.
///
/// ```rust
/// use debian_packaging::package_version::PackageVersion;
///
/// let v = PackageVersion::parse("1:4.7.0+dfsg1-2").unwrap();
/// assert_eq!(v.epoch(), Some(1));
/// assert_eq!(v.upstream_version(), "4.7.0+dfsg1");
/// assert_eq!(v.debian_revision(), Some("2"));
/// assert_eq!(format!("{}", v), "1:4.7.0+dfsg1-2");
///
/// assert!(v < PackageVersion::parse("1:4.7.0+dfsg1-3").unwrap());
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PackageVersion {
    epoch: Option<u32>,
    upstream_version: String,
    debian_revision: Option<String>,
}

impl PackageVersion {
    /// Construct an instance by parsing a version string.
    pub fn parse(s: &str) -> Result<Self> {
        // Epoch is the part before a colon, if present.
        // upstream_version and debian_revision are discovered by splitting on last hyphen.

        let (epoch, remainder) = if let Some(pos) = s.find(':') {
            (Some(&s[0..pos]), &s[pos + 1..])
        } else {
            (None, s)
        };

        let (upstream, debian) = if let Some(pos) = remainder.rfind('-') {
            (&remainder[0..pos], Some(&remainder[pos + 1..]))
        } else {
            (remainder, None)
        };

        // Now do our validation.

        // The epoch is numeric.
        let epoch = if let Some(epoch) = epoch {
            if !epoch.chars().all(|c| c.is_ascii_digit()) {
                return Err(DebianError::EpochNonNumeric(s.to_string()));
            }

            Some(u32::from_str(epoch)?)
        } else {
            None
        };

        // The upstream_version must contain only alphanumerics and the characters . + - ~ (full stop,
        // plus, hyphen, tilde) and should start with a digit. If there is no debian_revision then
        // hyphens are not allowed.
        if !upstream.chars().all(|c| match c {
            c if c.is_ascii_alphanumeric() => true,
            '.' | '+' | '~' => true,
            '-' => debian.is_some(),
            _ => false,
        }) {
            return Err(DebianError::UpstreamVersionIllegalChar(s.to_string()));
        }

        let upstream_version = upstream.to_string();

        let debian_revision = if let Some(debian) = debian {
            // It must contain only alphanumerics and the characters + . ~ (plus, full stop, tilde)
            if !debian.chars().all(|c| match c {
                c if c.is_ascii_alphanumeric() => true,
                '+' | '.' | '~' => true,
                _ => false,
            }) {
                return Err(DebianError::DebianRevisionIllegalChar(s.to_string()));
            }

            Some(debian.to_string())
        } else {
            None
        };

        Ok(Self {
            epoch,
            upstream_version,
            debian_revision,
        })
    }

    /// The `epoch` component of the version string.
    ///
    /// Only `Some` if present or defined explicitly.
    pub fn epoch(&self) -> Option<u32> {
        self.epoch
    }

    /// Assumed value of `epoch` component.
    ///
    /// If the component isn't explicitly defined, a default of `0` will be assumed.
    pub fn epoch_assumed(&self) -> u32 {
        if let Some(epoch) = &self.epoch {
            *epoch
        } else {
            0
        }
    }

    /// `upstream` component of the version string.
    ///
    /// This is the main part of the version number.
    ///
    /// It is typically the original version of the software from which this package came. Although
    /// it may be massaged to be compatible with packaging requirements.
    pub fn upstream_version(&self) -> &str {
        &self.upstream_version
    }

    /// `debian_revision` component of the version string.
    ///
    /// The part of the version string that specifies the version of the Debian package based on
    /// the upstream version.
    pub fn debian_revision(&self) -> Option<&str> {
        self.debian_revision.as_deref()
    }
}

impl Display for PackageVersion {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        // [epoch:]upstream_version[-debian_revision]
        write!(
            f,
            "{}{}{}{}{}",
            if let Some(epoch) = self.epoch {
                format!("{}", epoch)
            } else {
                "".to_string()
            },
            if self.epoch.is_some() { ":" } else { "" },
            self.upstream_version,
            if self.debian_revision.is_some() {
                "-"
            } else {
                ""
            },
            if let Some(v) = &self.debian_revision {
                v
            } else {
                ""
            }
        )
    }
}

/// Split a string on the first non-digit character.
///
/// Returns the leading component with non-digits and everything else afterwards.
/// Either value can be an empty string.
fn split_first_digit(s: &str) -> (&str, &str) {
    let first_nondigit_index = s.chars().position(|c| c.is_ascii_digit());

    match first_nondigit_index {
        Some(0) => ("", s),
        Some(pos) => (&s[0..pos], &s[pos..]),
        None => (s, ""),
    }
}

fn split_first_nondigit(s: &str) -> (&str, &str) {
    let pos = s.chars().position(|c| !c.is_ascii_digit());

    match pos {
        Some(0) => ("", s),
        Some(pos) => (&s[0..pos], &s[pos..]),
        None => (s, ""),
    }
}

/// Split a string on the first non-digit character and convert the leading digits to an integer.
fn split_first_digit_number(s: &str) -> (u64, &str) {
    let (digits, remaining) = split_first_nondigit(s);

    let numeric = if digits.is_empty() {
        0
    } else {
        u64::from_str(digits).expect("digits should deserialize to string")
    };

    (numeric, remaining)
}

fn lexical_compare(a: &str, b: &str) -> Ordering {
    // The lexical comparison is a comparison of ASCII values modified so that all the letters sort
    // earlier than all the non-letters and so that a tilde sorts before anything, even the end of a
    // part.

    // We change the order of the strings so tildes come first, followed by letters, followed
    // by everything else.
    let compare_char = |a: &char, b: &char| -> Ordering {
        match (a, b) {
            ('~', '~') => Ordering::Equal,
            ('~', _) => Ordering::Less,
            (_, '~') => Ordering::Greater,
            (a, b) if a.is_ascii_alphabetic() && !b.is_ascii_alphabetic() => Ordering::Less,
            (a, b) if !a.is_ascii_alphabetic() && b.is_ascii_alphabetic() => Ordering::Greater,
            (_, _) => Ordering::Equal,
        }
    };

    let mut a_chars = a.chars().collect::<Vec<_>>();
    let mut b_chars = b.chars().collect::<Vec<_>>();
    a_chars.sort_by(compare_char);
    b_chars.sort_by(compare_char);

    // We then compare character by character, taking our modified lexical sort into
    // consideration. This gets funky when string lengths are different. Normally the
    // shorter string would sort lower. But our custom lexical compare applies when comparing
    // against a missing character!

    for pos in 0..std::cmp::max(a_chars.len(), b_chars.len()) {
        let a_char = a_chars.get(pos);
        let b_char = b_chars.get(pos);

        match (a_char, b_char) {
            (Some(a_char), None) if *a_char == '~' => {
                return Ordering::Less;
            }
            (Some(_), None) => {
                return Ordering::Greater;
            }
            (None, Some(b_char)) if *b_char == '~' => {
                return Ordering::Greater;
            }
            (None, Some(_)) => {
                return Ordering::Less;
            }
            (Some(a_char), Some(b_char)) => match compare_char(a_char, b_char) {
                Ordering::Equal => {}
                res => {
                    return res;
                }
            },
            (None, None) => {
                panic!("None, None variant should never happen");
            }
        }
    }

    Ordering::Equal
}

/// Compare a version component string using Debian rules.
fn compare_component(a: &str, b: &str) -> Ordering {
    // The comparison consists of iterations of a 2 step process until both inputs are exhausted.
    //
    // Step 1: Initial part of each string consisting of non-digit characters is compared using
    // a custom lexical sort.
    //
    // Step 2: Initial part of remaining string consisting of digit characters is compared using
    // numerical sort.
    let mut a_remaining = a;
    let mut b_remaining = b;

    loop {
        let a_res = split_first_digit(a_remaining);
        let a_leading_nondigit = a_res.0;
        a_remaining = a_res.1;

        let b_res = split_first_digit(b_remaining);
        let b_leading_nondigit = b_res.0;
        b_remaining = b_res.1;

        // These two parts (one of which may be empty) are compared lexically. If a difference is
        // found it is returned.
        match lexical_compare(a_leading_nondigit, b_leading_nondigit) {
            Ordering::Equal => {}
            res => {
                return res;
            }
        }

        // Then the initial part of the remainder of each string which consists entirely of digit
        // characters is determined.

        // The numerical values of these two parts are compared, and any difference found is
        // returned as the result of the comparison. For these purposes an empty string (which can
        // only occur at the end of one or both version strings being compared) counts as zero.

        let a_res = split_first_digit_number(a_remaining);
        let a_numeric = a_res.0;
        a_remaining = a_res.1;

        let b_res = split_first_digit_number(b_remaining);
        let b_numeric = b_res.0;
        b_remaining = b_res.1;

        match a_numeric.cmp(&b_numeric) {
            Ordering::Equal => {}
            res => {
                return res;
            }
        }

        if a_remaining.is_empty() && b_remaining.is_empty() {
            return Ordering::Equal;
        }
    }
}

impl PartialOrd<Self> for PackageVersion {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        // Epoch is compared numerically. Then upstream and debian components are compared
        // using a custom algorithm. The absence of a debian revision is equivalent to `0`.

        match self.epoch_assumed().cmp(&other.epoch_assumed()) {
            Ordering::Less => Some(Ordering::Less),
            Ordering::Greater => Some(Ordering::Greater),
            Ordering::Equal => {
                match compare_component(&self.upstream_version, &other.upstream_version) {
                    Ordering::Less => Some(Ordering::Less),
                    Ordering::Greater => Some(Ordering::Greater),
                    Ordering::Equal => {
                        let a = self.debian_revision.as_deref().unwrap_or("0");
                        let b = other.debian_revision.as_deref().unwrap_or("0");

                        Some(compare_component(a, b))
                    }
                }
            }
        }
    }
}

impl Ord for PackageVersion {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn parse() -> Result<()> {
        assert_eq!(
            PackageVersion::parse("1:4.7.0+dfsg1-2")?,
            PackageVersion {
                epoch: Some(1),
                upstream_version: "4.7.0+dfsg1".into(),
                debian_revision: Some("2".into()),
            }
        );
        assert_eq!(
            PackageVersion::parse("3.3.2.final~github")?,
            PackageVersion {
                epoch: None,
                upstream_version: "3.3.2.final~github".into(),
                debian_revision: None,
            }
        );
        assert_eq!(
            PackageVersion::parse("3.3.2.final~github-2")?,
            PackageVersion {
                epoch: None,
                upstream_version: "3.3.2.final~github".into(),
                debian_revision: Some("2".into()),
            }
        );
        assert_eq!(
            PackageVersion::parse("0.18.0+dfsg-2+b1")?,
            PackageVersion {
                epoch: None,
                upstream_version: "0.18.0+dfsg".into(),
                debian_revision: Some("2+b1".into())
            }
        );

        Ok(())
    }

    #[test]
    fn format() -> Result<()> {
        for s in ["1:4.7.0+dfsg1-2", "3.3.2.final~github", "0.18.0+dfsg-2+b1"] {
            let v = PackageVersion::parse(s)?;
            assert_eq!(format!("{}", v), s);
        }

        Ok(())
    }

    #[test]
    fn test_lexical_compare() {
        assert_eq!(lexical_compare("~~", "~~a"), Ordering::Less);
        assert_eq!(lexical_compare("~~a", "~~"), Ordering::Greater);
        assert_eq!(lexical_compare("~~a", "~"), Ordering::Less);
        assert_eq!(lexical_compare("~", "~~a"), Ordering::Greater);
        assert_eq!(lexical_compare("~", ""), Ordering::Less);
        assert_eq!(lexical_compare("", "~"), Ordering::Greater);
        assert_eq!(lexical_compare("", "a"), Ordering::Less);
        assert_eq!(lexical_compare("a", ""), Ordering::Greater);

        // 1.0~beta1~svn1245 sorts earlier than 1.0~beta1, which sorts earlier than 1.0.
    }

    #[test]
    fn test_compare_component() {
        assert_eq!(
            compare_component("1.0~beta1~svn1245", "1.0~beta1"),
            Ordering::Less
        );
        assert_eq!(compare_component("1.0~beta1", "1.0"), Ordering::Less);
    }

    #[test]
    fn compare_version() {
        assert_eq!(
            PackageVersion {
                epoch: Some(1),
                upstream_version: "ignored".into(),
                debian_revision: None,
            }
            .cmp(&PackageVersion {
                epoch: Some(0),
                upstream_version: "ignored".into(),
                debian_revision: None
            }),
            Ordering::Greater
        );
    }
}