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    pub identifier: Option<String>,
13}
14
15impl From<&crate::models::LicenseDetection> for OutputLicenseDetection {
16    fn from(value: &crate::models::LicenseDetection) -> Self {
17        Self {
18            license_expression: value.license_expression.clone(),
19            license_expression_spdx: value.license_expression_spdx.clone(),
20            matches: value.matches.iter().map(OutputMatch::from).collect(),
21            detection_log: value.detection_log.clone(),
22            identifier: value.identifier.clone(),
23        }
24    }
25}
26
27impl TryFrom<&OutputLicenseDetection> for crate::models::LicenseDetection {
28    type Error = String;
29    fn try_from(value: &OutputLicenseDetection) -> Result<Self, Self::Error> {
30        let mut matches = Vec::with_capacity(value.matches.len());
31        for m in &value.matches {
32            matches.push(crate::models::Match::try_from(m)?);
33        }
34        Ok(Self {
35            license_expression: value.license_expression.clone(),
36            license_expression_spdx: value.license_expression_spdx.clone(),
37            matches,
38            detection_log: value.detection_log.clone(),
39            identifier: value.identifier.clone(),
40        })
41    }
42}