diff --git a/src/database/entry.rs b/src/database/entry.rs index 9e06de7..6cb9c4f 100644 --- a/src/database/entry.rs +++ b/src/database/entry.rs @@ -3,43 +3,9 @@ use crate::database::inner::models; use crate::util::hash::{b58_decode, hash, Hash, Hashable}; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -use std::convert::{TryFrom, TryInto}; +use std::convert::TryFrom; use std::io::{Cursor, Write}; -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(untagged)] -pub enum InEntry { - Entry(Entry), - Invariant(InvariantEntry), - Address { entity: InAddress }, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(untagged)] -pub enum InAddress { - Address(String), - Components { t: String, c: String }, -} - -impl TryInto
for InAddress { - type Error = anyhow::Error; - - fn try_into(self) -> Result { - Ok(match self { - InAddress::Address(address) => address.parse()?, - InAddress::Components { t, c } => { - // I absolutely cannot handle serde magic right now - // TODO: make this automatically derive from `Address` definition - match t.as_str() { - "Attribute" => Address::Attribute(c), - "Url" => Address::Url(c), - _ => c.parse()?, - } - } - }) - } -} - #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Entry { pub entity: Address, @@ -264,19 +230,4 @@ mod tests { Ok(()) } - - #[test] - fn test_in_address() -> Result<()> { - let address = Address::Url("https://upendproject.net".into()); - let in_address = InAddress::Address(address.to_string()); - assert_eq!(address, in_address.try_into()?); - - let in_address = InAddress::Components { - t: "Url".into(), - c: "https://upendproject.net".into(), - }; - assert_eq!(address, in_address.try_into()?); - - Ok(()) - } } diff --git a/src/routes.rs b/src/routes.rs index 86354ef..e10e0b9 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,6 +1,6 @@ use crate::addressing::{Address, Addressable}; use crate::database::constants::{ADDED_ATTR, LABEL_ATTR}; -use crate::database::entry::{Entry, EntryValue, InEntry, InvariantEntry}; +use crate::database::entry::{Entry, EntryValue, InvariantEntry}; use crate::database::hierarchies::{list_roots, resolve_path, UHierPath}; use crate::database::lang::Query; use crate::database::UpEndDatabase; @@ -245,6 +245,40 @@ pub async fn get_object( }))) } +#[derive(Debug, Clone, Deserialize)] +#[serde(untagged)] +pub enum InEntry { + Entry(Entry), + Invariant(InvariantEntry), + Address { entity: InAddress }, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(untagged)] +pub enum InAddress { + Address(String), + Components { t: String, c: String }, +} + +impl TryInto
for InAddress { + type Error = anyhow::Error; + + fn try_into(self) -> Result { + Ok(match self { + InAddress::Address(address) => address.parse()?, + InAddress::Components { t, c } => { + // I absolutely cannot handle serde magic right now + // TODO: make this automatically derive from `Address` definition + match t.as_str() { + "Attribute" => Address::Attribute(c), + "Url" => Address::Url(c), + _ => c.parse()?, + } + } + }) + } +} + #[put("/api/obj")] pub async fn put_object( state: web::Data, @@ -596,3 +630,24 @@ pub async fn get_info(state: web::Data) -> Result { "desktop": state.desktop_enabled }))) } + +#[cfg(test)] +mod tests { + use super::*; + use anyhow::Result; + + #[test] + fn test_in_address() -> Result<()> { + let address = Address::Url("https://upendproject.net".into()); + let in_address = InAddress::Address(address.to_string()); + assert_eq!(address, in_address.try_into()?); + + let in_address = InAddress::Components { + t: "Url".into(), + c: "https://upendproject.net".into(), + }; + assert_eq!(address, in_address.try_into()?); + + Ok(()) + } +}