move InEntry/InAddress to routes

feat/vaults
Tomáš Mládek 2022-02-12 14:51:28 +01:00
parent fd0b4a53d2
commit 038e4fd7d2
No known key found for this signature in database
GPG Key ID: 65E225C8B3E2ED8A
2 changed files with 57 additions and 51 deletions

View File

@ -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<Address> for InAddress {
type Error = anyhow::Error;
fn try_into(self) -> Result<Address, Self::Error> {
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(())
}
}

View File

@ -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<Address> for InAddress {
type Error = anyhow::Error;
fn try_into(self) -> Result<Address, Self::Error> {
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<State>,
@ -596,3 +630,24 @@ pub async fn get_info(state: web::Data<State>) -> Result<HttpResponse, Error> {
"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(())
}
}