maven_rs/meta/
snapshot_metadata.rs

1use chrono::NaiveDateTime;
2use serde::{Deserialize, Serialize};
3
4use crate::extension::MavenFileExtension;
5/// The metadata for a snapshot.
6///
7/// File name: `maven-metadata.xml`
8#[derive(Debug, Serialize, Deserialize, Clone)]
9#[serde(rename_all = "camelCase")]
10pub struct SnapshotMetadata {
11    pub group_id: String,
12    pub artifact_id: String,
13    pub version: String,
14    pub versioning: SnapshotVersioning,
15}
16
17impl SnapshotMetadata {
18    /// Returns None if the version is not found in the metadata.
19    pub fn get_latest_artifact_name(
20        &self,
21        extension: impl Into<MavenFileExtension>,
22    ) -> Option<String> {
23        let extension = extension.into();
24        if let Some(ref value) = self.versioning.snapshot_versions {
25            let filter = value.snapshot_version.iter().find(|x| (*x).eq(&extension));
26            return if let Some(value) = filter {
27                let name = format!("{}-{}{}", self.artifact_id, value.value, extension);
28                Some(name)
29            } else {
30                None
31            };
32        }
33        None
34    }
35}
36
37#[derive(Debug, Serialize, Deserialize, Clone, Default)]
38#[serde(rename_all = "camelCase")]
39pub struct SnapshotVersioning {
40    pub snapshot: Option<Snapshot>,
41    pub snapshot_versions: Option<SnapshotVersions>,
42    #[serde(with = "crate::utils::time::standard_time")]
43    pub last_updated: Option<NaiveDateTime>,
44}
45
46#[derive(Debug, Serialize, Deserialize, Clone, Default)]
47pub struct SnapshotVersions {
48    #[serde(rename = "snapshotVersion")]
49    pub snapshot_version: Vec<SnapshotVersion>,
50}
51
52#[derive(Debug, Serialize, Deserialize, Clone)]
53#[serde(rename_all = "camelCase")]
54pub struct Snapshot {
55    #[serde(with = "crate::utils::time::snapshot_time")]
56    pub timestamp: Option<NaiveDateTime>,
57    pub build_number: String,
58}
59
60#[derive(Debug, Serialize, Deserialize, Clone)]
61pub struct SnapshotVersion {
62    pub classifier: Option<String>,
63    #[serde(default)]
64    pub extension: String,
65    pub value: String,
66    #[serde(with = "crate::utils::time::standard_time")]
67    pub updated: Option<NaiveDateTime>,
68}
69
70impl PartialEq<MavenFileExtension> for SnapshotVersion {
71    fn eq(&self, other: &MavenFileExtension) -> bool {
72        self.extension.eq(&other.file_extension) && self.classifier.eq(&other.classifier)
73    }
74}
75
76#[cfg(test)]
77mod test {
78    use pretty_assertions::assert_eq;
79
80    #[test]
81    pub fn parse_snapshot() {
82        let test = r#"
83            <snapshot>
84                <timestamp>20210101.010101</timestamp>
85                <buildNumber>1</buildNumber>
86            </snapshot>
87        "#;
88        let snapshot: super::Snapshot = quick_xml::de::from_str(test).unwrap();
89        assert_eq!(
90            snapshot.timestamp.unwrap().to_string(),
91            "2021-01-01 01:01:01"
92        );
93        assert_eq!(snapshot.build_number, "1");
94    }
95}