Skip to main content

daml_grpc/data/command/
exercise_by_key.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::ExerciseByKeyCommand;
7use crate::grpc_protobuf::com::daml::ledger::api::v2::command::Command;
8use crate::util::Required;
9
10/// Exercise a choice on an existing contract specified by its key.
11#[derive(Debug, Eq, PartialEq, Clone)]
12pub struct DamlExerciseByKeyCommand {
13    template_id: DamlIdentifier,
14    contract_key: DamlValue,
15    choice: String,
16    choice_argument: DamlValue,
17}
18
19impl DamlExerciseByKeyCommand {
20    pub fn new(
21        template_id: impl Into<DamlIdentifier>,
22        contract_key: impl Into<DamlValue>,
23        choice: impl Into<String>,
24        choice_argument: impl Into<DamlValue>,
25    ) -> Self {
26        Self {
27            template_id: template_id.into(),
28            contract_key: contract_key.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 key of the contract the client wants to exercise upon.
40    pub const fn contract_key(&self) -> &DamlValue {
41        &self.contract_key
42    }
43
44    /// The name of the choice the client wants to exercise.
45    ///
46    /// Must match the regexp `[A-Za-z\$_][A-Za-z0-9\$_]*`
47    pub fn choice(&self) -> &str {
48        &self.choice
49    }
50
51    /// The argument for this choice.
52    pub const fn choice_argument(&self) -> &DamlValue {
53        &self.choice_argument
54    }
55}
56
57impl From<DamlExerciseByKeyCommand> for Command {
58    fn from(daml_exercise_command: DamlExerciseByKeyCommand) -> Self {
59        Command::ExerciseByKey(ExerciseByKeyCommand {
60            template_id: Some(daml_exercise_command.template_id.into()),
61            contract_key: Some(daml_exercise_command.contract_key.into()),
62            choice: daml_exercise_command.choice,
63            choice_argument: Some(daml_exercise_command.choice_argument.into()),
64        })
65    }
66}
67
68impl TryFrom<ExerciseByKeyCommand> for DamlExerciseByKeyCommand {
69    type Error = DamlError;
70
71    fn try_from(c: ExerciseByKeyCommand) -> DamlResult<Self> {
72        Ok(Self::new(
73            DamlIdentifier::from(c.template_id.req()?),
74            DamlValue::try_from(c.contract_key.req()?)?,
75            c.choice,
76            DamlValue::try_from(c.choice_argument.req()?)?,
77        ))
78    }
79}