Comment and add from_term deserialization function
This commit is contained in:
parent
7a79994b00
commit
5651210a83
7 changed files with 30 additions and 3 deletions
|
@ -2,7 +2,7 @@
|
|||
name = "nettext"
|
||||
description = "A text-based data format for cryptographic network protocols"
|
||||
authors = ["Alex Auvolat <alex@adnab.me>"]
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
license = "AGPL-3.0"
|
||||
readme = "README.md"
|
||||
|
|
|
@ -4,6 +4,7 @@ use crate::enc;
|
|||
|
||||
pub use ed25519_dalek::{Keypair, PublicKey, SecretKey, Signature, Signer, Verifier};
|
||||
|
||||
/// Generate a public/secret Ed25519 keypair
|
||||
pub fn generate_keypair() -> Keypair {
|
||||
let mut csprng = thread_rng();
|
||||
Keypair::generate(&mut csprng)
|
||||
|
|
|
@ -37,6 +37,7 @@ enum T<'a> {
|
|||
List(Vec<T<'a>>),
|
||||
}
|
||||
|
||||
/// The result type for trying to encode something as nettext
|
||||
pub type Result<'a> = std::result::Result<Term<'a>, Error>;
|
||||
|
||||
// ---- helpers to transform datatypes into encoder terms ----
|
||||
|
|
|
@ -10,10 +10,11 @@ use crate::dec::debug as fmtdebug;
|
|||
use crate::dec::*;
|
||||
use crate::serde::error::{Error, Result};
|
||||
|
||||
/// Serde deserializer for nettext
|
||||
pub struct Deserializer<'de, 'a>(Term<'de, 'a>);
|
||||
|
||||
impl<'de, 'a> Deserializer<'de, 'a> {
|
||||
pub fn from_term(input: &'a Term<'de, 'a>) -> Deserializer<'de, 'a> {
|
||||
fn from_term(input: &'a Term<'de, 'a>) -> Deserializer<'de, 'a> {
|
||||
if let Ok(nested) = input.nested() {
|
||||
Deserializer(nested)
|
||||
} else {
|
||||
|
@ -22,6 +23,7 @@ impl<'de, 'a> Deserializer<'de, 'a> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Deserialize nettext from bytes
|
||||
pub fn from_bytes<'a, T>(s: &'a [u8]) -> Result<T>
|
||||
where
|
||||
T: Deserialize<'a>,
|
||||
|
@ -31,6 +33,15 @@ where
|
|||
T::deserialize(&mut deserializer)
|
||||
}
|
||||
|
||||
/// Deserialize nettext from decoded term
|
||||
pub fn from_term<'a, T>(term: &'a Term<'a, 'a>) -> Result<T>
|
||||
where
|
||||
T: Deserialize<'a>,
|
||||
{
|
||||
let mut deserializer = Deserializer::from_term(term);
|
||||
T::deserialize(&mut deserializer)
|
||||
}
|
||||
|
||||
// ----
|
||||
|
||||
impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de, 'a> {
|
||||
|
|
|
@ -5,16 +5,25 @@ use serde::{de, ser};
|
|||
|
||||
use crate::{dec, enc};
|
||||
|
||||
/// Result of a serialization/deserialization operation
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Error representing a serialization/deserialization error
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
/// Custom error message
|
||||
Message(String),
|
||||
/// Nettext encoding error
|
||||
Encode(enc::Error),
|
||||
/// Nettext decoding error
|
||||
Decode(String),
|
||||
/// Nettext interpretation error
|
||||
Type(dec::TypeError),
|
||||
/// Cannot parse term as integer
|
||||
ParseInt(std::num::ParseIntError),
|
||||
/// Cannot parse term as float
|
||||
ParseFloat(std::num::ParseFloatError),
|
||||
/// Invalid utf8 byte string
|
||||
Utf8(std::string::FromUtf8Error),
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
//! Serde serialization and deserialization for nettext
|
||||
|
||||
mod de;
|
||||
mod error;
|
||||
mod ser;
|
||||
|
||||
pub use de::{from_bytes, Deserializer};
|
||||
pub use de::{from_bytes, from_term, Deserializer};
|
||||
pub use error::{Error, Result};
|
||||
pub use ser::{to_bytes, to_term, Serializer};
|
||||
|
||||
|
|
|
@ -4,8 +4,10 @@ use crate::enc::*;
|
|||
use crate::serde::error::{Error, Result};
|
||||
use serde::ser::Error as SerError;
|
||||
|
||||
/// Serde serializer for nettext
|
||||
pub struct Serializer;
|
||||
|
||||
/// Serialize value to nettext encoder term
|
||||
pub fn to_term<T>(value: &T) -> Result<Term<'static>>
|
||||
where
|
||||
T: Serialize,
|
||||
|
@ -13,6 +15,7 @@ where
|
|||
value.serialize(&mut Serializer)
|
||||
}
|
||||
|
||||
/// Serialize value to nettext
|
||||
pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>>
|
||||
where
|
||||
T: Serialize,
|
||||
|
|
Loading…
Reference in a new issue