Skip to main content

daml_grpc/
command_factory.rs

1use uuid::Uuid;
2
3use crate::data::command::DamlCommand;
4use crate::data::{DamlCommands, DamlCommandsDeduplicationPeriod, DamlMinLedgerTime};
5
6/// Factory for assembling [`DamlCommands`] payloads from a fixed set of
7/// submission parameters (workflow id, user id, act-as parties, dedup
8/// period, …) and a variable list of [`DamlCommand`]s.
9///
10/// v2 dropped the single-party `party` field; submissions act on behalf
11/// of the `act_as` set instead.
12#[derive(Debug)]
13pub struct DamlCommandFactory {
14    workflow_id: String,
15    user_id: String,
16    act_as: Vec<String>,
17    read_as: Vec<String>,
18    deduplication_period: Option<DamlCommandsDeduplicationPeriod>,
19    min_ledger_time: Option<DamlMinLedgerTime>,
20}
21
22impl DamlCommandFactory {
23    pub fn new(
24        workflow_id: impl Into<String>,
25        user_id: impl Into<String>,
26        act_as: impl Into<Vec<String>>,
27        read_as: impl Into<Vec<String>>,
28        deduplication_period: impl Into<Option<DamlCommandsDeduplicationPeriod>>,
29        min_ledger_time: impl Into<Option<DamlMinLedgerTime>>,
30    ) -> Self {
31        Self {
32            workflow_id: workflow_id.into(),
33            user_id: user_id.into(),
34            act_as: act_as.into(),
35            read_as: read_as.into(),
36            deduplication_period: deduplication_period.into(),
37            min_ledger_time: min_ledger_time.into(),
38        }
39    }
40
41    pub fn workflow_id(&self) -> &str {
42        &self.workflow_id
43    }
44
45    pub fn user_id(&self) -> &str {
46        &self.user_id
47    }
48
49    pub fn act_as(&self) -> &[String] {
50        &self.act_as
51    }
52
53    pub fn read_as(&self) -> &[String] {
54        &self.read_as
55    }
56
57    pub const fn deduplication_period(&self) -> &Option<DamlCommandsDeduplicationPeriod> {
58        &self.deduplication_period
59    }
60
61    pub const fn min_ledger_time(&self) -> &Option<DamlMinLedgerTime> {
62        &self.min_ledger_time
63    }
64
65    pub fn make_command(&self, command: DamlCommand) -> DamlCommands {
66        self.make_commands::<String, _>(vec![command], None)
67    }
68
69    pub fn make_command_with_id(&self, command: DamlCommand, command_id: impl Into<String>) -> DamlCommands {
70        self.make_commands(vec![command], Some(command_id))
71    }
72
73    pub fn make_commands<S, V>(&self, commands: V, command_id: Option<S>) -> DamlCommands
74    where
75        S: Into<String>,
76        V: Into<Vec<DamlCommand>>,
77    {
78        DamlCommands {
79            workflow_id: self.workflow_id.clone(),
80            read_as: self.read_as.clone(),
81            deduplication_period: self.deduplication_period.clone(),
82            min_ledger_time: self.min_ledger_time.clone(),
83            ..DamlCommands::new(
84                self.user_id.clone(),
85                command_id.map_or_else(|| Uuid::new_v4().to_string(), Into::into),
86                self.act_as.clone(),
87                commands,
88            )
89        }
90    }
91}