refactor TryFrom<Entry, ImmutableEntry>

feat/vaults
Tomáš Mládek 2022-02-07 18:25:17 +01:00
parent 5653b1b107
commit 0f2fa6836f
No known key found for this signature in database
GPG Key ID: ED21612889E75EC5
1 changed files with 15 additions and 26 deletions

View File

@ -66,22 +66,25 @@ impl TryFrom<&Entry> for models::Entry {
type Error = anyhow::Error;
fn try_from(e: &Entry) -> Result<Self, Self::Error> {
let base_entry = models::Entry {
identity: e.address()?.encode()?,
entity: e.entity.encode()?,
attribute: e.attribute.clone(),
value_str: None,
value_num: None,
immutable: false,
};
match e.value {
EntryValue::Number(n) => Ok(models::Entry {
identity: e.address()?.encode()?,
entity: e.entity.encode()?,
attribute: e.attribute.clone(),
value_str: None,
value_num: Some(n),
immutable: false,
..base_entry
}),
_ => Ok(models::Entry {
identity: e.address()?.encode()?,
entity: e.entity.encode()?,
attribute: e.attribute.clone(),
value_str: Some(e.value.to_string()?),
value_num: None,
immutable: false,
..base_entry
}),
}
}
@ -91,24 +94,10 @@ impl TryFrom<&ImmutableEntry> for models::Entry {
type Error = anyhow::Error;
fn try_from(e: &ImmutableEntry) -> Result<Self, Self::Error> {
match e.0.value {
EntryValue::Number(n) => Ok(models::Entry {
identity: e.0.address()?.encode()?,
entity: e.0.entity.encode()?,
attribute: e.0.attribute.clone(),
value_str: None,
value_num: Some(n),
immutable: false,
}),
_ => Ok(models::Entry {
identity: e.0.address()?.encode()?,
entity: e.0.entity.encode()?,
attribute: e.0.attribute.clone(),
value_str: Some(e.0.value.to_string()?),
value_num: None,
immutable: false,
}),
}
Ok(models::Entry {
immutable: true,
..models::Entry::try_from(&e.0)?
})
}
}