Skip to main content

provenant/output_schema/
top_level_license_detection.rs

1// SPDX-FileCopyrightText: Provenant contributors
2// SPDX-License-Identifier: Apache-2.0
3
4use serde::{Deserialize, Serialize};
5
6use super::license_match::OutputMatch;
7
8#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
9pub struct OutputTopLevelLicenseDetection {
10    pub identifier: String,
11    pub license_expression: String,
12    pub license_expression_spdx: String,
13    pub detection_count: usize,
14    #[serde(skip_serializing_if = "Vec::is_empty", default)]
15    pub detection_log: Vec<String>,
16    pub reference_matches: Vec<OutputMatch>,
17}
18
19impl From<&crate::models::TopLevelLicenseDetection> for OutputTopLevelLicenseDetection {
20    fn from(value: &crate::models::TopLevelLicenseDetection) -> Self {
21        Self {
22            identifier: value.identifier.clone(),
23            license_expression: value.license_expression.clone(),
24            license_expression_spdx: value.license_expression_spdx.clone(),
25            detection_count: value.detection_count,
26            detection_log: value.detection_log.clone(),
27            reference_matches: value
28                .reference_matches
29                .iter()
30                .map(OutputMatch::from)
31                .collect(),
32        }
33    }
34}
35
36impl TryFrom<&OutputTopLevelLicenseDetection> for crate::models::TopLevelLicenseDetection {
37    type Error = String;
38    fn try_from(value: &OutputTopLevelLicenseDetection) -> Result<Self, Self::Error> {
39        let mut reference_matches = Vec::with_capacity(value.reference_matches.len());
40        for m in &value.reference_matches {
41            reference_matches.push(crate::models::Match::try_from(m)?);
42        }
43        Ok(Self {
44            identifier: value.identifier.clone(),
45            license_expression: value.license_expression.clone(),
46            license_expression_spdx: value.license_expression_spdx.clone(),
47            detection_count: value.detection_count,
48            detection_log: value.detection_log.clone(),
49            reference_matches,
50        })
51    }
52}