diff --git a/src/addressing.rs b/src/addressing.rs index 3943fc0..c42ac7c 100644 --- a/src/addressing.rs +++ b/src/addressing.rs @@ -43,9 +43,11 @@ impl Address { let (digest_len, rest) = unsigned_varint::decode::usize(rest)?; let digest = rest; if digest_len != digest.len() { - Err(anyhow!( - "Actual digest length does not match declared digest length." - )) + Err(anyhow!(format!( + "Actual digest length ({}) does not match declared digest length ({}).", + digest.len(), + digest_len + ))) } else { match hash_func_type { KANGAROO_TWELVE => Ok(Self::Hash(Hash(Vec::from(digest)))), diff --git a/src/database.rs b/src/database.rs index 7587760..e1941c0 100644 --- a/src/database.rs +++ b/src/database.rs @@ -37,13 +37,13 @@ pub enum EntryValue { Invalid, } -impl TryFrom for Entry { +impl TryFrom<&models::Entry> for Entry { type Error = anyhow::Error; - fn try_from(e: models::Entry) -> Result { + fn try_from(e: &models::Entry) -> Result { Ok(Entry { target: Address::decode(&e.target)?, - key: e.key, + key: e.key.clone(), value: e.value.parse().unwrap(), }) } @@ -182,7 +182,7 @@ pub fn retrieve_object>( .or_filter(value.eq(EntryValue::Address(object_address).to_string()?)) .load::(connection)?; let entries = matches - .into_iter() + .iter() .map(Entry::try_from) .filter_map(Result::ok) .collect(); @@ -207,7 +207,7 @@ pub fn bulk_retrieve_objects>( // .or_filter(value.eq(EntryValue::Address(object_address).to_str()?)) .load::(connection)?; let entries = matches - .into_iter() + .iter() .map(Entry::try_from) .filter_map(Result::ok) .collect(); @@ -350,7 +350,10 @@ impl Query { } } - fn parse_component(value: &lexpr::Value) -> Result> { + fn parse_component(value: &lexpr::Value) -> Result> + where + ::Err: std::fmt::Debug, + { match value { Cons(cons) => { if let Symbol(symbol) = cons.car() { @@ -361,10 +364,9 @@ impl Query { let args = split.1; let values: Result, _> = args.iter().map(|value| { if let lexpr::Value::String(str) = value { - if let Ok(value) = T::from_str(str.borrow()) { - Ok(value) - } else { - Err(anyhow!(format!("Malformed expression: Conversion of inner value '{}' from string failed.", str))) + match T::from_str(str.borrow()) { + Ok(value) => Ok(value), + Err(error) => Err(anyhow!(format!("Malformed expression: Conversion of inner value '{}' from string failed: {:#?}",str, error))), } } else { Err(anyhow!("Malformed expression: Inner value list must be comprised of strings.")) @@ -406,16 +408,13 @@ impl Query { ))) } } - lexpr::Value::String(str) => { - if let Ok(value) = T::from_str(str.borrow()) { - Ok(QueryComponent::Exact(value)) - } else { - Err(anyhow!(format!( - "Malformed expression: Conversion of inner value '{}' from string failed.", - str - ))) - } - } + lexpr::Value::String(str) => match T::from_str(str.borrow()) { + Ok(value) => Ok(QueryComponent::Exact(value)), + Err(error) => Err(anyhow!(format!( + "Malformed expression: Conversion of inner value '{}' from string failed: {:#?}", + str, error + ))), + }, lexpr::Value::Symbol(symbol) => match symbol.borrow() { "?" => Ok(QueryComponent::Any), _ => Err(anyhow!(format!( @@ -440,7 +439,7 @@ pub fn query>(connection: &C, query: Query) -> R let matches = db_query.load::(connection)?; let entries = matches - .into_iter() + .iter() .map(Entry::try_from) .filter_map(Result::ok) .collect(); @@ -585,7 +584,7 @@ pub fn query_entries>( let matches = query.load::(connection)?; let entries = matches - .into_iter() + .iter() .map(Entry::try_from) .filter_map(Result::ok) .collect(); @@ -596,7 +595,7 @@ pub fn query_entries>( pub fn insert_entry>( connection: &C, entry: Entry, -) -> Result { +) -> Result
{ debug!("Inserting: {}", entry); let insert_entry = models::Entry { @@ -606,9 +605,13 @@ pub fn insert_entry>( 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) - .execute(connection)?) + .execute(connection)?; + + Ok(Address::Hash(entry.hash()?)) } #[derive(Debug)] diff --git a/src/routes.rs b/src/routes.rs index c2279d1..127d973 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -87,9 +87,9 @@ pub async fn put_object( let entry = serde_json::from_slice::(&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}")]