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