Skip to main content

daml_grpc/data/command/
create_and_exercise.rs

1use std::convert::TryFrom;
2
3use crate::data::identifier::DamlIdentifier;
4use crate::data::value::{DamlRecord, DamlValue};
5use crate::data::{DamlError, DamlResult};
6use crate::grpc_protobuf::com::daml::ledger::api::v2::CreateAndExerciseCommand;
7use crate::grpc_protobuf::com::daml::ledger::api::v2::command::Command;
8use crate::util::Required;
9
10/// Create a contract and exercise a choice on it in the same transaction.
11#[derive(Debug, Eq, PartialEq, Clone)]
12pub struct DamlCreateAndExerciseCommand {
13    template_id: DamlIdentifier,
14    create_arguments: DamlRecord,
15    choice: String,
16    choice_argument: DamlValue,
17}
18
19impl DamlCreateAndExerciseCommand {
20    pub fn new(
21        template_id: impl Into<DamlIdentifier>,
22        create_arguments: impl Into<DamlRecord>,
23        choice: impl Into<String>,
24        choice_argument: impl Into<DamlValue>,
25    ) -> Self {
26        Self {
27            template_id: template_id.into(),
28            create_arguments: create_arguments.into(),
29            choice: choice.into(),
30            choice_argument: choice_argument.into(),
31        }
32    }
33
34    /// The template of the contract the client wants to create.
35    pub const fn template_id(&self) -> &DamlIdentifier {
36        &self.template_id
37    }
38
39    /// The arguments required for creating a contract from this template.
40    pub const fn create_arguments(&self) -> &DamlRecord {
41        &self.create_arguments
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<DamlCreateAndExerciseCommand> for Command {
58    fn from(daml_create_and_exercise_command: DamlCreateAndExerciseCommand) -> Self {
59        Command::CreateAndExercise(CreateAndExerciseCommand {
60            template_id: Some(daml_create_and_exercise_command.template_id.into()),
61            create_arguments: Some(daml_create_and_exercise_command.create_arguments.into()),
62            choice: daml_create_and_exercise_command.choice,
63            choice_argument: Some(daml_create_and_exercise_command.choice_argument.into()),
64        })
65    }
66}
67
68impl TryFrom<CreateAndExerciseCommand> for DamlCreateAndExerciseCommand {
69    type Error = DamlError;
70
71    fn try_from(c: CreateAndExerciseCommand) -> DamlResult<Self> {
72        Ok(Self::new(
73            DamlIdentifier::from(c.template_id.req()?),
74            DamlRecord::try_from(c.create_arguments.req()?)?,
75            c.choice,
76            DamlValue::try_from(c.choice_argument.req()?)?,
77        ))
78    }
79}