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
// Conserve backup system.
// Copyright 2015, 2016, 2017 Martin Pool.

//! Bands are identified by a string like `b0001-0023`, represented by a `BandId` object.

use std::fmt;

use errors::*;


/// Identifier for a band within an archive, eg 'b0001' or 'b0001-0020'.
///
/// `BandId`s implement a total ordering `std::cmp::Ord`.
#[derive(Debug, PartialEq, Clone, Eq, PartialOrd, Ord)]
pub struct BandId {
    // TODO: Maybe don't store both the vec and the string?

    /// The sequence numbers at each tier.
    seqs: Vec<u32>,

    /// The pre-calculated string form for this id.
    string_form: String,
}

// TODO: Maybe a more concise debug form?


impl BandId {
    /// Makes a new BandId from a sequence of integers.
    pub fn new(seqs: &[u32]) -> BandId {
        assert!(seqs.len() > 0);
        BandId {
            seqs: seqs.to_vec(),
            string_form: BandId::make_string_form(seqs),
        }
    }

    /// Return the origin BandId.
    pub fn zero() -> BandId {
        BandId::new(&[0])
    }

    /// Return the next BandId at the same level as self.
    pub fn next_sibling(self: &BandId) -> BandId {
        let mut next_seqs = self.seqs.clone();
        next_seqs[self.seqs.len() - 1] += 1;
        BandId::new(&next_seqs)
    }

    /// Make a new BandId from a string form.
    pub fn from_string(s: &str) -> Result<BandId> {
        let nope = Err(ErrorKind::InvalidVersion.into());
        if !s.starts_with('b') {
            return nope;
        }
        let mut seqs = Vec::<u32>::new();
        for num_part in s[1..].split('-') {
            match num_part.parse::<u32>() {
                Ok(num) => seqs.push(num),
                Err(..) => return nope,
            }
        }
        if seqs.is_empty() {
            nope
        } else {
            // This rebuilds a new string form to get it into the canonical form.
            Ok(BandId::new(&seqs))
        }
    }

    /// Returns the string representation of this BandId.
    ///
    /// Bands have an id which is a sequence of one or more non-negative integers.
    /// This is externally represented as a string like `b0001-0010`, which becomes
    /// their directory name in the archive.
    ///
    /// Numbers are zero-padded to what should normally be a reasonable length, but they can
    /// be longer.
    pub fn as_string(self: &BandId) -> &String {
        &self.string_form
    }

    fn make_string_form(seqs: &[u32]) -> String {
        let mut result = String::with_capacity(30);
        result.push_str("b");
        for s in seqs {
            result.push_str(&format!("{:04}-", s));
        }
        result.pop(); // remove the last dash
        result.shrink_to_fit();
        result
    }
}


impl fmt::Display for BandId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.as_string().fmt(f)
    }
}


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

    #[test]
    #[should_panic]
    fn empty_id_not_allowed() {
        BandId::new(&[]);
    }

    #[test]
    fn equality() {
        assert_eq!(BandId::new(&[1]), BandId::new(&[1]))
    }

    #[test]
    fn zero() {
        assert_eq!(BandId::zero().as_string(), "b0000");
    }

    #[test]
    fn next() {
        assert_eq!(BandId::zero().next_sibling().as_string(), "b0001");
        assert_eq!(BandId::new(&[2, 3]).next_sibling().as_string(),
                   "b0002-0004");
    }

    #[test]
    fn as_string() {
        let band_id = BandId::new(&[1, 10, 20]);
        assert_eq!(band_id.as_string(), "b0001-0010-0020");
        assert_eq!(BandId::new(&[1000000, 2000000]).as_string(),
                   "b1000000-2000000")
    }

    #[test]
    fn from_string_detects_invalid() {
        assert!(BandId::from_string("").is_err());
        assert!(BandId::from_string("hello").is_err());
        assert!(BandId::from_string("b").is_err());
        assert!(BandId::from_string("b-").is_err());
        assert!(BandId::from_string("b2-").is_err());
        assert!(BandId::from_string("b-2").is_err());
        assert!(BandId::from_string("b2-1-").is_err());
        assert!(BandId::from_string("b2--1").is_err());
        assert!(BandId::from_string("beta").is_err());
        assert!(BandId::from_string("b-eta").is_err());
        assert!(BandId::from_string("b-1eta").is_err());
        assert!(BandId::from_string("b-1-eta").is_err());
    }

    #[test]
    fn from_string_valid() {
        assert_eq!(BandId::from_string("b0001").unwrap().as_string(), "b0001");
        assert_eq!(BandId::from_string("b123456").unwrap().as_string(),
                   "b123456");
        assert_eq!(BandId::from_string("b0001-0100-0234").unwrap().as_string(),
                   "b0001-0100-0234");
    }

    #[test]
    fn format() {
        let a_bandid = BandId::from_string("b0001-0234").unwrap();
        assert_eq!(format!("{}", a_bandid), "b0001-0234");
        // Implements padding correctly
        assert_eq!(format!("{:<15}", a_bandid), "b0001-0234     ");
    }
}