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::ExerciseCommand;
use crate::grpc_protobuf::com::daml::ledger::api::v2::command::Command;
use crate::util::Required;
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct DamlExerciseCommand {
template_id: DamlIdentifier,
contract_id: String,
choice: String,
choice_argument: DamlValue,
}
impl DamlExerciseCommand {
pub fn new(
template_id: impl Into<DamlIdentifier>,
contract_id: impl Into<String>,
choice: impl Into<String>,
choice_argument: impl Into<DamlValue>,
) -> Self {
Self {
template_id: template_id.into(),
contract_id: contract_id.into(),
choice: choice.into(),
choice_argument: choice_argument.into(),
}
}
pub const fn template_id(&self) -> &DamlIdentifier {
&self.template_id
}
pub fn contract_id(&self) -> &str {
&self.contract_id
}
pub fn choice(&self) -> &str {
&self.choice
}
pub const fn choice_argument(&self) -> &DamlValue {
&self.choice_argument
}
}
impl From<DamlExerciseCommand> for Command {
fn from(daml_exercise_command: DamlExerciseCommand) -> Self {
Command::Exercise(ExerciseCommand {
template_id: Some(daml_exercise_command.template_id.into()),
contract_id: daml_exercise_command.contract_id,
choice: daml_exercise_command.choice,
choice_argument: Some(daml_exercise_command.choice_argument.into()),
})
}
}
impl TryFrom<ExerciseCommand> for DamlExerciseCommand {
type Error = DamlError;
fn try_from(c: ExerciseCommand) -> DamlResult<Self> {
Ok(Self::new(
DamlIdentifier::from(c.template_id.req()?),
c.contract_id,
c.choice,
DamlValue::try_from(c.choice_argument.req()?)?,
))
}
}