chronver 0.2.1

Chronologic version parsing.
Documentation
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Chronologic version parsing.
//!
//! Chronologic versioning (see <https://chronver.org>) is a set of rules for assigning version
//! numbers.
//!
//! ## `ChronVer` overview
//!
//! Given a version number `YEAR.MONTH.DAY.CHANGESET_IDENTIFIER`, increment the:
//!
//! 1. YEAR version when the year changes,
//! 2. MONTH version when the month changes,
//! 3. DAY version when the day changes, and the
//! 4. `CHANGESET_IDENTIFIER` everytime you commit a change to your package/project.
//!
//! ## Versions
//!
//! A simple `Version` can be constructed by using the `parse` method:
//!
//! ```
//! use chronver::Version;
//! use time::macros::date;
//!
//! assert!(Version::parse("2020.01.06") == Ok(Version {
//!     date: date!(2020-01-06),
//!     changeset: 0,
//!     label: None,
//! }));
//! ```
//!
//! Versions can also be compared with each other:
//!
//! ```
//! use chronver::Version;
//!
//! assert_ne!(
//!     Version::parse("2020.01.06-alpha").unwrap(),
//!     Version::parse("2020.01.06-beta").unwrap()
//! );
//! ```
//!

#![doc(html_root_url = "https://docs.rs/chronver/0.2.0")]
#![forbid(unsafe_code)]
#![deny(clippy::all, clippy::pedantic)]
#![warn(clippy::nursery)]
#![warn(
    missing_docs,
    rustdoc::missing_doc_code_examples,
    clippy::missing_docs_in_private_items
)]

use std::{
    convert::TryFrom,
    fmt::{self, Display},
    str::FromStr,
};

use thiserror::Error;
use time::{format_description::FormatItem, macros::format_description, OffsetDateTime};
pub use time::{Date, Month};

/// An error type for this crate.
#[derive(Error, Debug, Clone, Eq, PartialEq)]
pub enum ChronVerError {
    /// The version string was too short.
    #[error("Version string is too short")]
    TooShort,
    /// An error occurred while parsing the version component.
    #[error("Invalid version string")]
    InvalidVersion(#[from] time::error::Parse),
    /// An error occurred while constructing an version from date components.
    #[error("Invalid date components")]
    InvalidComponents(#[from] time::error::ComponentRange),
    /// An error occurred while parsing the changeset component.
    #[error("Invalid changeset")]
    InvalidChangeset(#[from] std::num::ParseIntError),
    /// An error occurred while parsing the label component.
    #[error("Invalid label")]
    InvalidLabel,
}

/// Represents a version number conforming to the chronologic versioning scheme.
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(try_from = "&str"),
    serde(into = "String")
)]
pub struct Version {
    /// The date of release, to be updated whenever a new release is made on a different date than
    /// the last release.
    pub date: Date,
    /// The changeset number, to be incremented when a change was released on the same day.
    pub changeset: u32,
    /// The optional label, which can have any format or follow a branch formatting (see [`Label`]
    /// for more information).
    ///
    /// The special label `break` is reserved to signal a release with breaking changes.
    ///
    /// [`Label`]: enum.Label.html
    pub label: Option<Label>,
}

/// Minimum length that a version must have to be further processed.
const DATE_LENGTH: usize = 10;
/// Format for the date part of a version.
const DATE_FORMAT: &[FormatItem<'static>] = format_description!("[year].[month].[day]");
/// The special label to decide whether the version introduces breaking changes.
const BREAK_LABEL: &str = "break";

/// Shorthand to return an error when a condition is invalid.
macro_rules! ensure {
    ($cond:expr, $err:expr $(,)?) => {
        if !$cond {
            return Err($err);
        }
    };
}

impl Version {
    /// Parse a string into a chronver object.
    ///
    /// # Examples
    ///
    /// ```
    /// use chronver::{Version, Label};
    /// use time::macros::date;
    ///
    /// // Basic version with just a date
    /// assert_eq!(Version::parse("2020.03.05"), Ok(Version {
    ///     date: date!(2020-03-05),
    ///     changeset: 0,
    ///     label: None,
    /// }));
    ///
    /// // Version with a changeset
    /// assert_eq!(Version::parse("2020.03.05.2"), Ok(Version {
    ///     date: date!(2020-03-05),
    ///     changeset: 2,
    ///     label: None,
    /// }));
    ///
    /// // And with label
    /// assert_eq!(Version::parse("2020.03.05.2-new"), Ok(Version {
    ///     date: date!(2020-03-05),
    ///     changeset: 2,
    ///     label: Some(Label::Text("new".to_owned())),
    /// }));
    /// ```
    ///
    /// # Errors
    ///
    /// An error can occur in two cases. First, when the very first part of the version is not a
    /// valid date in the format `YYYY.MM.DD`. Second, when a **changeset** follows the date but
    /// it is not a valid `u32` number.
    pub fn parse(version: &str) -> Result<Self, ChronVerError> {
        ensure!(version.len() >= DATE_LENGTH, ChronVerError::TooShort);

        let date =
            Date::parse(&version[..DATE_LENGTH], &DATE_FORMAT).map_err(ChronVerError::from)?;

        let rem = &version[DATE_LENGTH..];

        let (changeset, label_pos) = if let Some(rem) = rem.strip_prefix('.') {
            let end = rem.find(|c: char| !c.is_ascii_digit()).unwrap_or(rem.len());
            (rem[..end].parse().map_err(ChronVerError::from)?, end + 1)
        } else {
            ensure!(
                rem.is_empty() || rem.starts_with('-'),
                ChronVerError::InvalidLabel
            );
            (0, 0)
        };

        let rem = &rem[label_pos..];

        let label = if let Some(rem) = rem.strip_prefix('-') {
            Some(rem.into())
        } else {
            ensure!(rem.is_empty(), ChronVerError::InvalidLabel);
            None
        };

        Ok(Self {
            date,
            changeset,
            label,
        })
    }

    /// Update the version to the current date or increment the changeset in case the date
    /// is the same. If a label exists, it will be removed.
    pub fn update(&mut self) {
        let new_date = OffsetDateTime::now_utc().date();
        if self.date == new_date {
            self.changeset += 1;
        } else {
            self.date = new_date;
            self.changeset = 0;
        }
        self.label = None;
    }

    /// Check whether the current version introduces breaking changes.
    ///
    /// # Examples
    ///
    /// ```
    /// use chronver::Version;
    ///
    /// assert!(Version::parse("2020.03.05-break").unwrap().is_breaking());
    /// assert!(!Version::parse("2020.03.05").unwrap().is_breaking());
    /// ```
    #[must_use]
    pub fn is_breaking(&self) -> bool {
        if let Some(Label::Text(label)) = &self.label {
            return label == BREAK_LABEL;
        }
        false
    }
}

impl Default for Version {
    #[must_use]
    fn default() -> Self {
        Self {
            date: OffsetDateTime::now_utc().date(),
            changeset: 0,
            label: None,
        }
    }
}

impl FromStr for Version {
    type Err = ChronVerError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

impl Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(&self.date.format(&DATE_FORMAT).map_err(|_| fmt::Error)?)?;
        if self.changeset > 0 {
            write!(f, ".{}", self.changeset)?;
        }
        if let Some(label) = &self.label {
            write!(f, "-{label}")?;
        }
        Ok(())
    }
}

impl From<Date> for Version {
    #[must_use]
    fn from(date: Date) -> Self {
        Self {
            date,
            changeset: 0,
            label: None,
        }
    }
}

impl TryFrom<(i32, Month, u8)> for Version {
    type Error = ChronVerError;

    fn try_from(tuple: (i32, Month, u8)) -> Result<Self, Self::Error> {
        Date::from_calendar_date(tuple.0, tuple.1, tuple.2)
            .map(Self::from)
            .map_err(Into::into)
    }
}

impl TryFrom<&str> for Version {
    type Error = ChronVerError;

    #[inline]
    fn try_from(s: &str) -> Result<Self, Self::Error> {
        s.parse()
    }
}

impl From<Version> for String {
    #[inline]
    #[must_use]
    fn from(version: Version) -> Self {
        format!("{version}")
    }
}

/// A label in the version metadata.
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(from = "&str"),
    serde(into = "String")
)]
pub enum Label {
    /// A simple text label without a specific format.
    Text(String),
    /// A feature label in the format `BRANCH.CHANGESET`, where the changeset can be
    /// omitted when it is 0.
    Feature {
        /// Name of the feature branch.
        branch: String,
        /// Changeset number, omitted if 0.
        changeset: u32,
    },
}

impl Label {
    ///
    ///
    /// # Examples
    ///
    /// ```
    /// use chronver::Label;
    ///
    /// assert_eq!(Label::parse("test"), Label::Text("test".to_owned()));
    /// assert_eq!(Label::parse("feature.1"), Label::Feature {
    ///     branch: "feature".to_owned(),
    ///     changeset: 1,
    /// });
    /// ```
    #[must_use]
    pub fn parse(label: &str) -> Self {
        if let Some(i) = label.rfind('.') {
            if let Ok(changeset) = label[i + 1..].parse() {
                return Self::Feature {
                    branch: label[..i].to_owned(),
                    changeset,
                };
            }
        }

        Self::Text(label.to_owned())
    }
}

impl Display for Label {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Text(s) => f.write_str(s),
            Self::Feature { branch, changeset } => write!(f, "{branch}.{changeset}"),
        }
    }
}

impl From<&str> for Label {
    #[inline]
    #[must_use]
    fn from(s: &str) -> Self {
        Self::parse(s)
    }
}

impl From<Label> for String {
    #[inline]
    #[must_use]
    fn from(label: Label) -> Self {
        format!("{label}")
    }
}

#[cfg(test)]
mod tests {
    use time::macros::date;

    use super::*;

    #[test]
    fn simple_version() {
        let version = Version::parse("2019.01.06");
        assert_eq!(Version::from(date!(2019 - 01 - 06)), version.unwrap());
    }

    #[test]
    fn with_changeset() {
        let version = Version::parse("2019.01.06.12");
        assert_eq!(
            Version {
                date: date!(2019 - 01 - 06),
                changeset: 12,
                label: None
            },
            version.unwrap()
        );
    }

    #[test]
    fn with_default_changeset() {
        let version = Version::parse("2019.01.06.0");
        assert_eq!(Version::from(date!(2019 - 01 - 06)), version.unwrap());
    }

    #[test]
    fn with_label() {
        let version = Version::parse("2019.01.06-test");
        assert_eq!(
            Version {
                date: date!(2019 - 01 - 06),
                changeset: 0,
                label: Some(Label::Text("test".to_owned()))
            },
            version.unwrap()
        );
    }

    #[test]
    fn with_changeset_and_label() {
        let version = Version::parse("2019.01.06.1-test");
        assert_eq!(
            Version {
                date: date!(2019 - 01 - 06),
                changeset: 1,
                label: Some(Label::Text("test".to_owned()))
            },
            version.unwrap()
        );
    }

    #[test]
    fn with_default_changeset_and_label() {
        let version = Version::parse("2019.01.06.0-test");
        assert_eq!(
            Version {
                date: date!(2019 - 01 - 06),
                changeset: 0,
                label: Some(Label::Text("test".to_owned()))
            },
            version.unwrap()
        );
    }

    #[test]
    fn too_short() {
        let version = Version::parse("2019");
        assert_eq!(ChronVerError::TooShort, version.unwrap_err());
    }

    #[test]
    fn invalid_date() {
        let version = Version::parse("2019.30.01");
        assert!(matches!(
            version.unwrap_err(),
            ChronVerError::InvalidVersion(_)
        ));
    }

    #[test]
    fn invalid_changeset() {
        let version = Version::parse("2019.01.06+111");
        assert_eq!(ChronVerError::InvalidLabel, version.unwrap_err());
    }

    #[test]
    fn invalid_changeset_number() {
        let version = Version::parse("2019.01.06.a");
        assert!(matches!(
            version.unwrap_err(),
            ChronVerError::InvalidChangeset(_)
        ));
    }

    #[test]
    fn invalid_label() {
        let version = Version::parse("2019.01.06.1+test");
        assert_eq!(ChronVerError::InvalidLabel, version.unwrap_err());
    }

    #[cfg(feature = "serde")]
    #[test]
    fn serialize() {
        let version = Version::parse("2019.01.06.1-test.2");
        assert_eq!(
            "\"2019.01.06.1-test.2\"",
            serde_json::to_string(&version.unwrap()).unwrap()
        );

        let version = Version::parse("2019.01.06.1-test");
        assert_eq!(
            "\"2019.01.06.1-test\"",
            serde_json::to_string(&version.unwrap()).unwrap()
        );
    }

    #[cfg(feature = "serde")]
    #[test]
    fn deserialize() {
        let version = Version::parse("2019.01.06.1-test.2");
        assert_eq!(
            serde_json::from_str::<Version>("\"2019.01.06.1-test.2\"").unwrap(),
            version.unwrap()
        );

        let version = Version::parse("2019.01.06.1-test");
        assert_eq!(
            serde_json::from_str::<Version>("\"2019.01.06.1-test\"").unwrap(),
            version.unwrap()
        );
    }
}