ampr_api/
changelog.rs

1use chrono::{DateTime, NaiveDateTime, Utc};
2use semver::Version;
3use serde::{Deserialize, Serialize};
4
5use crate::errors::Error;
6
7/// Describes an entry in the API changelog
8#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
9pub struct ChangeLogEntry {
10    /// Version this change is attached to
11    pub version: Version,
12    /// Time of change
13    pub time: DateTime<Utc>,
14    /// Change message
15    pub message: String,
16}
17
18impl ChangeLogEntry {
19    /// Construct a new ChangeLogEntry
20    pub fn new(version: &String, text: &String) -> Result<Self, Error> {
21        // Split the text into the message and the time
22        //
23        // The incoming text will be formatted as follows:
24        // `201411192022 First production release. 'GET encap' is only endpoint.`
25        let mut split_text = text.split_ascii_whitespace();
26        let timestamp: i64 = split_text.next().unwrap().parse().unwrap();
27        let message = split_text.collect::<Vec<&str>>().join(" ");
28
29        Ok(Self {
30            version: version.parse()?,
31            time: DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc),
32            message,
33        })
34    }
35}