respect fmt trait and flags in Value

This commit is contained in:
Ashley Mannix 2020-06-04 21:12:31 +10:00
parent c5c8e48f35
commit ee8c6cd426
4 changed files with 228 additions and 89 deletions

View File

@ -145,4 +145,20 @@ mod tests {
.expect("invalid value")
);
}
#[test]
fn fill_debug() {
struct TestFill;
impl Fill for TestFill {
fn fill(&self, slot: &mut Slot) -> Result<(), Error> {
slot.fill_any(42u64)
}
}
assert_eq!(
format!("{:04?}", 42u64),
format!("{:04?}", Value::from_fill(&TestFill)),
)
}
}

View File

@ -126,14 +126,14 @@ mod tests {
assert_eq!(42i64.to_value().to_string(), "42");
assert_eq!(42.01f64.to_value().to_string(), "42.01");
assert_eq!(true.to_value().to_string(), "true");
assert_eq!('a'.to_value().to_string(), "'a'");
assert_eq!('a'.to_value().to_string(), "a");
assert_eq!(
format_args!("a {}", "value").to_value().to_string(),
"a value"
);
assert_eq!(
"a loong string".to_value().to_string(),
"\"a loong string\""
"a loong string"
);
assert_eq!(Some(true).to_value().to_string(), "true");
assert_eq!(().to_value().to_string(), "None");

View File

@ -65,51 +65,55 @@ pub(in kv::value) use self::fmt::{Arguments, Debug, Display};
impl<'v> fmt::Debug for kv::Value<'v> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.visit(&mut FmtVisitor(f)).map_err(|_| fmt::Error)?;
struct DebugVisitor<'a, 'b: 'a>(&'a mut fmt::Formatter<'b>);
Ok(())
}
}
impl<'v> fmt::Display for kv::Value<'v> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.visit(&mut FmtVisitor(f)).map_err(|_| fmt::Error)?;
Ok(())
}
}
struct FmtVisitor<'a, 'b: 'a>(&'a mut fmt::Formatter<'b>);
impl<'a, 'b: 'a, 'v> Visitor<'v> for FmtVisitor<'a, 'b> {
impl<'a, 'b: 'a, 'v> Visitor<'v> for DebugVisitor<'a, 'b> {
fn debug(&mut self, v: &dyn fmt::Debug) -> Result<(), Error> {
v.fmt(self.0)?;
fmt::Debug::fmt(v, self.0)?;
Ok(())
}
fn display(&mut self, v: &dyn fmt::Display) -> Result<(), Error> {
fmt::Display::fmt(v, self.0)?;
Ok(())
}
fn u64(&mut self, v: u64) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
fmt::Debug::fmt(&v, self.0)?;
Ok(())
}
fn i64(&mut self, v: i64) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
fmt::Debug::fmt(&v, self.0)?;
Ok(())
}
fn f64(&mut self, v: f64) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
fmt::Debug::fmt(&v, self.0)?;
Ok(())
}
fn bool(&mut self, v: bool) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
fmt::Debug::fmt(&v, self.0)?;
Ok(())
}
fn char(&mut self, v: char) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
fmt::Debug::fmt(&v, self.0)?;
Ok(())
}
fn str(&mut self, v: &str) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
fmt::Debug::fmt(&v, self.0)?;
Ok(())
}
fn none(&mut self) -> Result<(), Error> {
@ -120,12 +124,89 @@ impl<'a, 'b: 'a, 'v> Visitor<'v> for FmtVisitor<'a, 'b> {
fn sval(&mut self, v: &dyn super::sval::Value) -> Result<(), Error> {
super::sval::fmt(self.0, v)
}
}
self.visit(&mut DebugVisitor(f)).map_err(|_| fmt::Error)?;
Ok(())
}
}
impl<'v> fmt::Display for kv::Value<'v> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
struct DisplayVisitor<'a, 'b: 'a>(&'a mut fmt::Formatter<'b>);
impl<'a, 'b: 'a, 'v> Visitor<'v> for DisplayVisitor<'a, 'b> {
fn debug(&mut self, v: &dyn fmt::Debug) -> Result<(), Error> {
fmt::Debug::fmt(v, self.0)?;
Ok(())
}
fn display(&mut self, v: &dyn fmt::Display) -> Result<(), Error> {
fmt::Display::fmt(v, self.0)?;
Ok(())
}
fn u64(&mut self, v: u64) -> Result<(), Error> {
fmt::Display::fmt(&v, self.0)?;
Ok(())
}
fn i64(&mut self, v: i64) -> Result<(), Error> {
fmt::Display::fmt(&v, self.0)?;
Ok(())
}
fn f64(&mut self, v: f64) -> Result<(), Error> {
fmt::Display::fmt(&v, self.0)?;
Ok(())
}
fn bool(&mut self, v: bool) -> Result<(), Error> {
fmt::Display::fmt(&v, self.0)?;
Ok(())
}
fn char(&mut self, v: char) -> Result<(), Error> {
fmt::Display::fmt(&v, self.0)?;
Ok(())
}
fn str(&mut self, v: &str) -> Result<(), Error> {
fmt::Display::fmt(&v, self.0)?;
Ok(())
}
fn none(&mut self) -> Result<(), Error> {
self.debug(&format_args!("None"))
}
#[cfg(feature = "kv_unstable_sval")]
fn sval(&mut self, v: &dyn super::sval::Value) -> Result<(), Error> {
super::sval::fmt(self.0, v)
}
}
self.visit(&mut DisplayVisitor(f)).map_err(|_| fmt::Error)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::kv::value::ToValue;
#[test]
fn fmt_cast() {
assert_eq!(
@ -142,4 +223,30 @@ mod tests {
.expect("invalid value")
);
}
#[test]
fn fmt_debug() {
assert_eq!(
format!("{:?}", "a string"),
format!("{:?}", "a string".to_value()),
);
assert_eq!(
format!("{:04?}", 42u64),
format!("{:04?}", 42u64.to_value()),
);
}
#[test]
fn fmt_display() {
assert_eq!(
format!("{}", "a string"),
format!("{}", "a string".to_value()),
);
assert_eq!(
format!("{:04}", 42u64),
format!("{:04}", 42u64.to_value()),
);
}
}

View File

@ -42,6 +42,48 @@ impl<'s, 'f> Slot<'s, 'f> {
impl<'v> sval::Value for kv::Value<'v> {
fn stream(&self, s: &mut sval::value::Stream) -> sval::value::Result {
struct SvalVisitor<'a, 'b: 'a>(&'a mut sval::value::Stream<'b>);
impl<'a, 'b: 'a, 'v> Visitor<'v> for SvalVisitor<'a, 'b> {
fn debug(&mut self, v: &dyn fmt::Debug) -> Result<(), Error> {
self.0
.fmt(format_args!("{:?}", v))
.map_err(Error::from_sval)
}
fn u64(&mut self, v: u64) -> Result<(), Error> {
self.0.u64(v).map_err(Error::from_sval)
}
fn i64(&mut self, v: i64) -> Result<(), Error> {
self.0.i64(v).map_err(Error::from_sval)
}
fn f64(&mut self, v: f64) -> Result<(), Error> {
self.0.f64(v).map_err(Error::from_sval)
}
fn bool(&mut self, v: bool) -> Result<(), Error> {
self.0.bool(v).map_err(Error::from_sval)
}
fn char(&mut self, v: char) -> Result<(), Error> {
self.0.char(v).map_err(Error::from_sval)
}
fn str(&mut self, v: &str) -> Result<(), Error> {
self.0.str(v).map_err(Error::from_sval)
}
fn none(&mut self) -> Result<(), Error> {
self.0.none().map_err(Error::from_sval)
}
fn sval(&mut self, v: &dyn sval::Value) -> Result<(), Error> {
self.0.any(v).map_err(Error::from_sval)
}
}
self.visit(&mut SvalVisitor(s)).map_err(Error::into_sval)?;
Ok(())
@ -107,48 +149,6 @@ impl Error {
}
}
struct SvalVisitor<'a, 'b: 'a>(&'a mut sval::value::Stream<'b>);
impl<'a, 'b: 'a, 'v> Visitor<'v> for SvalVisitor<'a, 'b> {
fn debug(&mut self, v: &dyn fmt::Debug) -> Result<(), Error> {
self.0
.fmt(format_args!("{:?}", v))
.map_err(Error::from_sval)
}
fn u64(&mut self, v: u64) -> Result<(), Error> {
self.0.u64(v).map_err(Error::from_sval)
}
fn i64(&mut self, v: i64) -> Result<(), Error> {
self.0.i64(v).map_err(Error::from_sval)
}
fn f64(&mut self, v: f64) -> Result<(), Error> {
self.0.f64(v).map_err(Error::from_sval)
}
fn bool(&mut self, v: bool) -> Result<(), Error> {
self.0.bool(v).map_err(Error::from_sval)
}
fn char(&mut self, v: char) -> Result<(), Error> {
self.0.char(v).map_err(Error::from_sval)
}
fn str(&mut self, v: &str) -> Result<(), Error> {
self.0.str(v).map_err(Error::from_sval)
}
fn none(&mut self) -> Result<(), Error> {
self.0.none().map_err(Error::from_sval)
}
fn sval(&mut self, v: &dyn sval::Value) -> Result<(), Error> {
self.0.any(v).map_err(Error::from_sval)
}
}
#[cfg(test)]
mod tests {
use super::*;
@ -191,4 +191,20 @@ mod tests {
.expect("invalid value")
);
}
#[test]
fn sval_debug() {
struct TestSval;
impl sval::Value for TestSval {
fn stream(&self, stream: &mut sval::value::Stream) -> sval::value::Result {
stream.u64(42)
}
}
assert_eq!(
format!("{:04?}", 42u64),
format!("{:04?}", kv::Value::from_sval(&TestSval)),
);
}
}