Skip to main content

daml_grpc/data/command/
exercise.rs

1use std::convert::TryFrom;
2
3use crate::data::identifier::DamlIdentifier;
4use crate::data::value::DamlValue;
5use crate::data::{DamlError, DamlResult};
6use crate::grpc_protobuf::com::daml::ledger::api::v2::ExerciseCommand;
7use crate::grpc_protobuf::com::daml::ledger::api::v2::command::Command;
8use crate::util::Required;
9
10/// Exercise a choice on an existing contract.
11#[derive(Debug, Eq, PartialEq, Clone)]
12pub struct DamlExerciseCommand {
13    template_id: DamlIdentifier,
14    contract_id: String,
15    choice: String,
16    choice_argument: DamlValue,
17}
18
19impl DamlExerciseCommand {
20    pub fn new(
21        template_id: impl Into<DamlIdentifier>,
22        contract_id: impl Into<String>,
23        choice: impl Into<String>,
24        choice_argument: impl Into<DamlValue>,
25    ) -> Self {
26        Self {
27            template_id: template_id.into(),
28            contract_id: contract_id.into(),
29            choice: choice.into(),
30            choice_argument: choice_argument.into(),
31        }
32    }
33
34    /// The template of contract the client wants to exercise.
35    pub const fn template_id(&self) -> &DamlIdentifier {
36        &self.template_id
37    }
38
39    /// The name of the choice the client wants to exercise.
40    ///
41    /// Must match the regexp ``[A-Za-z0-9#:\-_/ ]+``
42    pub fn contract_id(&self) -> &str {
43        &self.contract_id
44    }
45
46    /// The name of the choice the client wants to exercise.
47    ///
48    /// Must match the regexp `[A-Za-z\$_][A-Za-z0-9\$_]*`
49    pub fn choice(&self) -> &str {
50        &self.choice
51    }
52
53    /// The argument for this choice.
54    pub const fn choice_argument(&self) -> &DamlValue {
55        &self.choice_argument
56    }
57}
58
59impl From<DamlExerciseCommand> for Command {
60    fn from(daml_exercise_command: DamlExerciseCommand) -> Self {
61        Command::Exercise(ExerciseCommand {
62            template_id: Some(daml_exercise_command.template_id.into()),
63            contract_id: daml_exercise_command.contract_id,
64            choice: daml_exercise_command.choice,
65            choice_argument: Some(daml_exercise_command.choice_argument.into()),
66        })
67    }
68}
69
70impl TryFrom<ExerciseCommand> for DamlExerciseCommand {
71    type Error = DamlError;
72
73    fn try_from(c: ExerciseCommand) -> DamlResult<Self> {
74        Ok(Self::new(
75            DamlIdentifier::from(c.template_id.req()?),
76            c.contract_id,
77            c.choice,
78            DamlValue::try_from(c.choice_argument.req()?)?,
79        ))
80    }
81}