master
Avril 4 years ago
parent 7a0aa40452
commit 988f49d3bd
Signed by: flanchan
GPG Key ID: 284488987C31F630

4
Cargo.lock generated

@ -206,9 +206,9 @@ dependencies = [
[[package]] [[package]]
name = "cryptohelpers" name = "cryptohelpers"
version = "1.2.0" version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ef6d4c394fce0d9b42b9bd7242df5201b6c4996d1b9f63ac75c83ba9e6b05ce" checksum = "0bc52622422e80c9a71f9664e3b90a77465b3ea4f05f06591987395eda0ff0f2"
dependencies = [ dependencies = [
"crc", "crc",
"getrandom", "getrandom",

@ -15,7 +15,7 @@ local-time = []
[dependencies] [dependencies]
color-eyre = "0.5.3" color-eyre = "0.5.3"
lazy_static = "1.4.0" lazy_static = "1.4.0"
crypto = {package= "cryptohelpers", version = "1.2", features=["full", "async", "serialise"]} crypto = {package= "cryptohelpers", version = "1.3", features=["full", "async", "serialise"]}
cfg-if = "0.1.10" cfg-if = "0.1.10"
tokio = {version = "0.2", features=["full"]} tokio = {version = "0.2", features=["full"]}
serde = {version ="1.0.116", features=["derive"]} serde = {version ="1.0.116", features=["derive"]}

@ -82,7 +82,7 @@ impl Header for KeyHeader
fn hash(&self) -> Sha256Hash fn hash(&self) -> Sha256Hash
{ {
let mut output = Vec::new(); let mut output = Vec::new();
self.write_bytes(&mut output).now_or_never().unwrap().expect("Failed to write bytes to in-memory buffer"); self.write_bytes(&mut output, None).now_or_never().unwrap().expect("Failed to write bytes to in-memory buffer");
crypto::sha256::compute_slice(output) crypto::sha256::compute_slice(output)
} }
} }
@ -136,7 +136,7 @@ impl KeyHeader
/// Write this superheader as text bytes to this stream /// Write this superheader as text bytes to this stream
#[instrument(err, skip(out))] #[instrument(err, skip(out))]
pub async fn write_text<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T) -> Result<usize, eyre::Report> pub async fn write_text<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T, passwd: Option<&Password>) -> Result<usize, eyre::Report>
{ {
let text = serialise::into_text(self) let text = serialise::into_text(self)
.wrap_err_with(|| eyre::eyre!("Failed to serialise header to text")) .wrap_err_with(|| eyre::eyre!("Failed to serialise header to text"))
@ -157,8 +157,8 @@ impl KeyHeader
Ok(written + 4) Ok(written + 4)
} }
/// Read a superheader as text bytes from this stream /// Read a superheader as text bytes from this stream
#[instrument(err, skip(input))] #[instrument(err, skip(input, passwd))]
pub async fn read_text<T: AsyncBufRead+Unpin+?Sized>(input: &mut T) -> Result<Self, eyre::Report> pub async fn read_text<T: AsyncBufRead+Unpin+?Sized, F: FnOnce(&Salt) -> Option<Password>>(input: &mut T, passwd: F) -> Result<Self, eyre::Report>
{ {
let (mut tx, mut rx) = mpsc::channel(1); let (mut tx, mut rx) = mpsc::channel(1);
@ -302,7 +302,7 @@ impl KeyHeader
} }
/// Write this key header as bytes to this stream /// Write this key header as bytes to this stream
#[instrument(err, skip(out))] #[instrument(err, skip(out))]
pub async fn write_bytes<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T) -> Result<usize, eyre::Report> pub async fn write_bytes<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T, passwd: Option<&Password>) -> Result<usize, eyre::Report>
{ {
out.write_u8(self.kind as u8).await?; out.write_u8(self.kind as u8).await?;
let desc = { let desc = {
@ -321,8 +321,8 @@ impl KeyHeader
} }
/// Read a key header as bytes from this stream /// Read a key header as bytes from this stream
#[instrument(err, skip(input))] #[instrument(err, skip(input, passwd))]
pub async fn read_bytes<T: AsyncRead+Unpin+?Sized>(input: &mut T) -> Result<Self, eyre::Report> pub async fn read_bytes<T: AsyncRead+Unpin+?Sized, F: FnOnce(&Salt) -> Option<Password>>(input: &mut T, passwd: F) -> Result<Self, eyre::Report>
{ {
let kind = { let kind = {
let byte = input.read_u8().await?; let byte = input.read_u8().await?;
@ -361,27 +361,27 @@ impl KeyHeader
impl serialise::BinarySerialisable for KeyHeader impl serialise::BinarySerialisable for KeyHeader
{ {
#[inline(always)] fn serialise_bytes<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T) -> LocalBoxFuture<'a, Result<usize, eyre::Report>> #[inline(always)] fn serialise_bytes<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T, passwd: Option<&'b Password>) -> LocalBoxFuture<'a, Result<usize, eyre::Report>>
{ {
self.write_bytes(out).boxed_local() self.write_bytes(out, passwd).boxed_local()
} }
#[inline(always)] fn deserialise_bytes<'b , T: AsyncRead+Unpin+?Sized>(input: &'b mut T) -> LocalBoxFuture<'b, Result<Self, eyre::Report>> #[inline(always)] fn deserialise_bytes<'b , T: AsyncRead+Unpin+?Sized, F: for<'r> FnOnce(&'r Salt) -> Option<Password>+ 'b>(input: &'b mut T, passwd: F) -> LocalBoxFuture<'b, Result<Self, eyre::Report>>
where Self: 'b where Self: 'b
{ {
Self::read_bytes(input).boxed_local() Self::read_bytes(input, passwd).boxed_local()
} }
} }
impl serialise::TextSerialiseable for KeyHeader impl serialise::TextSerialiseable for KeyHeader
{ {
#[inline(always)] fn serialise_text<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T) -> LocalBoxFuture<'a, Result<usize, eyre::Report>> #[inline(always)] fn serialise_text<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T, passwd: Option<&'b Password>) -> LocalBoxFuture<'a, Result<usize, eyre::Report>>
{ {
self.write_text(out).boxed_local() self.write_text(out, passwd).boxed_local()
} }
#[inline(always)] fn deserialise_text<'a, T: AsyncBufRead+Unpin+?Sized>(input: &'a mut T) -> LocalBoxFuture<'a, Result<Self, eyre::Report>> #[inline(always)] fn deserialise_text<'a, T: AsyncBufRead+Unpin+?Sized, F: for<'r> FnOnce(&'r Salt) -> Option<Password> + 'a>(input: &'a mut T, passwd: F) -> LocalBoxFuture<'a, Result<Self, eyre::Report>>
where Self: 'a where Self: 'a
{ {
Self::read_text(input).boxed_local() Self::read_text(input, passwd).boxed_local()
} }
} }
@ -414,17 +414,18 @@ mod tests
let header = KeyHeader::new_now(KeyHeaderKind::Aes, Default::default(), Default::default()); let header = KeyHeader::new_now(KeyHeaderKind::Aes, Default::default(), Default::default());
let mut ser = Vec::new(); let mut ser = Vec::new();
let password = Password::derive("hello world", &Default::default());
let superheader = SuperHeader::<KeyHeader>::new_for(&header); let superheader = SuperHeader::<KeyHeader>::new_for(&header);
println!("Writing: {:?} + {:?}", superheader, header); println!("Writing: {:?} + {:?}", superheader, header);
let written = superheader.serialise_bytes(&mut ser).await? + let written = superheader.serialise_bytes(&mut ser, Some(&password)).await? +
header.write_bytes(&mut ser).await?; header.write_bytes(&mut ser,Some(&password)).await?;
println!("Wrote {} bytes", written); println!("Wrote {} bytes", written);
println!("{}\n", ser.fmt_view()); println!("{}\n", ser.fmt_view());
let mut read = &ser[..]; let mut read = &ser[..];
let reads = SuperHeader::deserialise_bytes(&mut read).await?; let reads = SuperHeader::deserialise_bytes(&mut read, |_| Some(password.clone())).await?;
println!("Read super: {:?}", reads); println!("Read super: {:?}", reads);
let readheader = KeyHeader::deserialise_bytes(&mut read).await?; let readheader = KeyHeader::deserialise_bytes(&mut read, |_| Some(password.clone())).await?;
println!("Read real: {:?}", readheader); println!("Read real: {:?}", readheader);
reads.verify_for(&header)?; reads.verify_for(&header)?;
@ -440,11 +441,11 @@ mod tests
let header = KeyHeader::new_now(KeyHeaderKind::Aes, Default::default(), Default::default()); let header = KeyHeader::new_now(KeyHeaderKind::Aes, Default::default(), Default::default());
let mut ser = Vec::new(); let mut ser = Vec::new();
println!("Writing {:?}", header); println!("Writing {:?}", header);
let val = header.write_bytes(&mut ser).await?; let val = header.write_bytes(&mut ser, None).await?;
println!("Wrote {} bytes:", val); println!("Wrote {} bytes:", val);
println!("{}\n", ser.fmt_view()); println!("{}\n", ser.fmt_view());
let reader = KeyHeader::read_bytes(&mut &ser[..]).await?; let reader = KeyHeader::read_bytes(&mut &ser[..], |_| None).await?;
println!("Read: {:?}", reader); println!("Read: {:?}", reader);
assert_eq!(reader, header); assert_eq!(reader, header);
@ -457,11 +458,11 @@ mod tests
let header = KeyHeader::new_now(KeyHeaderKind::Aes, Default::default(), Default::default()); let header = KeyHeader::new_now(KeyHeaderKind::Aes, Default::default(), Default::default());
let mut ser = Vec::new(); let mut ser = Vec::new();
println!("Writing {:?}", header); println!("Writing {:?}", header);
let val = header.write_text(&mut ser).await?; let val = header.write_text(&mut ser, None).await?;
println!("Wrote {} bytes:", val); println!("Wrote {} bytes:", val);
println!("{}\n", ser.fmt_view()); println!("{}\n", ser.fmt_view());
let reader = KeyHeader::read_text(&mut &ser[..]).await?; let reader = KeyHeader::read_text(&mut &ser[..], |_| None).await?;
println!("Read: {:?}", reader); println!("Read: {:?}", reader);
assert_eq!(reader, header); assert_eq!(reader, header);
@ -476,16 +477,16 @@ mod tests
let mut ser = Vec::new(); let mut ser = Vec::new();
let superheader = SuperHeader::<KeyHeader>::new_for(&header); let superheader = SuperHeader::<KeyHeader>::new_for(&header);
println!("Writing: {:?} + {:?}", superheader, header); println!("Writing: {:?} + {:?}", superheader, header);
let written = superheader.write_text(&mut ser).await?; let written = superheader.write_text(&mut ser, None).await?;
ser.extend(header.into_memory(serialise::Mode::Text)?); //header.write_text(&mut ser).await?; ser.extend(header.into_memory(serialise::Mode::Text, None)?); //header.write_text(&mut ser).await?;
println!("Wrote {} bytes", written); println!("Wrote {} bytes", written);
println!("{}\n", ser.fmt_view()); println!("{}\n", ser.fmt_view());
let mut read = &ser[..]; let mut read = &ser[..];
let (reads, readn) = SuperHeader::from_memory(&mut read, serialise::Mode::Text)?; // SuperHeader::read_text(read).await?; let (reads, readn) = SuperHeader::from_memory(&mut read, serialise::Mode::Text, |_| None)?; // SuperHeader::read_text(read).await?;
let mut read = &read[readn..]; let mut read = &read[readn..];
println!("Read super: {:?}", reads); println!("Read super: {:?}", reads);
let readheader = KeyHeader::read_text(&mut read).await?; let readheader = KeyHeader::read_text(&mut read, |_| None).await?;
println!("Read real: {:?}", readheader); println!("Read real: {:?}", readheader);
reads.verify_for(&header)?; reads.verify_for(&header)?;

@ -20,7 +20,7 @@ use tokio::{
}, },
}; };
use version::Version; use version::Version;
use crypto::password::{Password,Salt};
/// Trait RAE headers implement /// Trait RAE headers implement
pub trait Header: fmt::Debug pub trait Header: fmt::Debug
@ -73,7 +73,7 @@ impl<H: Header +?Sized> SuperHeader<H>
{ {
/// Write this superheader as text bytes to this stream /// Write this superheader as text bytes to this stream
#[instrument(err, skip(out))] #[instrument(err, skip(out))]
pub async fn write_text<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T) -> Result<usize, eyre::Report> pub async fn write_text<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T, passwd: Option<&Password>) -> Result<usize, eyre::Report>
{ {
let string = format!(r#"--- {} v{}-{:x} ({}) {} --- let string = format!(r#"--- {} v{}-{:x} ({}) {} ---
"#, std::str::from_utf8(&self.head[..]) "#, std::str::from_utf8(&self.head[..])
@ -88,8 +88,8 @@ impl<H: Header +?Sized> SuperHeader<H>
Ok(string.len()) Ok(string.len())
} }
/// Read a superheader as text bytes from this stream /// Read a superheader as text bytes from this stream
#[instrument(err, skip(input))] #[instrument(err, skip(input, passwd))]
pub async fn read_text<T: AsyncBufRead+Unpin+?Sized>(input: &mut T) -> Result<Self, eyre::Report> pub async fn read_text<T: AsyncBufRead+Unpin+?Sized, F: FnOnce(&Salt) -> Option<Password>>(input: &mut T, passwd: F) -> Result<Self, eyre::Report>
{ {
let mut line = String::new(); let mut line = String::new();
input.read_line(&mut line).await?; input.read_line(&mut line).await?;
@ -198,7 +198,7 @@ impl<H: Header +?Sized> SuperHeader<H>
/// Write this superheader as bytes to this stream /// Write this superheader as bytes to this stream
#[instrument(err, skip(out))] #[instrument(err, skip(out))]
pub async fn write_bytes<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T) -> Result<usize, eyre::Report> pub async fn write_bytes<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T, passwd: Option<&Password>) -> Result<usize, eyre::Report>
{ {
Ok({out.write_all(&self.head[..]).await?; self.head.len()} + Ok({out.write_all(&self.head[..]).await?; self.head.len()} +
{out.write_all(self.vers.as_bytes()).await?; std::mem::size_of::<Version>()} + {out.write_all(self.vers.as_bytes()).await?; std::mem::size_of::<Version>()} +
@ -206,8 +206,8 @@ impl<H: Header +?Sized> SuperHeader<H>
{out.write_all(self.header_hash.as_ref()).await?; std::mem::size_of::<crypto::sha256::Sha256Hash>()}) {out.write_all(self.header_hash.as_ref()).await?; std::mem::size_of::<crypto::sha256::Sha256Hash>()})
} }
/// Read a superheader as bytes from this stream /// Read a superheader as bytes from this stream
#[instrument(err, skip(input))] #[instrument(err, skip(input, passwd))]
pub async fn read_bytes<T: AsyncRead+Unpin+?Sized>(input: &mut T) -> Result<Self, eyre::Report> pub async fn read_bytes<T: AsyncRead+Unpin+?Sized, F: FnOnce(&Salt) -> Option<Password>>(input: &mut T, passwd: F) -> Result<Self, eyre::Report>
{ {
let mut new = Self::new(); let mut new = Self::new();
input.read_exact(&mut new.head[..]).await?; input.read_exact(&mut new.head[..]).await?;
@ -323,27 +323,27 @@ impl<H: Header+?Sized> fmt::Display for VerificationError<H>
impl<H: Header+?Sized> serialise::BinarySerialisable for SuperHeader<H> impl<H: Header+?Sized> serialise::BinarySerialisable for SuperHeader<H>
{ {
#[inline(always)] fn serialise_bytes<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T) -> LocalBoxFuture<'a, Result<usize, eyre::Report>> #[inline(always)] fn serialise_bytes<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T, passwd: Option<&'b Password>) -> LocalBoxFuture<'a, Result<usize, eyre::Report>>
{ {
self.write_bytes(out).boxed_local() self.write_bytes(out, passwd).boxed_local()
} }
#[inline(always)] fn deserialise_bytes<'b , T: AsyncRead+Unpin+?Sized>(input: &'b mut T) -> LocalBoxFuture<'b, Result<Self, eyre::Report>> #[inline(always)] fn deserialise_bytes<'b , T: AsyncRead+Unpin+?Sized, F: for<'r> FnOnce(&'r Salt) -> Option<Password> + 'b>(input: &'b mut T, passwd: F) -> LocalBoxFuture<'b, Result<Self, eyre::Report>>
where Self: 'b where Self: 'b
{ {
Self::read_bytes(input).boxed_local() Self::read_bytes(input, passwd).boxed_local()
} }
} }
impl<H: Header+?Sized> serialise::TextSerialiseable for SuperHeader<H> impl<H: Header+?Sized> serialise::TextSerialiseable for SuperHeader<H>
{ {
#[inline(always)] fn serialise_text<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T) -> LocalBoxFuture<'a, Result<usize, eyre::Report>> #[inline(always)] fn serialise_text<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T, passwd: Option<&'b Password>) -> LocalBoxFuture<'a, Result<usize, eyre::Report>>
{ {
self.write_text(out).boxed_local() self.write_text(out, passwd).boxed_local()
} }
#[inline(always)] fn deserialise_text<'a, T: AsyncBufRead+Unpin+?Sized>(input: &'a mut T) -> LocalBoxFuture<'a, Result<Self, eyre::Report>> #[inline(always)] fn deserialise_text<'a, T: AsyncBufRead+Unpin+?Sized, F: for<'r> FnOnce(&'r Salt) -> Option<Password> + 'a>(input: &'a mut T, passwd: F) -> LocalBoxFuture<'a, Result<Self, eyre::Report>>
where Self: 'a where Self: 'a
{ {
Self::read_text(input).boxed_local() Self::read_text(input, passwd).boxed_local()
} }
} }

@ -148,16 +148,16 @@ async fn fuck() -> eyre::Result<()>
let mut ser = Vec::new(); let mut ser = Vec::new();
let superheader = SuperHeader::<KeyHeader>::new_for(&header); let superheader = SuperHeader::<KeyHeader>::new_for(&header);
println!("Writing: {:?} + {:?}", superheader, header); println!("Writing: {:?} + {:?}", superheader, header);
let written = superheader.write_text(&mut ser).await?; let written = superheader.write_text(&mut ser, None).await?;
ser.extend(header.into_memory(serialise::Mode::Text)?); //header.write_text(&mut ser).await?; ser.extend(header.into_memory(serialise::Mode::Text, None)?); //header.write_text(&mut ser).await?;
println!("Wrote {} bytes", written); println!("Wrote {} bytes", written);
println!("{}\n", ser.fmt_view()); println!("{}\n", ser.fmt_view());
let mut read = &ser[..]; let mut read = &ser[..];
let (reads, readn) = SuperHeader::from_memory(&mut read, serialise::Mode::Text)?; // SuperHeader::read_text(read).await?; let (reads, readn) = SuperHeader::from_memory(&mut read, serialise::Mode::Text, |_| None)?; // SuperHeader::read_text(read).await?;
let mut read = &read[readn..]; let mut read = &read[readn..];
println!("Read super: {:?}", reads); println!("Read super: {:?}", reads);
let readheader = KeyHeader::read_text(&mut read).await?; let readheader = KeyHeader::read_text(&mut read, |_| None).await?;
println!("Read real: {:?}", readheader); println!("Read real: {:?}", readheader);
reads.verify_for(&header)?; reads.verify_for(&header)?;

@ -5,13 +5,23 @@ use std::{
marker::Unpin, marker::Unpin,
fmt, fmt,
}; };
use crypto::{
password::{
Salt,
Password,
},
};
///// The callback to get a derrived password
//pub trait PasswordFn: for<'r> FnOnce(&'r Salt) -> Option<Password>{}
//impl<T: for<'r> FnOnce(&'r Salt) -> Option<Password>> PasswordFn for T{}
// ^ -- super dumb awful unhelpful error messages trying to do this, so fuck it copy & paste it is
pub trait TextSerialiseable: Sized pub trait TextSerialiseable: Sized
{ {
fn serialise_text<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T) -> LocalBoxFuture<'a, Result<usize, eyre::Report>>; fn serialise_text<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T, passwd: Option<&'b Password>) -> LocalBoxFuture<'a, Result<usize, eyre::Report>>;
fn deserialise_text<'a, T: AsyncBufRead+Unpin+?Sized>(input: &'a mut T) -> LocalBoxFuture<'a, Result<Self, eyre::Report>> fn deserialise_text<'a, T: AsyncBufRead+Unpin+?Sized, F: for<'r> FnOnce(&'r Salt) -> Option<Password> + 'a>(input: &'a mut T, passwd: F) -> LocalBoxFuture<'a, Result<Self, eyre::Report>>
where Self: 'a; where Self: 'a;
#[inline] fn size_hint(&self) -> (usize, Option<usize>) #[inline] fn size_hint(&self) -> (usize, Option<usize>)
{ {
(0,None) (0,None)
@ -21,9 +31,9 @@ pub trait TextSerialiseable: Sized
pub trait BinarySerialisable: Sized pub trait BinarySerialisable: Sized
{ {
fn serialise_bytes<'a, 'b, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T) -> LocalBoxFuture<'a, Result<usize, eyre::Report>> fn serialise_bytes<'a, 'b, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T, passwd: Option<&'b Password>) -> LocalBoxFuture<'a, Result<usize, eyre::Report>>
where 'b: 'a; where 'b: 'a;
fn deserialise_bytes<'b , T: AsyncRead+Unpin+?Sized>(input: &'b mut T) -> LocalBoxFuture<'b, Result<Self, eyre::Report>> fn deserialise_bytes<'b , T: AsyncRead+Unpin+?Sized, F: for<'r> FnOnce(&'r Salt) -> Option<Password> + 'b>(input: &'b mut T, passwd: F) -> LocalBoxFuture<'b, Result<Self, eyre::Report>>
where Self: 'b; where Self: 'b;
#[inline] fn size_hint(&self) -> (usize, Option<usize>) #[inline] fn size_hint(&self) -> (usize, Option<usize>)
@ -42,7 +52,7 @@ pub trait Serialisable: BinarySerialisable + TextSerialiseable
/// # Notes /// # Notes
/// This function will fail if any `await`ed future within the `serialise_*` method cannot complete immediately. /// This function will fail if any `await`ed future within the `serialise_*` method cannot complete immediately.
#[instrument(skip(self))] #[instrument(skip(self))]
fn into_memory(&self, mode: Mode) -> Result<Vec<u8>, eyre::Report> fn into_memory(&self, mode: Mode, passwd: Option<&Password>) -> Result<Vec<u8>, eyre::Report>
{ {
let mut output = match mode.size_hint(self) let mut output = match mode.size_hint(self)
{ {
@ -52,8 +62,8 @@ pub trait Serialisable: BinarySerialisable + TextSerialiseable
trace!("Cap for membuf is {} bytes", output.capacity()); trace!("Cap for membuf is {} bytes", output.capacity());
let note = || format!("While serialising {} into memory", std::any::type_name::<Self>()); let note = || format!("While serialising {} into memory", std::any::type_name::<Self>());
let written = match mode { let written = match mode {
Mode::Text => self.serialise_text(&mut output).now_or_never(), Mode::Text => self.serialise_text(&mut output, passwd).now_or_never(),
Mode::Binary => self.serialise_bytes(&mut output).now_or_never(), Mode::Binary => self.serialise_bytes(&mut output, passwd).now_or_never(),
} }
.ok_or_else(|| eyre::eyre!("Failed to complete {} write syncronously. A future returned `Poll::Pending`.", mode)) .ok_or_else(|| eyre::eyre!("Failed to complete {} write syncronously. A future returned `Poll::Pending`.", mode))
.with_note(note.clone())? .with_note(note.clone())?
@ -72,8 +82,8 @@ pub trait Serialisable: BinarySerialisable + TextSerialiseable
/// ///
/// # Notes /// # Notes
/// This function will fail if any `await`ed future within the `deserialise_*` method cannot complete immediately. /// This function will fail if any `await`ed future within the `deserialise_*` method cannot complete immediately.
#[instrument(skip(buf),fields(buf = ?buf.as_ref()))] #[instrument(skip(buf, passwd),fields(buf = ?buf.as_ref()))]
fn from_memory<T: AsRef<[u8]>>(buf: T, mode: Mode) -> Result<(Self, usize), eyre::Report> fn from_memory<T: AsRef<[u8]>, F: FnOnce(&Salt) -> Option<Password>>(buf: T, mode: Mode, passwd: F) -> Result<(Self, usize), eyre::Report>
{ {
let buf = buf.as_ref(); let buf = buf.as_ref();
let mut reader = &buf[..]; let mut reader = &buf[..];
@ -83,8 +93,8 @@ pub trait Serialisable: BinarySerialisable + TextSerialiseable
let bytes = || format!("{}", buf.fmt_view()).header("Binary buffer was"); let bytes = || format!("{}", buf.fmt_view()).header("Binary buffer was");
let text = || String::from_utf8_lossy(buf).into_owned().header("Text buffer was"); let text = || String::from_utf8_lossy(buf).into_owned().header("Text buffer was");
let value = match mode { let value = match mode {
Mode::Binary => Self::deserialise_bytes(&mut reader).now_or_never(), Mode::Binary => Self::deserialise_bytes(&mut reader, passwd).now_or_never(),
Mode::Text => Self::deserialise_text(&mut reader).now_or_never(), Mode::Text => Self::deserialise_text(&mut reader, passwd).now_or_never(),
} }
.ok_or_else(|| eyre::eyre!("Failed to complete {} read syncronously. A future returned `Poll::Pending`.", mode)) .ok_or_else(|| eyre::eyre!("Failed to complete {} read syncronously. A future returned `Poll::Pending`.", mode))
.with_note(note.clone())? .with_note(note.clone())?

Loading…
Cancel
Save