Skip to main content

provenant/output_schema/
license_detection.rs

1use serde::{Deserialize, Serialize};
2
3use super::license_match::OutputMatch;
4
5#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
6pub struct OutputLicenseDetection {
7    pub license_expression: String,
8    pub license_expression_spdx: String,
9    pub matches: Vec<OutputMatch>,
10    #[serde(skip_serializing_if = "Vec::is_empty", default)]
11    pub detection_log: Vec<String>,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub identifier: Option<String>,
14}
15
16impl From<&crate::models::LicenseDetection> for OutputLicenseDetection {
17    fn from(value: &crate::models::LicenseDetection) -> Self {
18        Self {
19            license_expression: value.license_expression.clone(),
20            license_expression_spdx: value.license_expression_spdx.clone(),
21            matches: value.matches.iter().map(OutputMatch::from).collect(),
22            detection_log: value.detection_log.clone(),
23            identifier: value.identifier.clone(),
24        }
25    }
26}
27
28impl TryFrom<&OutputLicenseDetection> for crate::models::LicenseDetection {
29    type Error = String;
30    fn try_from(value: &OutputLicenseDetection) -> Result<Self, Self::Error> {
31        let mut matches = Vec::with_capacity(value.matches.len());
32        for m in &value.matches {
33            matches.push(crate::models::Match::try_from(m)?);
34        }
35        Ok(Self {
36            license_expression: value.license_expression.clone(),
37            license_expression_spdx: value.license_expression_spdx.clone(),
38            matches,
39            detection_log: value.detection_log.clone(),
40            identifier: value.identifier.clone(),
41        })
42    }
43}