From d50565386640c7d822ddd45734062a8cc0f9c7d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Sun, 13 Sep 2020 16:30:01 +0200 Subject: [PATCH] reorganize code in database.rs --- src/database.rs | 120 ++++++++++++++++++++++-------------------------- 1 file changed, 55 insertions(+), 65 deletions(-) diff --git a/src/database.rs b/src/database.rs index 0738ba9..c8aa596 100644 --- a/src/database.rs +++ b/src/database.rs @@ -38,19 +38,6 @@ pub enum EntryValue { Invalid, } -impl TryFrom for Entry { - type Error = anyhow::Error; - - fn try_from(e: models::Entry) -> Result { - Ok(Entry { - identity: Hash(e.identity), - target: Address::decode(&e.target)?, - key: e.key, - value: e.value.parse().unwrap(), - }) - } -} - impl Entry { pub fn as_json(&self) -> serde_json::Value { json!({ @@ -65,12 +52,35 @@ impl Entry { } } +impl TryFrom for Entry { + type Error = anyhow::Error; + + fn try_from(e: models::Entry) -> Result { + Ok(Entry { + identity: Hash(e.identity), + target: Address::decode(&e.target)?, + key: e.key, + value: e.value.parse().unwrap(), + }) + } +} + impl std::fmt::Display for InnerEntry { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{} | {} | {}", self.target, self.key, self.value) } } +impl Hashable for InnerEntry { + fn hash(self: &InnerEntry) -> Result { + let mut result = Cursor::new(vec![0u8; 0]); + result.write(self.target.encode()?.as_slice())?; + result.write(self.key.as_bytes())?; + result.write(self.value.to_str()?.as_bytes())?; + Ok(hash(result.get_ref())) + } +} + impl EntryValue { pub fn to_str(&self) -> Result { let (type_char, content) = match self { @@ -140,48 +150,6 @@ pub struct InsertFile { pub file: models::NewFile, } -#[derive(Message)] -#[rtype(result = "Result>")] -pub struct RetrieveByHash { - pub hash: Hash, -} - -#[derive(Message)] -#[rtype(result = "Result>")] -pub struct LookupByFilename { - pub query: String, -} - -#[derive(Message)] -#[rtype(result = "Result>")] -pub struct RetrieveObject { - pub target: Address, -} - -#[derive(Message)] -#[rtype(result = "Result>")] -pub struct QueryEntries { - pub target: Option
, - pub key: Option, - pub value: Option, -} - -impl Default for QueryEntries { - fn default() -> Self { - QueryEntries { - target: None, - key: None, - value: None, - } - } -} - -#[derive(Message)] -#[rtype(result = "Result")] -pub struct InsertEntry { - pub entry: InnerEntry, -} - impl Handler for DbExecutor { type Result = Result; @@ -202,6 +170,12 @@ impl Handler for DbExecutor { } } +#[derive(Message)] +#[rtype(result = "Result>")] +pub struct RetrieveByHash { + pub hash: Hash, +} + impl Handler for DbExecutor { type Result = Result>; @@ -219,6 +193,12 @@ impl Handler for DbExecutor { } } +#[derive(Message)] +#[rtype(result = "Result>")] +pub struct LookupByFilename { + pub query: String, +} + impl Handler for DbExecutor { type Result = Result>; @@ -236,6 +216,12 @@ impl Handler for DbExecutor { } } +#[derive(Message)] +#[rtype(result = "Result>")] +pub struct RetrieveObject { + pub target: Address, +} + impl Handler for DbExecutor { type Result = Result>; @@ -258,6 +244,14 @@ impl Handler for DbExecutor { } } +#[derive(Message)] +#[rtype(result = "Result>")] +pub struct QueryEntries { + pub target: Option
, + pub key: Option, + pub value: Option, +} + impl Handler for DbExecutor { type Result = Result>; @@ -292,6 +286,12 @@ impl Handler for DbExecutor { } } +#[derive(Message)] +#[rtype(result = "Result")] +pub struct InsertEntry { + pub entry: InnerEntry, +} + impl Handler for DbExecutor { type Result = Result; @@ -315,16 +315,6 @@ impl Handler for DbExecutor { } } -impl Hashable for InnerEntry { - fn hash(self: &InnerEntry) -> Result { - let mut result = Cursor::new(vec![0u8; 0]); - result.write(self.target.encode()?.as_slice())?; - result.write(self.key.as_bytes())?; - result.write(self.value.to_str()?.as_bytes())?; - Ok(hash(result.get_ref())) - } -} - #[derive(Debug)] pub struct ConnectionOptions { pub enable_foreign_keys: bool,