daml_grpc/data/
commands.rs

1use crate::data::command::DamlCommand;
2use crate::data::{DamlError, DamlResult};
3use crate::grpc_protobuf::com::daml::ledger::api::v1::commands::DeduplicationPeriod;
4use crate::grpc_protobuf::com::daml::ledger::api::v1::{Command, Commands};
5use crate::util;
6use chrono::DateTime;
7use chrono::Utc;
8use std::convert::TryFrom;
9use std::time::Duration;
10
11/// A list of Daml commands.
12#[derive(Debug, Eq, PartialEq, Clone)]
13pub struct DamlCommands {
14    workflow_id: String,
15    application_id: String,
16    command_id: String,
17    submission_id: String,
18    party: String,
19    act_as: Vec<String>,
20    read_as: Vec<String>,
21    commands: Vec<DamlCommand>,
22    deduplication_period: Option<DamlCommandsDeduplicationPeriod>,
23    min_ledger_time: Option<DamlMinLedgerTime>,
24}
25
26impl DamlCommands {
27    #[allow(clippy::too_many_arguments)]
28    pub fn new(
29        workflow_id: impl Into<String>,
30        application_id: impl Into<String>,
31        command_id: impl Into<String>,
32        submission_id: impl Into<String>,
33        party: impl Into<String>,
34        act_as: impl Into<Vec<String>>,
35        read_as: impl Into<Vec<String>>,
36        commands: impl Into<Vec<DamlCommand>>,
37        deduplication_period: impl Into<Option<DamlCommandsDeduplicationPeriod>>,
38        min_ledger_time: impl Into<Option<DamlMinLedgerTime>>,
39    ) -> Self {
40        Self {
41            workflow_id: workflow_id.into(),
42            application_id: application_id.into(),
43            command_id: command_id.into(),
44            submission_id: submission_id.into(),
45            party: party.into(),
46            act_as: act_as.into(),
47            read_as: read_as.into(),
48            commands: commands.into(),
49            deduplication_period: deduplication_period.into(),
50            min_ledger_time: min_ledger_time.into(),
51        }
52    }
53
54    pub fn workflow_id(&self) -> &str {
55        &self.workflow_id
56    }
57
58    pub fn application_id(&self) -> &str {
59        &self.application_id
60    }
61
62    pub fn command_id(&self) -> &str {
63        &self.command_id
64    }
65
66    pub fn submission_id(&self) -> &str {
67        &self.submission_id
68    }
69
70    pub fn party(&self) -> &str {
71        &self.party
72    }
73
74    pub fn act_as(&self) -> &[String] {
75        &self.act_as
76    }
77
78    pub fn read_as(&self) -> &[String] {
79        &self.read_as
80    }
81
82    pub fn commands(&self) -> &[DamlCommand] {
83        &self.commands
84    }
85
86    pub const fn deduplication_period(&self) -> &Option<DamlCommandsDeduplicationPeriod> {
87        &self.deduplication_period
88    }
89
90    pub const fn min_ledger_time(&self) -> &Option<DamlMinLedgerTime> {
91        &self.min_ledger_time
92    }
93}
94
95impl TryFrom<DamlCommands> for Commands {
96    type Error = DamlError;
97
98    fn try_from(daml_commands: DamlCommands) -> DamlResult<Commands> {
99        Ok(Commands {
100            // To allow each `DamlCommands` to be reusable between ledgers The Daml ledger id is updated immediately
101            // prior to sending to the server.
102            ledger_id: String::new(),
103            workflow_id: daml_commands.workflow_id,
104            application_id: daml_commands.application_id,
105            command_id: daml_commands.command_id,
106            party: daml_commands.party,
107            act_as: daml_commands.act_as,
108            read_as: daml_commands.read_as,
109            submission_id: daml_commands.submission_id,
110            commands: daml_commands.commands.into_iter().map(Command::from).collect(),
111            min_ledger_time_abs: match daml_commands.min_ledger_time {
112                Some(DamlMinLedgerTime::Absolute(timestamp)) => Some(util::to_grpc_timestamp(timestamp)?),
113                _ => None,
114            },
115            min_ledger_time_rel: match daml_commands.min_ledger_time {
116                Some(DamlMinLedgerTime::Relative(duration)) => Some(util::to_grpc_duration(&duration)?),
117                _ => None,
118            },
119            deduplication_period: daml_commands.deduplication_period.map(DeduplicationPeriod::try_from).transpose()?,
120        })
121    }
122}
123
124#[derive(Debug, Eq, PartialEq, Clone)]
125pub enum DamlMinLedgerTime {
126    Absolute(DateTime<Utc>),
127    Relative(Duration),
128}
129
130#[derive(Debug, Eq, PartialEq, Clone)]
131pub enum DamlCommandsDeduplicationPeriod {
132    DeduplicationOffset(String),
133    DeduplicationDuration(Duration),
134}
135
136impl TryFrom<DamlCommandsDeduplicationPeriod> for DeduplicationPeriod {
137    type Error = DamlError;
138
139    fn try_from(deduplication_period: DamlCommandsDeduplicationPeriod) -> DamlResult<Self> {
140        Ok(match deduplication_period {
141            DamlCommandsDeduplicationPeriod::DeduplicationOffset(offset) =>
142                DeduplicationPeriod::DeduplicationOffset(offset),
143            DamlCommandsDeduplicationPeriod::DeduplicationDuration(duration) =>
144                DeduplicationPeriod::DeduplicationDuration(util::to_grpc_duration(&duration)?),
145        })
146    }
147}