summaryrefslogtreecommitdiffstats
path: root/vendor/spdx-rs/src/models/annotation.rs
blob: f5396aa1d546019f3976ab2a1de1044634d395f6 (plain)
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
// SPDX-FileCopyrightText: 2020-2021 HH Partners
//
// SPDX-License-Identifier: MIT

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// <https://spdx.github.io/spdx-spec/8-annotations/>
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Annotation {
    /// <https://spdx.github.io/spdx-spec/8-annotations/#81-annotator>
    pub annotator: String,

    /// <https://spdx.github.io/spdx-spec/8-annotations/#82-annotation-date>
    pub annotation_date: DateTime<Utc>,

    /// <https://spdx.github.io/spdx-spec/8-annotations/#83-annotation-type>
    pub annotation_type: AnnotationType,

    /// <https://spdx.github.io/spdx-spec/8-annotations/#84-spdx-identifier-reference>
    // TODO: According to the spec this is mandatory, but the example file doesn't
    // have it.
    pub spdx_identifier_reference: Option<String>,

    /// <https://spdx.github.io/spdx-spec/8-annotations/#85-annotation-comment>
    #[serde(rename = "comment")]
    pub annotation_comment: String,
}

impl Annotation {
    pub fn new(
        annotator: String,
        annotation_date: DateTime<Utc>,
        annotation_type: AnnotationType,
        spdx_identifier_reference: Option<String>,
        annotation_comment: String,
    ) -> Self {
        Self {
            annotator,
            annotation_date,
            annotation_type,
            spdx_identifier_reference,
            annotation_comment,
        }
    }
}

/// <https://spdx.github.io/spdx-spec/8-annotations/#83-annotation-type>
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Copy)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum AnnotationType {
    Review,
    Other,
}

#[cfg(test)]
mod test {
    use std::fs::read_to_string;

    use chrono::TimeZone;

    use crate::models::SPDX;

    use super::*;

    #[test]
    fn annotator() {
        let spdx_file: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx_file.annotations[0].annotator,
            "Person: Jane Doe ()".to_string()
        );
    }

    #[test]
    fn annotation_date() {
        let spdx_file: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx_file.annotations[0].annotation_date,
            Utc.ymd(2010, 1, 29).and_hms(18, 30, 22)
        );
    }

    #[test]
    fn annotation_type() {
        let spdx_file: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx_file.annotations[0].annotation_type,
            AnnotationType::Other
        );
    }

    #[test]
    fn annotation_comment() {
        let spdx_file: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx_file.annotations[0].annotation_comment,
            "Document level annotation"
        );
    }
}