impl Serialize for SSK (serde json)
This commit is contained in:
parent
e11e72eab7
commit
f37a230f02
|
@ -13,3 +13,8 @@ description = "Implementation of FCPv2 freenet protocol"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
regex = "1.4.3"
|
regex = "1.4.3"
|
||||||
rusqlite = "0.24.2"
|
rusqlite = "0.24.2"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
[dev-dependencies]
|
||||||
|
serde_json = "1.0"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod client;
|
pub mod client;
|
||||||
pub mod node;
|
pub mod node;
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
extern crate serde_json;
|
||||||
|
|
|
@ -1,7 +1,13 @@
|
||||||
use super::traits::{FcpParser, FcpRequest};
|
use super::traits::{FcpParser, FcpRequest};
|
||||||
use rusqlite::types::ToSqlOutput;
|
use rusqlite::types::ToSqlOutput;
|
||||||
use rusqlite::{Result, ToSql, types::{FromSql, ValueRef, FromSqlResult, FromSqlError}};
|
use rusqlite::{
|
||||||
|
types::{FromSql, FromSqlError, FromSqlResult, ValueRef},
|
||||||
|
Result, ToSql,
|
||||||
|
};
|
||||||
|
extern crate serde;
|
||||||
|
|
||||||
|
use serde::ser::{SerializeStruct, Serializer};
|
||||||
|
use serde::Serialize ;
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct SSK {
|
pub struct SSK {
|
||||||
pub sign_key: String,
|
pub sign_key: String,
|
||||||
|
@ -16,19 +22,51 @@ impl ToSql for SSK {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// converting from rusqlite type to SSK
|
/// converting from rusqlite type to SSK
|
||||||
impl FromSql for SSK{
|
impl FromSql for SSK {
|
||||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self>{
|
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||||
match SSK::parse(value.as_str()?) {
|
match SSK::parse(value.as_str()?) {
|
||||||
Some(res) => Ok(res),
|
Some(res) => Ok(res),
|
||||||
None => Err(FromSqlError::InvalidType)
|
None => Err(FromSqlError::InvalidType),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl serde::ser::Serialize for SSK {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
// 3 is the number of fields in the struct.
|
||||||
|
serializer.serialize_str(&self.convert()[..])
|
||||||
|
}
|
||||||
|
}
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct USK {
|
pub struct USK {
|
||||||
pub ssk: SSK,
|
pub ssk: SSK,
|
||||||
pub index: i32,
|
pub index: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use types::traits::FcpRequest;
|
||||||
|
|
||||||
|
use crate::types::SSK;
|
||||||
|
use crate::*;
|
||||||
|
#[test]
|
||||||
|
fn is_serializing() {
|
||||||
|
let ssk:SSK = SSK {
|
||||||
|
sign_key: "AKTTKG6YwjrHzWo67laRcoPqibyiTdyYufjVg54fBlWr".to_string(),
|
||||||
|
decrypt_key: "AwUSJG5ZS-FDZTqnt6skTzhxQe08T-fbKXj8aEHZsXM".to_string(),
|
||||||
|
settings: None
|
||||||
|
};
|
||||||
|
let json = serde_json::json!(&ssk);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
assert_eq!("SSK@AKTTKG6YwjrHzWo67laRcoPqibyiTdyYufjVg54fBlWr,AwUSJG5ZS-FDZTqnt6skTzhxQe08T-fbKXj8aEHZsXM", json);
|
||||||
|
assert_eq!("SSK@AKTTKG6YwjrHzWo67laRcoPqibyiTdyYufjVg54fBlWr,AwUSJG5ZS-FDZTqnt6skTzhxQe08T-fbKXj8aEHZsXM", &ssk.convert());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue