tackler_api/
metadata.rs

1/*
2 * Tackler-NG 2022
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Txn Set and Report metadata
7//!
8pub mod items;
9
10use items::MetadataItem;
11use items::MetadataItems;
12use items::Text;
13use jiff::tz::TimeZone;
14use serde::Serialize;
15
16/// Metadata of Inputs, Txn Set, Reporting parameters, etc.
17///
18#[derive(Serialize, Debug, Clone, Default)]
19pub struct Metadata {
20    // todo: fix pub access
21    #[doc(hidden)]
22    pub items: MetadataItems,
23}
24
25impl Metadata {
26    /// Get new empty `metadata`
27    #[must_use]
28    pub fn new() -> Metadata {
29        Metadata { items: Vec::new() }
30    }
31
32    /// Get new metadata with existing Metadata item
33    #[must_use]
34    pub fn from_mdi(mdi: MetadataItem) -> Metadata {
35        let items = vec![mdi];
36
37        Metadata { items }
38    }
39
40    /// Get new metadata from existing Metadata.
41    ///
42    /// If there is an existing [`TxnSetChecksum`](items::TxnSetChecksum) metadata item,
43    /// it will be removed from the new set.
44    #[must_use]
45    pub fn from_metadata(md: &Metadata) -> Metadata {
46        let mut metadata = Metadata::new();
47        for mdi in &md.items {
48            match mdi {
49                MetadataItem::TxnSetChecksum(_) => (),
50                _ => metadata.push(mdi.clone()),
51            }
52        }
53        metadata
54    }
55
56    /// Add metadata item into metadata
57    pub fn push(&mut self, mdi: MetadataItem) {
58        self.items.push(mdi);
59    }
60
61    /// Get textual representation of Metadata
62    #[must_use]
63    pub fn text(&self, tz: TimeZone) -> String {
64        let ts = self
65            .items
66            .iter()
67            .flat_map(|item| {
68                let mut vs = item.text(tz.clone());
69                // put a newline between metadata items
70                vs.push(String::default());
71                vs
72            })
73            .collect::<Vec<String>>();
74        ts.join("\n")
75    }
76
77    /// Test if metadata is empty
78    pub fn is_empty(&self) -> bool {
79        self.items.is_empty()
80    }
81}
82
83/// Generic checksum value
84#[derive(Serialize, Debug, Clone)]
85pub struct Checksum {
86    /// used hash algorithm
87    pub algorithm: String,
88    /// hexadecimal hash value
89    pub value: String,
90}