Skip to main content

daml_grpc/data/command/
create.rs

1use std::convert::TryFrom;
2
3use crate::data::identifier::DamlIdentifier;
4use crate::data::value::DamlRecord;
5use crate::data::{DamlError, DamlResult};
6use crate::grpc_protobuf::com::daml::ledger::api::v2::CreateCommand;
7use crate::grpc_protobuf::com::daml::ledger::api::v2::command::Command;
8use crate::util::Required;
9
10/// Create a new contract instance based on a template.
11#[derive(Debug, Eq, PartialEq, Clone)]
12pub struct DamlCreateCommand {
13    template_id: DamlIdentifier,
14    create_arguments: DamlRecord,
15}
16
17/// Create a new contract instance based on a template.
18impl DamlCreateCommand {
19    pub fn new(template_id: impl Into<DamlIdentifier>, create_arguments: impl Into<DamlRecord>) -> Self {
20        Self {
21            template_id: template_id.into(),
22            create_arguments: create_arguments.into(),
23        }
24    }
25
26    /// The template of contract the client wants to create.
27    pub const fn template_id(&self) -> &DamlIdentifier {
28        &self.template_id
29    }
30
31    /// The arguments required for creating a contract from this template.
32    pub const fn create_arguments(&self) -> &DamlRecord {
33        &self.create_arguments
34    }
35}
36
37impl From<DamlCreateCommand> for Command {
38    fn from(daml_create_command: DamlCreateCommand) -> Self {
39        Command::Create(CreateCommand {
40            template_id: Some(daml_create_command.template_id.into()),
41            create_arguments: Some(daml_create_command.create_arguments.into()),
42        })
43    }
44}
45
46impl TryFrom<CreateCommand> for DamlCreateCommand {
47    type Error = DamlError;
48
49    fn try_from(c: CreateCommand) -> DamlResult<Self> {
50        Ok(Self::new(DamlIdentifier::from(c.template_id.req()?), DamlRecord::try_from(c.create_arguments.req()?)?))
51    }
52}