Skip to content

Commit

Permalink
Merge pull request #230 from quartiq/misc
Browse files Browse the repository at this point in the history
misc
  • Loading branch information
jordens authored Jul 19, 2024
2 parents 70a01b6 + 142c7cc commit f7c2511
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
4 changes: 4 additions & 0 deletions miniconf/src/json_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,7 @@ impl<'de, T: TreeSerialize<Y> + TreeDeserialize<'de, Y> + ?Sized, const Y: usize
Ok(ser.end())
}
}

/// Shorthand for owned deserialization through [`JsonCoreSlash`].
pub trait JsonCoreSlashOwned<const Y: usize = 1>: for<'de> JsonCoreSlash<'de, Y> {}
impl<T, const Y: usize> JsonCoreSlashOwned<Y> for T where T: for<'de> JsonCoreSlash<'de, Y> {}
6 changes: 6 additions & 0 deletions miniconf/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ impl<const D: usize> From<Indices<[usize; D]>> for [usize; D] {
}
}

impl<T> From<T> for Indices<T> {
fn from(value: T) -> Self {
Self(value)
}
}

impl<'a, T: AsRef<[usize]> + ?Sized> IntoKeys for &'a Indices<T> {
type IntoKeys = KeysIter<Copied<Iter<'a, usize>>>;
fn into_keys(self) -> Self::IntoKeys {
Expand Down
4 changes: 4 additions & 0 deletions miniconf/src/postcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ impl<'de, T: TreeSerialize<Y> + TreeDeserialize<'de, Y> + ?Sized, const Y: usize
ser.output.finalize().map_err(Error::Finalization)
}
}

/// Shorthand for owned [`Postcard`].
pub trait PostcardOwned<const Y: usize = 1>: for<'de> Postcard<'de, Y> {}
impl<T, const Y: usize> PostcardOwned<Y> for T where T: for<'de> Postcard<'de, Y> {}
4 changes: 4 additions & 0 deletions miniconf/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,7 @@ pub trait TreeDeserialize<'de, const Y: usize = 1>: TreeKey<Y> {
K: Keys,
D: Deserializer<'de>;
}

/// Shorthand for owned deserialization through [`TreeDeserialize`].
pub trait TreeDeserializeOwned<const Y: usize = 1>: for<'de> TreeDeserialize<'de, Y> {}
impl<T, const Y: usize> TreeDeserializeOwned<Y> for T where T: for<'de> TreeDeserialize<'de, Y> {}
44 changes: 18 additions & 26 deletions miniconf_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,10 @@ use miniconf::{
struct WriteWrap<T>(T);

impl<T: Write> fmt::Write for WriteWrap<T> {
fn write_char(&mut self, c: char) -> fmt::Result {
let mut buf = [0; 4];
self.write_str(c.encode_utf8(&mut buf))
}

fn write_str(&mut self, s: &str) -> fmt::Result {
self.0.write_all(s.as_bytes()).or(Err(fmt::Error))?;
Ok(())
}

fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
self.0.write_fmt(args).or(Err(fmt::Error))?;
Ok(())
}
}

async fn awrite<W: AWrite>(mut write: W, buf: &[u8]) -> Result<(), Error<W::Error>> {
Expand All @@ -40,7 +30,7 @@ async fn awrite<W: AWrite>(mut write: W, buf: &[u8]) -> Result<(), Error<W::Erro
pub enum Error<I> {
Fmt(core::fmt::Error),
Traversal(miniconf::Traversal),
Serialize(serde_json_core::ser::Error),
Serialize(usize, serde_json_core::ser::Error),
Io(I),
}

Expand All @@ -53,9 +43,9 @@ impl<I> From<Traversal> for Error<I> {
impl<I> From<miniconf::Error<serde_json_core::ser::Error>> for Error<I> {
fn from(value: miniconf::Error<serde_json_core::ser::Error>) -> Self {
match value {
miniconf::Error::Inner(_depth, e) => Self::Serialize(e),
miniconf::Error::Inner(depth, e) => Self::Serialize(depth, e),
miniconf::Error::Traversal(e) => Self::Traversal(e),
miniconf::Error::Finalization(_) => unreachable!(),
miniconf::Error::Finalization(e) => Self::Serialize(0, e),
_ => unimplemented!(),
}
}
Expand Down Expand Up @@ -99,17 +89,17 @@ where
}

fn push(&self, path: &str) -> Result<(Self, Node), Traversal> {
let (key, node) = M::transcode(self.key.chain(path.split(SEPARATOR)))?;
let (key, node) = M::transcode(self.key.chain(&Path::<_, SEPARATOR>::from(path)))?;
Ok((Self::new(key), node))
}

fn pop(&self, levels: usize) -> Result<(Self, Node), Traversal> {
let (idx, node) = M::transcode::<Indices<[_; Y]>, _>(self.key)?;
if node.depth() < levels {
Err(Traversal::TooShort(node.depth()))
} else {
let (key, node) = M::transcode(idx[..node.depth() - levels].iter().copied())?;
if let Some(idx) = idx.get(..node.depth() - levels) {
let (key, node) = M::transcode(&Indices::from(idx))?;
Ok((Self::new(key), node))
} else {
Err(Traversal::TooShort(node.depth()))
}
}

Expand Down Expand Up @@ -165,6 +155,7 @@ where
{
let def = M::default();
for keys in M::nodes::<Packed>().root(self.key)? {
// Slight abuse of TooLong for "keys to long for packed"
let (keys, node) =
keys.map_err(|depth| miniconf::Error::Traversal(Traversal::TooLong(depth)))?;
debug_assert!(node.is_leaf());
Expand Down Expand Up @@ -235,11 +226,11 @@ where
ret => &buf[..ret?],
};
if yafnv::fnv1a::<u32, _>(def) == check {
awrite(&mut write, " (default)\n".as_bytes()).await?;
awrite(&mut write, " [default]\n".as_bytes()).await?;
} else {
awrite(&mut write, " (default: ".as_bytes()).await?;
awrite(&mut write, " [default: ".as_bytes()).await?;
awrite(&mut write, def).await?;
awrite(&mut write, ")\n".as_bytes()).await?;
awrite(&mut write, "]\n".as_bytes()).await?;
}
}
Ok(())
Expand Down Expand Up @@ -289,21 +280,22 @@ mod tests {
let mut stdout = embedded_io_adapters::tokio_1::FromTokio::new(tokio::io::stdout());
let mut menu = Menu::<Set, Y>::default();
s.c = Some(8);
menu.enter("b/0").unwrap();
menu.enter("/b").unwrap();
menu.enter("/0").unwrap();
menu.set(&mut s, b"1234").unwrap();
menu.exit(2).unwrap();
menu.push("f/1/e").unwrap().0.set(&mut s, b"9").unwrap();
menu.push("/f/1/e").unwrap().0.set(&mut s, b"9").unwrap();
let paths: Vec<String<128>> = menu.list().unwrap().map(Result::unwrap).collect();
stdout
.write_all(format!("{:?}\n", paths).as_bytes())
.await
.unwrap();
menu.dump(&s, &mut stdout, &mut buf).await.unwrap();
menu.enter("f").unwrap();
menu.enter("/f").unwrap();
menu.dump(&s, &mut stdout, &mut buf).await.unwrap();
menu.exit(1).unwrap();
menu.push("c").unwrap().0.reset(&mut s, &mut buf).unwrap();
menu.push("b").unwrap().0.reset(&mut s, &mut buf).unwrap();
menu.push("/c").unwrap().0.reset(&mut s, &mut buf).unwrap();
menu.push("/b").unwrap().0.reset(&mut s, &mut buf).unwrap();
menu.dump(&s, &mut stdout, &mut buf).await.unwrap();
}
}

0 comments on commit f7c2511

Please sign in to comment.