Skip to main content

provenant/output_schema/
tallies.rs

1use serde::{Deserialize, Serialize};
2
3use super::tally_entry::OutputTallyEntry;
4
5#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default)]
6pub struct OutputTallies {
7    #[serde(default, skip_serializing_if = "Vec::is_empty")]
8    pub detected_license_expression: Vec<OutputTallyEntry>,
9    #[serde(default, skip_serializing_if = "Vec::is_empty")]
10    pub copyrights: Vec<OutputTallyEntry>,
11    #[serde(default, skip_serializing_if = "Vec::is_empty")]
12    pub holders: Vec<OutputTallyEntry>,
13    #[serde(default, skip_serializing_if = "Vec::is_empty")]
14    pub authors: Vec<OutputTallyEntry>,
15    #[serde(default, skip_serializing_if = "Vec::is_empty")]
16    pub programming_language: Vec<OutputTallyEntry>,
17}
18
19impl From<&crate::models::Tallies> for OutputTallies {
20    fn from(value: &crate::models::Tallies) -> Self {
21        Self {
22            detected_license_expression: value
23                .detected_license_expression
24                .iter()
25                .map(OutputTallyEntry::from)
26                .collect(),
27            copyrights: value
28                .copyrights
29                .iter()
30                .map(OutputTallyEntry::from)
31                .collect(),
32            holders: value.holders.iter().map(OutputTallyEntry::from).collect(),
33            authors: value.authors.iter().map(OutputTallyEntry::from).collect(),
34            programming_language: value
35                .programming_language
36                .iter()
37                .map(OutputTallyEntry::from)
38                .collect(),
39        }
40    }
41}
42
43impl TryFrom<&OutputTallies> for crate::models::Tallies {
44    type Error = String;
45    fn try_from(value: &OutputTallies) -> Result<Self, Self::Error> {
46        let mut detected_license_expression =
47            Vec::with_capacity(value.detected_license_expression.len());
48        for e in &value.detected_license_expression {
49            detected_license_expression.push(crate::models::TallyEntry::from(e));
50        }
51        let mut copyrights = Vec::with_capacity(value.copyrights.len());
52        for e in &value.copyrights {
53            copyrights.push(crate::models::TallyEntry::from(e));
54        }
55        let mut holders = Vec::with_capacity(value.holders.len());
56        for e in &value.holders {
57            holders.push(crate::models::TallyEntry::from(e));
58        }
59        let mut authors = Vec::with_capacity(value.authors.len());
60        for e in &value.authors {
61            authors.push(crate::models::TallyEntry::from(e));
62        }
63        let mut programming_language = Vec::with_capacity(value.programming_language.len());
64        for e in &value.programming_language {
65            programming_language.push(crate::models::TallyEntry::from(e));
66        }
67        Ok(Self {
68            detected_license_expression,
69            copyrights,
70            holders,
71            authors,
72            programming_language,
73        })
74    }
75}