fail to implement deserialize for SSK

This commit is contained in:
Horhik 2021-03-29 10:21:38 +03:00
parent f37a230f02
commit 6857959562

View file

@ -6,8 +6,11 @@ use rusqlite::{
}; };
extern crate serde; extern crate serde;
use std::fmt;
use serde::de::{self, Deserialize, Deserializer, MapAccess, SeqAccess, Visitor, Error};
use serde::ser::{SerializeStruct, Serializer}; use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize ; use serde::Serialize;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct SSK { pub struct SSK {
pub sign_key: String, pub sign_key: String,
@ -40,6 +43,70 @@ impl serde::ser::Serialize for SSK {
serializer.serialize_str(&self.convert()[..]) serializer.serialize_str(&self.convert()[..])
} }
} }
impl<'de> Deserialize<'de> for SSK {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct SSKVisitor;
impl<'de> Visitor<'de> for SSKVisitor {
type Value = SSK;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("struct SSK")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
return Ok(SSK{sign_key: "lol".to_string(), decrypt_key: "kik".to_string(),settings: Some("kek".to_string())});
match SSK::parse(v) {
Some(ssk) => {
Ok(ssk)
},
None => {
Err(de::Error::unknown_variant(v, &["expected key with structure like SSK@../../?"]))
}
}
}
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
where
E: de::Error,
{
return Ok(SSK{sign_key: "lol".to_string(), decrypt_key: "kik".to_string(),settings: Some("kek".to_string())});
match SSK::parse(v) {
Some(ssk) => {
Ok(ssk)
},
None => {
Err(de::Error::unknown_variant(v, &["expected key with structure like SSK@../../?"]))
}
}
}
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: de::Error,
{
return Ok(SSK{sign_key: "lol".to_string(), decrypt_key: "kik".to_string(),settings: Some("kek".to_string())});
match SSK::parse(&v[..]) {
Some(ssk) => {
Ok(ssk)
},
None => {
Err(de::Error::unknown_variant(&v[..], &["expected key with structure like SSK@../../?"]))
}
}
}
}
println!("tset");
return deserializer.deserialize_any(SSKVisitor)
}
}
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct USK { pub struct USK {
pub ssk: SSK, pub ssk: SSK,
@ -53,20 +120,26 @@ mod tests {
use crate::types::SSK; use crate::types::SSK;
use crate::*; use crate::*;
#[test] #[test]
fn is_serializing() { fn is_ssk_serializing() {
let ssk:SSK = SSK { let ssk: SSK = SSK {
sign_key: "AKTTKG6YwjrHzWo67laRcoPqibyiTdyYufjVg54fBlWr".to_string(), sign_key: "AKTTKG6YwjrHzWo67laRcoPqibyiTdyYufjVg54fBlWr".to_string(),
decrypt_key: "AwUSJG5ZS-FDZTqnt6skTzhxQe08T-fbKXj8aEHZsXM".to_string(), decrypt_key: "AwUSJG5ZS-FDZTqnt6skTzhxQe08T-fbKXj8aEHZsXM".to_string(),
settings: None settings: None,
}; };
let json = serde_json::json!(&ssk); let json = serde_json::json!(&ssk);
assert_eq!("SSK@AKTTKG6YwjrHzWo67laRcoPqibyiTdyYufjVg54fBlWr,AwUSJG5ZS-FDZTqnt6skTzhxQe08T-fbKXj8aEHZsXM", json); assert_eq!("SSK@AKTTKG6YwjrHzWo67laRcoPqibyiTdyYufjVg54fBlWr,AwUSJG5ZS-FDZTqnt6skTzhxQe08T-fbKXj8aEHZsXM", json);
assert_eq!("SSK@AKTTKG6YwjrHzWo67laRcoPqibyiTdyYufjVg54fBlWr,AwUSJG5ZS-FDZTqnt6skTzhxQe08T-fbKXj8aEHZsXM", &ssk.convert()); assert_eq!("SSK@AKTTKG6YwjrHzWo67laRcoPqibyiTdyYufjVg54fBlWr,AwUSJG5ZS-FDZTqnt6skTzhxQe08T-fbKXj8aEHZsXM", &ssk.convert());
}
#[test]
fn is_ssk_deserializing() {
let ssk: SSK = SSK {
sign_key: "AKTTKG6YwjrHzWo67laRcoPqibyiTdyYufjVg54fBlWr".to_string(),
decrypt_key: "AwUSJG5ZS-FDZTqnt6skTzhxQe08T-fbKXj8aEHZsXM".to_string(),
settings: None,
};
let res = serde_json::from_str::<SSK>("SSK@AKTTKG6YwjrHzWo67laRcoPqibyiTdyYufjVg54fBlWr,AwUSJG5ZS-FDZTqnt6skTzhxQe08T-fbKXj8aEHZsXM");
let aa = res.unwrap();
assert_eq!(aa, ssk)
} }
} }