daml_grpc/data/
transaction_tree.rs

1use crate::data::event::DamlTreeEvent;
2
3use crate::data::{DamlError, DamlResult};
4use crate::grpc_protobuf::com::daml::ledger::api::v1::TransactionTree;
5use crate::util;
6use crate::util::Required;
7use chrono::DateTime;
8use chrono::Utc;
9use std::collections::HashMap;
10use std::convert::TryFrom;
11
12/// A Daml ledger transaction tree.
13#[derive(Debug, Eq, PartialEq, Clone)]
14pub struct DamlTransactionTree {
15    transaction_id: String,
16    command_id: String,
17    workflow_id: String,
18    effective_at: DateTime<Utc>,
19    offset: String,
20    events_by_id: HashMap<String, DamlTreeEvent>,
21    root_event_ids: Vec<String>,
22}
23
24impl DamlTransactionTree {
25    #[allow(clippy::too_many_arguments)]
26    pub fn new(
27        transaction_id: impl Into<String>,
28        command_id: impl Into<String>,
29        workflow_id: impl Into<String>,
30        effective_at: impl Into<DateTime<Utc>>,
31        offset: impl Into<String>,
32        events_by_id: impl Into<HashMap<String, DamlTreeEvent>>,
33        root_event_ids: impl Into<Vec<String>>,
34    ) -> Self {
35        Self {
36            transaction_id: transaction_id.into(),
37            command_id: command_id.into(),
38            workflow_id: workflow_id.into(),
39            effective_at: effective_at.into(),
40            offset: offset.into(),
41            events_by_id: events_by_id.into(),
42            root_event_ids: root_event_ids.into(),
43        }
44    }
45
46    pub fn transaction_id(&self) -> &str {
47        &self.transaction_id
48    }
49
50    pub fn command_id(&self) -> &str {
51        &self.command_id
52    }
53
54    pub fn workflow_id(&self) -> &str {
55        &self.workflow_id
56    }
57
58    pub const fn effective_at(&self) -> &DateTime<Utc> {
59        &self.effective_at
60    }
61
62    pub const fn events_by_id(&self) -> &HashMap<String, DamlTreeEvent> {
63        &self.events_by_id
64    }
65
66    pub fn take_events_by_id(self) -> HashMap<String, DamlTreeEvent> {
67        self.events_by_id
68    }
69
70    pub fn root_event_ids(&self) -> &[String] {
71        &self.root_event_ids
72    }
73
74    pub fn offset(&self) -> &str {
75        &self.offset
76    }
77}
78
79impl TryFrom<TransactionTree> for DamlTransactionTree {
80    type Error = DamlError;
81
82    fn try_from(tx: TransactionTree) -> Result<Self, Self::Error> {
83        let events_by_id = tx
84            .events_by_id
85            .into_iter()
86            .map(|(id, event)| match DamlTreeEvent::try_from(event) {
87                Ok(m) => Ok((id, m)),
88                Err(e) => Err(e),
89            })
90            .collect::<DamlResult<HashMap<_, _>>>()?;
91        Ok(Self::new(
92            tx.transaction_id,
93            tx.command_id,
94            tx.workflow_id,
95            util::from_grpc_timestamp(&tx.effective_at.req()?),
96            tx.offset,
97            events_by_id,
98            tx.root_event_ids,
99        ))
100    }
101}