use crate::data::DamlError;
use crate::data::value::DamlValue;
use crate::grpc_protobuf::com::daml::ledger::api::v2::{RecordField, Value};
use crate::util::Required;
use std::convert::TryFrom;
use std::ops::Not;
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd)]
pub struct DamlRecordField {
label: Option<String>,
value: DamlValue,
}
impl DamlRecordField {
pub fn new(label: Option<impl Into<String>>, value: impl Into<DamlValue>) -> Self {
Self {
label: label.map(Into::into),
value: value.into(),
}
}
pub const fn label(&self) -> &Option<String> {
&self.label
}
pub const fn value(&self) -> &DamlValue {
&self.value
}
pub fn into_value(self) -> DamlValue {
self.value
}
}
impl TryFrom<RecordField> for DamlRecordField {
type Error = DamlError;
fn try_from(field: RecordField) -> Result<Self, Self::Error> {
let label = field.label;
let value: DamlValue = field.value.req().and_then(DamlValue::try_from)?;
Ok(Self::new(label.is_empty().not().then_some(label), value))
}
}
impl From<DamlRecordField> for RecordField {
fn from(daml_record_field: DamlRecordField) -> Self {
Self {
label: daml_record_field.label.unwrap_or_default(),
value: Some(Value::from(daml_record_field.value)),
}
}
}