attempt (?) to return hash from insert_entry, improve error logging

feat/vaults
Tomáš Mládek 2021-02-19 22:18:31 +01:00
parent 3ad31a482d
commit 3fb436708b
3 changed files with 35 additions and 30 deletions

View File

@ -43,9 +43,11 @@ impl Address {
let (digest_len, rest) = unsigned_varint::decode::usize(rest)?; let (digest_len, rest) = unsigned_varint::decode::usize(rest)?;
let digest = rest; let digest = rest;
if digest_len != digest.len() { if digest_len != digest.len() {
Err(anyhow!( Err(anyhow!(format!(
"Actual digest length does not match declared digest length." "Actual digest length ({}) does not match declared digest length ({}).",
)) digest.len(),
digest_len
)))
} else { } else {
match hash_func_type { match hash_func_type {
KANGAROO_TWELVE => Ok(Self::Hash(Hash(Vec::from(digest)))), KANGAROO_TWELVE => Ok(Self::Hash(Hash(Vec::from(digest)))),

View File

@ -37,13 +37,13 @@ pub enum EntryValue {
Invalid, Invalid,
} }
impl TryFrom<models::Entry> for Entry { impl TryFrom<&models::Entry> for Entry {
type Error = anyhow::Error; type Error = anyhow::Error;
fn try_from(e: models::Entry) -> Result<Self, Self::Error> { fn try_from(e: &models::Entry) -> Result<Self, Self::Error> {
Ok(Entry { Ok(Entry {
target: Address::decode(&e.target)?, target: Address::decode(&e.target)?,
key: e.key, key: e.key.clone(),
value: e.value.parse().unwrap(), value: e.value.parse().unwrap(),
}) })
} }
@ -182,7 +182,7 @@ pub fn retrieve_object<C: Connection<Backend = Sqlite>>(
.or_filter(value.eq(EntryValue::Address(object_address).to_string()?)) .or_filter(value.eq(EntryValue::Address(object_address).to_string()?))
.load::<models::Entry>(connection)?; .load::<models::Entry>(connection)?;
let entries = matches let entries = matches
.into_iter() .iter()
.map(Entry::try_from) .map(Entry::try_from)
.filter_map(Result::ok) .filter_map(Result::ok)
.collect(); .collect();
@ -207,7 +207,7 @@ pub fn bulk_retrieve_objects<C: Connection<Backend = Sqlite>>(
// .or_filter(value.eq(EntryValue::Address(object_address).to_str()?)) // .or_filter(value.eq(EntryValue::Address(object_address).to_str()?))
.load::<models::Entry>(connection)?; .load::<models::Entry>(connection)?;
let entries = matches let entries = matches
.into_iter() .iter()
.map(Entry::try_from) .map(Entry::try_from)
.filter_map(Result::ok) .filter_map(Result::ok)
.collect(); .collect();
@ -350,7 +350,10 @@ impl Query {
} }
} }
fn parse_component<T: FromStr>(value: &lexpr::Value) -> Result<QueryComponent<T>> { fn parse_component<T: FromStr>(value: &lexpr::Value) -> Result<QueryComponent<T>>
where
<T as FromStr>::Err: std::fmt::Debug,
{
match value { match value {
Cons(cons) => { Cons(cons) => {
if let Symbol(symbol) = cons.car() { if let Symbol(symbol) = cons.car() {
@ -361,10 +364,9 @@ impl Query {
let args = split.1; let args = split.1;
let values: Result<Vec<T>, _> = args.iter().map(|value| { let values: Result<Vec<T>, _> = args.iter().map(|value| {
if let lexpr::Value::String(str) = value { if let lexpr::Value::String(str) = value {
if let Ok(value) = T::from_str(str.borrow()) { match T::from_str(str.borrow()) {
Ok(value) Ok(value) => Ok(value),
} else { Err(error) => Err(anyhow!(format!("Malformed expression: Conversion of inner value '{}' from string failed: {:#?}",str, error))),
Err(anyhow!(format!("Malformed expression: Conversion of inner value '{}' from string failed.", str)))
} }
} else { } else {
Err(anyhow!("Malformed expression: Inner value list must be comprised of strings.")) Err(anyhow!("Malformed expression: Inner value list must be comprised of strings."))
@ -406,16 +408,13 @@ impl Query {
))) )))
} }
} }
lexpr::Value::String(str) => { lexpr::Value::String(str) => match T::from_str(str.borrow()) {
if let Ok(value) = T::from_str(str.borrow()) { Ok(value) => Ok(QueryComponent::Exact(value)),
Ok(QueryComponent::Exact(value)) Err(error) => Err(anyhow!(format!(
} else { "Malformed expression: Conversion of inner value '{}' from string failed: {:#?}",
Err(anyhow!(format!( str, error
"Malformed expression: Conversion of inner value '{}' from string failed.", ))),
str },
)))
}
}
lexpr::Value::Symbol(symbol) => match symbol.borrow() { lexpr::Value::Symbol(symbol) => match symbol.borrow() {
"?" => Ok(QueryComponent::Any), "?" => Ok(QueryComponent::Any),
_ => Err(anyhow!(format!( _ => Err(anyhow!(format!(
@ -440,7 +439,7 @@ pub fn query<C: Connection<Backend = Sqlite>>(connection: &C, query: Query) -> R
let matches = db_query.load::<models::Entry>(connection)?; let matches = db_query.load::<models::Entry>(connection)?;
let entries = matches let entries = matches
.into_iter() .iter()
.map(Entry::try_from) .map(Entry::try_from)
.filter_map(Result::ok) .filter_map(Result::ok)
.collect(); .collect();
@ -585,7 +584,7 @@ pub fn query_entries<C: Connection<Backend = Sqlite>>(
let matches = query.load::<models::Entry>(connection)?; let matches = query.load::<models::Entry>(connection)?;
let entries = matches let entries = matches
.into_iter() .iter()
.map(Entry::try_from) .map(Entry::try_from)
.filter_map(Result::ok) .filter_map(Result::ok)
.collect(); .collect();
@ -596,7 +595,7 @@ pub fn query_entries<C: Connection<Backend = Sqlite>>(
pub fn insert_entry<C: Connection<Backend = Sqlite>>( pub fn insert_entry<C: Connection<Backend = Sqlite>>(
connection: &C, connection: &C,
entry: Entry, entry: Entry,
) -> Result<usize> { ) -> Result<Address> {
debug!("Inserting: {}", entry); debug!("Inserting: {}", entry);
let insert_entry = models::Entry { let insert_entry = models::Entry {
@ -606,9 +605,13 @@ pub fn insert_entry<C: Connection<Backend = Sqlite>>(
value: entry.value.to_string()?, value: entry.value.to_string()?,
}; };
Ok(diesel::insert_into(data::table) let entry = Entry::try_from(&insert_entry)?;
diesel::insert_into(data::table)
.values(insert_entry) .values(insert_entry)
.execute(connection)?) .execute(connection)?;
Ok(Address::Hash(entry.hash()?))
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -87,9 +87,9 @@ pub async fn put_object(
let entry = serde_json::from_slice::<Entry>(&body)?; let entry = serde_json::from_slice::<Entry>(&body)?;
insert_entry(&connection, entry).map_err(ErrorInternalServerError)?; let result_address = insert_entry(&connection, entry).map_err(ErrorInternalServerError)?;
Ok(HttpResponse::Ok().finish()) Ok(HttpResponse::Ok().json(result_address))
} }
#[delete("/api/obj/{address_str}")] #[delete("/api/obj/{address_str}")]