attempt (?) to return hash from insert_entry, improve error logging
This commit is contained in:
parent
3ad31a482d
commit
3fb436708b
3 changed files with 35 additions and 30 deletions
|
@ -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)))),
|
||||
|
|
|
@ -37,13 +37,13 @@ pub enum EntryValue {
|
|||
Invalid,
|
||||
}
|
||||
|
||||
impl TryFrom<models::Entry> for Entry {
|
||||
impl TryFrom<&models::Entry> for Entry {
|
||||
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 {
|
||||
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<C: Connection<Backend = Sqlite>>(
|
|||
.or_filter(value.eq(EntryValue::Address(object_address).to_string()?))
|
||||
.load::<models::Entry>(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<C: Connection<Backend = Sqlite>>(
|
|||
// .or_filter(value.eq(EntryValue::Address(object_address).to_str()?))
|
||||
.load::<models::Entry>(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<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 {
|
||||
Cons(cons) => {
|
||||
if let Symbol(symbol) = cons.car() {
|
||||
|
@ -361,10 +364,9 @@ impl Query {
|
|||
let args = split.1;
|
||||
let values: Result<Vec<T>, _> = 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<C: Connection<Backend = Sqlite>>(connection: &C, query: Query) -> R
|
|||
let matches = db_query.load::<models::Entry>(connection)?;
|
||||
|
||||
let entries = matches
|
||||
.into_iter()
|
||||
.iter()
|
||||
.map(Entry::try_from)
|
||||
.filter_map(Result::ok)
|
||||
.collect();
|
||||
|
@ -585,7 +584,7 @@ pub fn query_entries<C: Connection<Backend = Sqlite>>(
|
|||
let matches = query.load::<models::Entry>(connection)?;
|
||||
|
||||
let entries = matches
|
||||
.into_iter()
|
||||
.iter()
|
||||
.map(Entry::try_from)
|
||||
.filter_map(Result::ok)
|
||||
.collect();
|
||||
|
@ -596,7 +595,7 @@ pub fn query_entries<C: Connection<Backend = Sqlite>>(
|
|||
pub fn insert_entry<C: Connection<Backend = Sqlite>>(
|
||||
connection: &C,
|
||||
entry: Entry,
|
||||
) -> Result<usize> {
|
||||
) -> Result<Address> {
|
||||
debug!("Inserting: {}", entry);
|
||||
|
||||
let insert_entry = models::Entry {
|
||||
|
@ -606,9 +605,13 @@ pub fn insert_entry<C: Connection<Backend = Sqlite>>(
|
|||
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)]
|
||||
|
|
|
@ -87,9 +87,9 @@ pub async fn put_object(
|
|||
|
||||
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}")]
|
||||
|
|
Loading…
Reference in a new issue