use std::convert::TryFrom;
use crate::data::identifier::DamlIdentifier;
use crate::data::value::{DamlRecord, DamlValue};
use crate::data::{DamlError, DamlResult};
use crate::grpc_protobuf::com::daml::ledger::api::v2::CreateAndExerciseCommand;
use crate::grpc_protobuf::com::daml::ledger::api::v2::command::Command;
use crate::util::Required;
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct DamlCreateAndExerciseCommand {
template_id: DamlIdentifier,
create_arguments: DamlRecord,
choice: String,
choice_argument: DamlValue,
}
impl DamlCreateAndExerciseCommand {
pub fn new(
template_id: impl Into<DamlIdentifier>,
create_arguments: impl Into<DamlRecord>,
choice: impl Into<String>,
choice_argument: impl Into<DamlValue>,
) -> Self {
Self {
template_id: template_id.into(),
create_arguments: create_arguments.into(),
choice: choice.into(),
choice_argument: choice_argument.into(),
}
}
pub const fn template_id(&self) -> &DamlIdentifier {
&self.template_id
}
pub const fn create_arguments(&self) -> &DamlRecord {
&self.create_arguments
}
pub fn choice(&self) -> &str {
&self.choice
}
pub const fn choice_argument(&self) -> &DamlValue {
&self.choice_argument
}
}
impl From<DamlCreateAndExerciseCommand> for Command {
fn from(daml_create_and_exercise_command: DamlCreateAndExerciseCommand) -> Self {
Command::CreateAndExercise(CreateAndExerciseCommand {
template_id: Some(daml_create_and_exercise_command.template_id.into()),
create_arguments: Some(daml_create_and_exercise_command.create_arguments.into()),
choice: daml_create_and_exercise_command.choice,
choice_argument: Some(daml_create_and_exercise_command.choice_argument.into()),
})
}
}
impl TryFrom<CreateAndExerciseCommand> for DamlCreateAndExerciseCommand {
type Error = DamlError;
fn try_from(c: CreateAndExerciseCommand) -> DamlResult<Self> {
Ok(Self::new(
DamlIdentifier::from(c.template_id.req()?),
DamlRecord::try_from(c.create_arguments.req()?)?,
c.choice,
DamlValue::try_from(c.choice_argument.req()?)?,
))
}
}