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