reorganize code in database.rs
parent
267bca92bf
commit
d505653866
120
src/database.rs
120
src/database.rs
|
@ -38,19 +38,6 @@ pub enum EntryValue {
|
|||
Invalid,
|
||||
}
|
||||
|
||||
impl TryFrom<models::Entry> for Entry {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(e: models::Entry) -> Result<Self, Self::Error> {
|
||||
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<models::Entry> for Entry {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(e: models::Entry) -> Result<Self, Self::Error> {
|
||||
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<Hash> {
|
||||
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<String> {
|
||||
let (type_char, content) = match self {
|
||||
|
@ -140,48 +150,6 @@ pub struct InsertFile {
|
|||
pub file: models::NewFile,
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Result<Option<String>>")]
|
||||
pub struct RetrieveByHash {
|
||||
pub hash: Hash,
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Result<Vec<models::File>>")]
|
||||
pub struct LookupByFilename {
|
||||
pub query: String,
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Result<Vec<Entry>>")]
|
||||
pub struct RetrieveObject {
|
||||
pub target: Address,
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Result<Vec<Entry>>")]
|
||||
pub struct QueryEntries {
|
||||
pub target: Option<Address>,
|
||||
pub key: Option<String>,
|
||||
pub value: Option<EntryValue>,
|
||||
}
|
||||
|
||||
impl Default for QueryEntries {
|
||||
fn default() -> Self {
|
||||
QueryEntries {
|
||||
target: None,
|
||||
key: None,
|
||||
value: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Result<usize>")]
|
||||
pub struct InsertEntry {
|
||||
pub entry: InnerEntry,
|
||||
}
|
||||
|
||||
impl Handler<InsertFile> for DbExecutor {
|
||||
type Result = Result<usize>;
|
||||
|
||||
|
@ -202,6 +170,12 @@ impl Handler<InsertFile> for DbExecutor {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Result<Option<String>>")]
|
||||
pub struct RetrieveByHash {
|
||||
pub hash: Hash,
|
||||
}
|
||||
|
||||
impl Handler<RetrieveByHash> for DbExecutor {
|
||||
type Result = Result<Option<String>>;
|
||||
|
||||
|
@ -219,6 +193,12 @@ impl Handler<RetrieveByHash> for DbExecutor {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Result<Vec<models::File>>")]
|
||||
pub struct LookupByFilename {
|
||||
pub query: String,
|
||||
}
|
||||
|
||||
impl Handler<LookupByFilename> for DbExecutor {
|
||||
type Result = Result<Vec<models::File>>;
|
||||
|
||||
|
@ -236,6 +216,12 @@ impl Handler<LookupByFilename> for DbExecutor {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Result<Vec<Entry>>")]
|
||||
pub struct RetrieveObject {
|
||||
pub target: Address,
|
||||
}
|
||||
|
||||
impl Handler<RetrieveObject> for DbExecutor {
|
||||
type Result = Result<Vec<Entry>>;
|
||||
|
||||
|
@ -258,6 +244,14 @@ impl Handler<RetrieveObject> for DbExecutor {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Result<Vec<Entry>>")]
|
||||
pub struct QueryEntries {
|
||||
pub target: Option<Address>,
|
||||
pub key: Option<String>,
|
||||
pub value: Option<EntryValue>,
|
||||
}
|
||||
|
||||
impl Handler<QueryEntries> for DbExecutor {
|
||||
type Result = Result<Vec<Entry>>;
|
||||
|
||||
|
@ -292,6 +286,12 @@ impl Handler<QueryEntries> for DbExecutor {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Result<usize>")]
|
||||
pub struct InsertEntry {
|
||||
pub entry: InnerEntry,
|
||||
}
|
||||
|
||||
impl Handler<InsertEntry> for DbExecutor {
|
||||
type Result = Result<usize>;
|
||||
|
||||
|
@ -315,16 +315,6 @@ impl Handler<InsertEntry> for DbExecutor {
|
|||
}
|
||||
}
|
||||
|
||||
impl Hashable for InnerEntry {
|
||||
fn hash(self: &InnerEntry) -> Result<Hash> {
|
||||
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,
|
||||
|
|
Loading…
Reference in New Issue