start base64 serialiser

master
Avril 4 years ago
parent 6eff4d89ec
commit 093930b57d
Signed by: flanchan
GPG Key ID: 284488987C31F630

1
Cargo.lock generated

@ -746,6 +746,7 @@ dependencies = [
name = "rae"
version = "0.1.0"
dependencies = [
"base64",
"base65536",
"cfg-if",
"chrono",

@ -31,7 +31,8 @@ serde_cbor = "0.11.1"
futures = "0.3.5"
chrono = "0.4.15"
pin-project = "0.4.23"
#serde_json = "1.0.57" # serde not suitable for our text formatting :/
base64 = "0.12.3"
#serde_json = "1.0.57" # serde not suitable for our text formatting :/ maybe just use `cbor` -> base64 with text header? would be a PEM-like format. sounds good imo
[build-dependencies]
rustc_version = "0.2"

@ -104,22 +104,13 @@ impl KeyHeader
#[instrument(err, skip(out))]
pub async fn write_text<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T) -> Result<usize, eyre::Report>
{
let vec = serde_json::to_vec(self)
.wrap_err_with(|| eyre::eyre!("Failed to serialise self to JSON"))
.with_section(|| format!("{:?}", self).header("Self was"))?;
out.write_all(&vec[..]).await?;
Ok(vec.len())
todo!()
}
/// Read a superheader as text bytes from this stream
#[instrument(err, skip(input))]
pub async fn read_text<T: AsyncRead+Unpin+?Sized>(input: &mut T) -> Result<Self, eyre::Report>
{
let whole = input.read_whole_stream(MAX_TEXT_SZ).await
.wrap_err_with(|| eyre::eyre!("Failed to read text stream into memory"))?;
Ok(serde_json::from_slice(&whole[..])
.wrap_err_with(|| eyre::eyre!("Failed to deserialise JSON to value"))
.with_section(move || String::from_utf8_lossy(&whole[..]).into_owned().header("Read text was"))?)
todo!()
}
/// Write this key header as bytes to this stream
#[instrument(err, skip(out))]
@ -269,7 +260,7 @@ mod tests
//color_eyre::install()?;
panic!("We're going to have to write our own text serialisation to get around the `read_whole_buffer` thingy...");
/*
let header = KeyHeader::new_now(KeyHeaderKind::Aes, Default::default(), Default::default());
let mut ser = Vec::new();
let superheader = SuperHeader::<KeyHeader>::new_for(&header);
@ -290,6 +281,6 @@ mod tests
assert_eq!(readheader, header);
assert_eq!(reads, superheader);
Ok(())
Ok(())*/
}
}

@ -56,22 +56,13 @@ impl<H: Header> SuperHeader<H>
#[instrument(err, skip(out))]
pub async fn write_text<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T) -> Result<usize, eyre::Report>
{
let vec = serde_json::to_vec(self)
.wrap_err_with(|| eyre::eyre!("Failed to serialise self to JSON"))
.with_section(|| format!("{:?}", self).header("Self was"))?;
out.write_all(&vec[..]).await?;
Ok(vec.len())
todo!()
}
/// Read a superheader as text bytes from this stream
#[instrument(err, skip(input))]
pub async fn read_text<T: AsyncRead+Unpin+?Sized>(input: &mut T) -> Result<Self, eyre::Report>
{
let whole = input.read_whole_stream(MAX_TEXT_SZ).await
.wrap_err_with(|| eyre::eyre!("Failed to read text stream into memory"))?;
Ok(serde_json::from_slice(&whole[..])
.wrap_err_with(|| eyre::eyre!("Failed to deserialise JSON to value"))
.with_section(move || String::from_utf8_lossy(&whole[..]).into_owned().header("Read text was"))?)
todo!()
}
/// Write this superheader as bytes to this stream

@ -66,6 +66,73 @@ pub mod timestamp
}
}
pub mod serialise
{
use super::*;
#[instrument(err, skip(value), fields(value_type=?std::any::type_name::<T>()))]
pub fn into_text<T: Serialize>(value: &T) -> Result<String, Error>
{
let bytes = serde_cbor::to_vec(value).map_err(Error::Serialise)?;
Ok(base64::encode(bytes))
}
#[derive(Debug)]
pub enum Error
{
Base64(base64::DecodeError),
Serialise(serde_cbor::Error),
Deserialise(serde_cbor::Error),
}
impl std::error::Error for Error
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self {
Self::Base64(b) => Some(b),
Self::Serialise(b) => Some(b),
Self::Deserialise(b) => Some(b),
}
}
}
impl std::fmt::Display for Error
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
{
match self {
Self::Base64(_) => write!(f, "base64 decode failed"),
Self::Serialise(_) => write!(f, "serialisation failed"),
Self::Deserialise(_) => write!(f, "deserialisation failed"),
}
}
}
#[instrument(err, skip(string), fields(string = ?string.as_ref()))]
pub fn from_text<T: serde::de::DeserializeOwned>(string: impl AsRef<[u8]>) -> Result<T, Error>
{
let bytes = base64::decode(string).map_err(Error::Base64)?;
serde_cbor::from_reader(&bytes[..]).map_err(Error::Deserialise)
}
#[cfg(test)]
mod tests
{
use crate::*;
#[test]
fn test_ser() -> Result<(), eyre::Report>
{
let object = "hello world";
let enc = super::into_text(&String::from(object)).wrap_err(eyre::eyre!("failed to encode"))?;
println!("Enc: {:?}", enc);
let dec: String = super::from_text(enc).wrap_err(eyre::eyre!("failed to decode"))?;
assert_eq!(object,dec);
Ok(())
}
}
}
/*/// Dispatch params operations that can be handled at top level (i.e. `Help`)
async fn dispatch_args() -> Result<args::Operation, eyre::Report>
{

Loading…
Cancel
Save