Comment and add from_term deserialization function

This commit is contained in:
Alex 2022-11-18 19:11:40 +01:00
parent 7a79994b00
commit 5651210a83
Signed by: lx
GPG key ID: 0E496D15096376BE
7 changed files with 30 additions and 3 deletions

View file

@ -2,7 +2,7 @@
name = "nettext" name = "nettext"
description = "A text-based data format for cryptographic network protocols" description = "A text-based data format for cryptographic network protocols"
authors = ["Alex Auvolat <alex@adnab.me>"] authors = ["Alex Auvolat <alex@adnab.me>"]
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2021"
license = "AGPL-3.0" license = "AGPL-3.0"
readme = "README.md" readme = "README.md"

View file

@ -4,6 +4,7 @@ use crate::enc;
pub use ed25519_dalek::{Keypair, PublicKey, SecretKey, Signature, Signer, Verifier}; pub use ed25519_dalek::{Keypair, PublicKey, SecretKey, Signature, Signer, Verifier};
/// Generate a public/secret Ed25519 keypair
pub fn generate_keypair() -> Keypair { pub fn generate_keypair() -> Keypair {
let mut csprng = thread_rng(); let mut csprng = thread_rng();
Keypair::generate(&mut csprng) Keypair::generate(&mut csprng)

View file

@ -37,6 +37,7 @@ enum T<'a> {
List(Vec<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>; pub type Result<'a> = std::result::Result<Term<'a>, Error>;
// ---- helpers to transform datatypes into encoder terms ---- // ---- helpers to transform datatypes into encoder terms ----

View file

@ -10,10 +10,11 @@ use crate::dec::debug as fmtdebug;
use crate::dec::*; use crate::dec::*;
use crate::serde::error::{Error, Result}; use crate::serde::error::{Error, Result};
/// Serde deserializer for nettext
pub struct Deserializer<'de, 'a>(Term<'de, 'a>); pub struct Deserializer<'de, 'a>(Term<'de, 'a>);
impl<'de, 'a> Deserializer<'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() { if let Ok(nested) = input.nested() {
Deserializer(nested) Deserializer(nested)
} else { } 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> pub fn from_bytes<'a, T>(s: &'a [u8]) -> Result<T>
where where
T: Deserialize<'a>, T: Deserialize<'a>,
@ -31,6 +33,15 @@ where
T::deserialize(&mut deserializer) 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> { impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de, 'a> {

View file

@ -5,16 +5,25 @@ use serde::{de, ser};
use crate::{dec, enc}; use crate::{dec, enc};
/// Result of a serialization/deserialization operation
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
/// Error representing a serialization/deserialization error
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
/// Custom error message
Message(String), Message(String),
/// Nettext encoding error
Encode(enc::Error), Encode(enc::Error),
/// Nettext decoding error
Decode(String), Decode(String),
/// Nettext interpretation error
Type(dec::TypeError), Type(dec::TypeError),
/// Cannot parse term as integer
ParseInt(std::num::ParseIntError), ParseInt(std::num::ParseIntError),
/// Cannot parse term as float
ParseFloat(std::num::ParseFloatError), ParseFloat(std::num::ParseFloatError),
/// Invalid utf8 byte string
Utf8(std::string::FromUtf8Error), Utf8(std::string::FromUtf8Error),
} }

View file

@ -1,8 +1,10 @@
//! Serde serialization and deserialization for nettext
mod de; mod de;
mod error; mod error;
mod ser; mod ser;
pub use de::{from_bytes, Deserializer}; pub use de::{from_bytes, from_term, Deserializer};
pub use error::{Error, Result}; pub use error::{Error, Result};
pub use ser::{to_bytes, to_term, Serializer}; pub use ser::{to_bytes, to_term, Serializer};

View file

@ -4,8 +4,10 @@ use crate::enc::*;
use crate::serde::error::{Error, Result}; use crate::serde::error::{Error, Result};
use serde::ser::Error as SerError; use serde::ser::Error as SerError;
/// Serde serializer for nettext
pub struct Serializer; pub struct Serializer;
/// Serialize value to nettext encoder term
pub fn to_term<T>(value: &T) -> Result<Term<'static>> pub fn to_term<T>(value: &T) -> Result<Term<'static>>
where where
T: Serialize, T: Serialize,
@ -13,6 +15,7 @@ where
value.serialize(&mut Serializer) value.serialize(&mut Serializer)
} }
/// Serialize value to nettext
pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>> pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>>
where where
T: Serialize, T: Serialize,