Skip to content

Commit

Permalink
start factoring out value-bag
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Jan 29, 2024
1 parent fdc1c6e commit 7f93aca
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 98 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ std = []

# requires the latest stable
# this will have a tighter MSRV before stabilization
kv_unstable = ["value-bag"]
kv_unstable = []
kv_unstable_sval = ["kv_unstable", "value-bag/sval", "sval", "sval_ref"]
kv_unstable_std = ["std", "kv_unstable", "value-bag/error"]
kv_unstable_serde = ["kv_unstable_std", "value-bag/serde", "serde"]
Expand Down
14 changes: 7 additions & 7 deletions src/kv/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ mod std_support {
mod tests {
use std::collections::{BTreeMap, HashMap};

use crate::kv::value::tests::Token;
use crate::kv::value;

use super::*;

Expand All @@ -377,7 +377,7 @@ mod std_support {
fn get() {
let source = vec![("a", 1), ("b", 2), ("a", 1)];
assert_eq!(
Token::I64(1),
value::inner::Token::I64(1),
Source::get(&source, Key::from_str("a")).unwrap().to_token()
);

Expand All @@ -393,7 +393,7 @@ mod std_support {

assert_eq!(2, Source::count(&map));
assert_eq!(
Token::I64(1),
value::inner::Token::I64(1),
Source::get(&map, Key::from_str("a")).unwrap().to_token()
);
}
Expand All @@ -406,7 +406,7 @@ mod std_support {

assert_eq!(2, Source::count(&map));
assert_eq!(
Token::I64(1),
value::inner::Token::I64(1),
Source::get(&map, Key::from_str("a")).unwrap().to_token()
);
}
Expand All @@ -415,7 +415,7 @@ mod std_support {

#[cfg(test)]
mod tests {
use crate::kv::value::tests::Token;
use crate::kv::value;

use super::*;

Expand Down Expand Up @@ -452,11 +452,11 @@ mod tests {
fn get() {
let source = &[("a", 1), ("b", 2), ("a", 1)] as &[_];
assert_eq!(
Token::I64(1),
value::inner::Token::I64(1),
Source::get(source, Key::from_str("a")).unwrap().to_token()
);
assert_eq!(
Token::I64(2),
value::inner::Token::I64(2),
Source::get(source, Key::from_str("b")).unwrap().to_token()
);
assert!(Source::get(&source, Key::from_str("c")).is_none());
Expand Down
211 changes: 121 additions & 90 deletions src/kv/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use std::fmt;

pub use crate::kv::Error;

use value_bag::ValueBag;

/// A type that can be converted into a [`Value`](struct.Value.html).
pub trait ToValue {
/// Perform the conversion.
Expand Down Expand Up @@ -101,7 +99,7 @@ impl<'v> ToValue for Value<'v> {
/// as a struct using its `serde::Serialize`, it will also serialize as a struct
/// through `sval`, or be formatted using a `Debug`-compatible representation.
pub struct Value<'v> {
inner: ValueBag<'v>,
inner: inner::Inner<'v>,
}

impl<'v> Value<'v> {
Expand All @@ -119,7 +117,7 @@ impl<'v> Value<'v> {
T: fmt::Debug,
{
Value {
inner: ValueBag::from_debug(value),
inner: inner::Inner::from_debug(value),
}
}

Expand All @@ -129,7 +127,7 @@ impl<'v> Value<'v> {
T: fmt::Display,
{
Value {
inner: ValueBag::from_display(value),
inner: inner::Inner::from_display(value),
}
}

Expand All @@ -140,7 +138,7 @@ impl<'v> Value<'v> {
T: serde::Serialize,
{
Value {
inner: ValueBag::from_serde1(value),
inner: inner::Inner::from_serde1(value),
}
}

Expand All @@ -151,36 +149,36 @@ impl<'v> Value<'v> {
T: sval::Value,
{
Value {
inner: ValueBag::from_sval2(value),
inner: inner::Inner::from_sval2(value),
}
}

/// Get a value from a dynamic `std::fmt::Debug`.
pub fn from_dyn_debug(value: &'v dyn fmt::Debug) -> Self {
Value {
inner: ValueBag::from_dyn_debug(value),
inner: inner::Inner::from_dyn_debug(value),
}
}

/// Get a value from a dynamic `std::fmt::Display`.
pub fn from_dyn_display(value: &'v dyn fmt::Display) -> Self {
Value {
inner: ValueBag::from_dyn_display(value),
inner: inner::Inner::from_dyn_display(value),
}
}

/// Get a value from a dynamic error.
#[cfg(feature = "kv_unstable_std")]
pub fn from_dyn_error(err: &'v (dyn std::error::Error + 'static)) -> Self {
Value {
inner: ValueBag::from_dyn_error(err),
inner: inner::Inner::from_dyn_error(err),
}
}

/// Get a value from an internal primitive.
fn from_value_bag<T>(value: T) -> Self
where
T: Into<ValueBag<'v>>,
T: Into<inner::Inner<'v>>,
{
Value {
inner: value.into(),
Expand All @@ -192,74 +190,7 @@ impl<'v> Value<'v> {
/// When the `kv_unstable_serde` or `kv_unstable_sval` features are enabled, you can also
/// serialize a value using its `Serialize` or `Value` implementation.
pub fn visit(&self, visitor: impl Visitor<'v>) -> Result<(), Error> {
struct Visitor<V>(V);

impl<'v, V> value_bag::visit::Visitor<'v> for Visitor<V>
where
V: Visitor<'v>,
{
fn visit_any(&mut self, value: ValueBag) -> Result<(), value_bag::Error> {
self.0
.visit_any(Value { inner: value })
.map_err(Error::into_value)
}

fn visit_u64(&mut self, value: u64) -> Result<(), value_bag::Error> {
self.0.visit_u64(value).map_err(Error::into_value)
}

fn visit_i64(&mut self, value: i64) -> Result<(), value_bag::Error> {
self.0.visit_i64(value).map_err(Error::into_value)
}

fn visit_u128(&mut self, value: u128) -> Result<(), value_bag::Error> {
self.0.visit_u128(value).map_err(Error::into_value)
}

fn visit_i128(&mut self, value: i128) -> Result<(), value_bag::Error> {
self.0.visit_i128(value).map_err(Error::into_value)
}

fn visit_f64(&mut self, value: f64) -> Result<(), value_bag::Error> {
self.0.visit_f64(value).map_err(Error::into_value)
}

fn visit_bool(&mut self, value: bool) -> Result<(), value_bag::Error> {
self.0.visit_bool(value).map_err(Error::into_value)
}

fn visit_str(&mut self, value: &str) -> Result<(), value_bag::Error> {
self.0.visit_str(value).map_err(Error::into_value)
}

fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), value_bag::Error> {
self.0.visit_borrowed_str(value).map_err(Error::into_value)
}

fn visit_char(&mut self, value: char) -> Result<(), value_bag::Error> {
self.0.visit_char(value).map_err(Error::into_value)
}

#[cfg(feature = "kv_unstable_std")]
fn visit_error(
&mut self,
err: &(dyn std::error::Error + 'static),
) -> Result<(), value_bag::Error> {
self.0.visit_error(err).map_err(Error::into_value)
}

#[cfg(feature = "kv_unstable_std")]
fn visit_borrowed_error(
&mut self,
err: &'v (dyn std::error::Error + 'static),
) -> Result<(), value_bag::Error> {
self.0.visit_borrowed_error(err).map_err(Error::into_value)
}
}

self.inner
.visit(&mut Visitor(visitor))
.map_err(Error::from_value)
inner::visit(&self.inner, visitor)
}
}

Expand Down Expand Up @@ -661,13 +592,113 @@ where
}
}

#[cfg(feature = "value-bag")]
pub(in crate::kv) mod inner {
use super::*;

pub use value_bag::ValueBag as Inner;

#[cfg(test)]
pub use value_bag::test::TestToken as Token;

#[cfg(test)]
pub fn to_test_token<'v>(inner: &Inner<'v>) -> Token {
inner.to_test_token()
}

pub fn visit<'v>(inner: &Inner<'v>, visitor: impl Visitor<'v>) -> Result<(), Error> {
struct InnerVisitor<V>(V);

impl<'v, V> value_bag::visit::Visit<'v> for InnerVisitor<V>
where
V: Visitor<'v>,
{
fn visit_any(&mut self, value: value_bag::ValueBag) -> Result<(), value_bag::Error> {
self.0
.visit_any(Value { inner: value })
.map_err(Error::into_value)
}

fn visit_u64(&mut self, value: u64) -> Result<(), value_bag::Error> {
self.0.visit_u64(value).map_err(Error::into_value)
}

fn visit_i64(&mut self, value: i64) -> Result<(), value_bag::Error> {
self.0.visit_i64(value).map_err(Error::into_value)
}

fn visit_u128(&mut self, value: u128) -> Result<(), value_bag::Error> {
self.0.visit_u128(value).map_err(Error::into_value)
}

fn visit_i128(&mut self, value: i128) -> Result<(), value_bag::Error> {
self.0.visit_i128(value).map_err(Error::into_value)
}

fn visit_f64(&mut self, value: f64) -> Result<(), value_bag::Error> {
self.0.visit_f64(value).map_err(Error::into_value)
}

fn visit_bool(&mut self, value: bool) -> Result<(), value_bag::Error> {
self.0.visit_bool(value).map_err(Error::into_value)
}

fn visit_str(&mut self, value: &str) -> Result<(), value_bag::Error> {
self.0.visit_str(value).map_err(Error::into_value)
}

fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), value_bag::Error> {
self.0.visit_borrowed_str(value).map_err(Error::into_value)
}

fn visit_char(&mut self, value: char) -> Result<(), value_bag::Error> {
self.0.visit_char(value).map_err(Error::into_value)
}

#[cfg(feature = "kv_unstable_std")]
fn visit_error(
&mut self,
err: &(dyn std::error::Error + 'static),
) -> Result<(), value_bag::Error> {
self.0.visit_error(err).map_err(Error::into_value)
}

#[cfg(feature = "kv_unstable_std")]
fn visit_borrowed_error(
&mut self,
err: &'v (dyn std::error::Error + 'static),
) -> Result<(), value_bag::Error> {
self.0.visit_borrowed_error(err).map_err(Error::into_value)
}
}

inner
.visit(&mut InnerVisitor(visitor))
.map_err(Error::from_value)
}
}

#[cfg(not(feature = "value-bag"))]
pub(in crate::kv) mod inner {
use super::*;

pub enum Inner<'v> {

}

pub enum Token {}

pub fn visit(inner: &Inner<'v>, visitor: impl Visitor<'v>) -> Result<(), Error> {
todo!()
}
}

#[cfg(test)]
pub(crate) mod tests {
use super::*;
pub(crate) use value_bag::test::TestToken as Token;

impl<'v> Value<'v> {
pub(crate) fn to_token(&self) -> Token {
pub(crate) fn to_token(&self) -> inner::Token {
self.inner.to_test_token()
}
}
Expand Down Expand Up @@ -735,18 +766,18 @@ pub(crate) mod tests {

#[test]
fn test_to_value_structured() {
assert_eq!(42u64.to_value().to_token(), Token::U64(42));
assert_eq!(42i64.to_value().to_token(), Token::I64(42));
assert_eq!(42.01f64.to_value().to_token(), Token::F64(42.01));
assert_eq!(true.to_value().to_token(), Token::Bool(true));
assert_eq!('a'.to_value().to_token(), Token::Char('a'));
assert_eq!(42u64.to_value().to_token(), inner::Token::U64(42));
assert_eq!(42i64.to_value().to_token(), inner::Token::I64(42));
assert_eq!(42.01f64.to_value().to_token(), inner::Token::F64(42.01));
assert_eq!(true.to_value().to_token(), inner::Token::Bool(true));
assert_eq!('a'.to_value().to_token(), inner::Token::Char('a'));
assert_eq!(
"a loong string".to_value().to_token(),
Token::Str("a loong string".into())
inner::Token::Str("a loong string".into())
);
assert_eq!(Some(true).to_value().to_token(), Token::Bool(true));
assert_eq!(().to_value().to_token(), Token::None);
assert_eq!(None::<bool>.to_value().to_token(), Token::None);
assert_eq!(Some(true).to_value().to_token(), inner::Token::Bool(true));
assert_eq!(().to_value().to_token(), inner::Token::None);
assert_eq!(None::<bool>.to_value().to_token(), inner::Token::None);
}

#[test]
Expand Down

0 comments on commit 7f93aca

Please sign in to comment.