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
// SPDX-FileCopyrightText: 2020-2021 HH Partners
//
// SPDX-License-Identifier: MIT

use serde::{Deserialize, Serialize};
use spdx_expression::SpdxExpression;

/// <https://spdx.github.io/spdx-spec/5-snippet-information/>
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Default)]
pub struct Snippet {
    /// <https://spdx.github.io/spdx-spec/5-snippet-information/#51-snippet-spdx-identifier>
    #[serde(rename = "SPDXID")]
    pub snippet_spdx_identifier: String,

    /// <https://spdx.github.io/spdx-spec/5-snippet-information/#52-snippet-from-file-spdx-identifier>
    #[serde(rename = "snippetFromFile")]
    pub snippet_from_file_spdx_identifier: String,

    /// <https://spdx.github.io/spdx-spec/5-snippet-information/#53-snippet-byte-range>
    pub ranges: Vec<Range>,

    /// <https://spdx.github.io/spdx-spec/5-snippet-information/#55-snippet-concluded-license>
    #[serde(
        rename = "licenseConcluded",
        skip_serializing_if = "Option::is_none",
        default
    )]
    pub snippet_concluded_license: Option<SpdxExpression>,

    /// <https://spdx.github.io/spdx-spec/5-snippet-information/#56-license-information-in-snippet>
    #[serde(
        rename = "licenseInfoInSnippets",
        skip_serializing_if = "Vec::is_empty",
        default
    )]
    pub license_information_in_snippet: Vec<String>,

    /// <https://spdx.github.io/spdx-spec/5-snippet-information/#57-snippet-comments-on-license>
    #[serde(
        rename = "licenseComments",
        skip_serializing_if = "Option::is_none",
        default
    )]
    pub snippet_comments_on_license: Option<String>,

    /// <https://spdx.github.io/spdx-spec/5-snippet-information/#58-snippet-copyright-text>
    #[serde(
        rename = "copyrightText",
        skip_serializing_if = "Option::is_none",
        default
    )]
    pub snippet_copyright_text: Option<String>,

    /// <https://spdx.github.io/spdx-spec/5-snippet-information/#59-snippet-comment>
    #[serde(rename = "comment", skip_serializing_if = "Option::is_none", default)]
    pub snippet_comment: Option<String>,

    /// <https://spdx.github.io/spdx-spec/5-snippet-information/#510-snippet-name>
    #[serde(rename = "name", skip_serializing_if = "Option::is_none", default)]
    pub snippet_name: Option<String>,

    /// <https://spdx.github.io/spdx-spec/5-snippet-information/#511-snippet-attribution-text>
    #[serde(
        rename = "attributionText",
        skip_serializing_if = "Option::is_none",
        default
    )]
    pub snippet_attribution_text: Option<String>,
}

/// <https://spdx.github.io/spdx-spec/5-snippet-information/#53-snippet-byte-range>
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Range {
    pub start_pointer: Pointer,
    pub end_pointer: Pointer,
}

impl Range {
    pub fn new(start_pointer: Pointer, end_pointer: Pointer) -> Self {
        Self {
            start_pointer,
            end_pointer,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(untagged)]
pub enum Pointer {
    Byte {
        reference: Option<String>,
        offset: i32,
    },
    Line {
        reference: Option<String>,
        #[serde(rename = "lineNumber")]
        line_number: i32,
    },
}

impl Pointer {
    /// Create a new [`Pointer::Byte`].
    pub fn new_byte(reference: Option<String>, offset: i32) -> Self {
        Self::Byte { reference, offset }
    }

    /// Create a new [`Pointer::Line`].
    pub fn new_line(reference: Option<String>, line_number: i32) -> Self {
        Self::Line {
            reference,
            line_number,
        }
    }
}

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

    use crate::models::SPDX;

    use super::*;

    #[test]
    fn snippet_spdx_identifier() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.snippet_information[0].snippet_spdx_identifier,
            "SPDXRef-Snippet".to_string()
        );
    }
    #[test]
    fn snippet_from_file_spdx_identifier() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.snippet_information[0].snippet_from_file_spdx_identifier,
            "SPDXRef-DoapSource".to_string()
        );
    }
    #[test]
    fn ranges() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.snippet_information[0].ranges,
            vec![
                Range {
                    end_pointer: Pointer::Line {
                        line_number: 23,
                        reference: Some("SPDXRef-DoapSource".to_string()),
                    },
                    start_pointer: Pointer::Line {
                        line_number: 5,
                        reference: Some("SPDXRef-DoapSource".to_string()),
                    },
                },
                Range {
                    end_pointer: Pointer::Byte {
                        offset: 420,
                        reference: Some("SPDXRef-DoapSource".to_string()),
                    },
                    start_pointer: Pointer::Byte {
                        offset: 310,
                        reference: Some("SPDXRef-DoapSource".to_string()),
                    },
                },
            ]
        );
    }
    #[test]
    fn snippet_concluded_license() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.snippet_information[0]
                .snippet_concluded_license
                .as_ref()
                .unwrap()
                .clone(),
            SpdxExpression::parse("GPL-2.0-only").unwrap()
        );
    }
    #[test]
    fn license_information_in_snippet() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.snippet_information[0].license_information_in_snippet,
            vec!["GPL-2.0-only".to_string()]
        );
    }
    #[test]
    fn snippet_comments_on_license() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
                    spdx.snippet_information[0].snippet_comments_on_license,
                    Some("The concluded license was taken from package xyz, from which the snippet was copied into the current file. The concluded license information was found in the COPYING.txt file in package xyz.".to_string())
                );
    }
    #[test]
    fn snippet_copyright_text() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.snippet_information[0]
                .snippet_copyright_text
                .as_ref()
                .unwrap()
                .clone(),
            "Copyright 2008-2010 John Smith".to_string()
        );
    }
    #[test]
    fn snippet_comment() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
                    spdx.snippet_information[0].snippet_comment,
                    Some("This snippet was identified as significant and highlighted in this Apache-2.0 file, when a commercial scanner identified it as being derived from file foo.c in package xyz which is licensed under GPL-2.0.".to_string())
                );
    }
    #[test]
    fn snippet_name() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.snippet_information[0].snippet_name,
            Some("from linux kernel".to_string())
        );
    }
}