parent
697f7463e7
commit
11a2807116
@ -0,0 +1,66 @@
|
||||
use crate::hydrus_serializable::definitions_update::{
|
||||
HashDefinition, HydrusDefinitionsUpdate, TagDefinition,
|
||||
};
|
||||
use crate::hydrus_serializable::wrapper::GenericHydrusSerWrapper;
|
||||
use crate::Result;
|
||||
use crate::{Endpoint, Error, FromJson, GetEndpoint};
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct UpdateEndpoint;
|
||||
|
||||
impl Endpoint for UpdateEndpoint {
|
||||
fn path() -> &'static str {
|
||||
"update"
|
||||
}
|
||||
}
|
||||
|
||||
impl GetEndpoint for UpdateEndpoint {
|
||||
type Response = UpdateResponse;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum UpdateResponse {
|
||||
Definitions(DefinitionsUpdateResponse),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct DefinitionsUpdateResponse {
|
||||
pub hashes: HashMap<u64, String>,
|
||||
pub tags: HashMap<u64, String>,
|
||||
}
|
||||
|
||||
impl FromJson for UpdateResponse {
|
||||
fn from_json(value: Value) -> crate::Result<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let wrapper = serde_json::from_value::<GenericHydrusSerWrapper>(value)?;
|
||||
match wrapper.type_id {
|
||||
36 => {
|
||||
let definitions_update = DefinitionsUpdateResponse::from_wrapper(wrapper)?;
|
||||
|
||||
Ok(Self::Definitions(definitions_update))
|
||||
}
|
||||
_ => Err(Error::Malformed),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DefinitionsUpdateResponse {
|
||||
fn from_wrapper(wrapper: GenericHydrusSerWrapper) -> Result<Self> {
|
||||
let mut definitions_update = wrapper.into_inner::<HydrusDefinitionsUpdate>()?;
|
||||
|
||||
let hashes = definitions_update
|
||||
.take::<HashDefinition>()?
|
||||
.map(|h| h.into_iter().map(|h| (h.id, h.hash)).collect())
|
||||
.unwrap_or_else(|| HashMap::new());
|
||||
|
||||
let tags = definitions_update
|
||||
.take::<TagDefinition>()?
|
||||
.map(|t| t.into_iter().map(|t| (t.id, t.tag)).collect())
|
||||
.unwrap_or_else(|| HashMap::new());
|
||||
|
||||
Ok(Self { hashes, tags })
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
use crate::constants::HYDRUS_TYPE_DEFINITIONS_UPDATE;
|
||||
use crate::hydrus_serializable::HydrusSerializable;
|
||||
use crate::{Error, Result};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct HydrusDefinitionsUpdate(pub Vec<DefinitionsUpdateEntries>);
|
||||
|
||||
impl HydrusDefinitionsUpdate {
|
||||
pub fn take<D: DefinitionsTrait>(&mut self) -> Result<Option<Vec<D>>> {
|
||||
let entry_index = self
|
||||
.0
|
||||
.iter()
|
||||
.position(|d| d.definition_id == D::definition_id());
|
||||
if let Some(idx) = entry_index {
|
||||
let entry = self.0.swap_remove(idx);
|
||||
|
||||
entry.into_inner()
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl HydrusSerializable for HydrusDefinitionsUpdate {
|
||||
fn type_id() -> u64 {
|
||||
HYDRUS_TYPE_DEFINITIONS_UPDATE
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct DefinitionsUpdateEntries {
|
||||
pub definition_id: u64,
|
||||
entries: Value,
|
||||
}
|
||||
|
||||
impl DefinitionsUpdateEntries {
|
||||
pub fn into_inner<T: DeserializeOwned>(self) -> Result<T> {
|
||||
serde_json::from_value::<T>(self.entries).map_err(Error::from)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait DefinitionsTrait: DeserializeOwned {
|
||||
fn definition_id() -> u64;
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
pub struct HashDefinition {
|
||||
pub id: u64,
|
||||
pub hash: String,
|
||||
}
|
||||
|
||||
impl DefinitionsTrait for HashDefinition {
|
||||
fn definition_id() -> u64 {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
pub struct TagDefinition {
|
||||
pub id: u64,
|
||||
pub tag: String,
|
||||
}
|
||||
|
||||
impl DefinitionsTrait for TagDefinition {
|
||||
fn definition_id() -> u64 {
|
||||
1
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue