Skip to content

Commit

Permalink
document Visit (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
carllerche authored May 17, 2021
1 parent 437a60e commit fd0fb8e
Show file tree
Hide file tree
Showing 4 changed files with 437 additions and 13 deletions.
106 changes: 106 additions & 0 deletions valuable/examples/print.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use valuable::{NamedValues, Valuable, Value, Visit};

struct Print(String);

impl Print {
fn indent(&self) -> Print {
Print(format!("{} ", self.0))
}
}

impl Visit for Print {
fn visit_value(&mut self, value: Value<'_>) {
match value {
Value::Structable(v) => {
let def = v.definition();
// Print the struct name
println!("{}{}:", self.0, def.name());

// Visit fields
let mut visit = self.indent();
v.visit(&mut visit);
}
Value::Enumerable(v) => {
let def = v.definition();
let variant = v.variant();
// Print the enum name
println!("{}{}::{}:", self.0, def.name(), variant.name());

// Visit fields
let mut visit = self.indent();
v.visit(&mut visit);
}
Value::Listable(v) => {
println!("{}", self.0);

// Visit fields
let mut visit = self.indent();
v.visit(&mut visit);
}
Value::Mappable(v) => {
println!("{}", self.0);

// Visit fields
let mut visit = self.indent();
v.visit(&mut visit);
}
// Primitive or unknown type, just render Debug
v => println!("{:?}", v),
}
}

fn visit_named_fields(&mut self, named_values: &NamedValues<'_>) {
for (field, value) in named_values.entries() {
print!("{}- {}: ", self.0, field.name());
value.visit(self);
}
}

fn visit_unnamed_fields(&mut self, values: &[Value<'_>]) {
for value in values {
print!("{}- ", self.0);
value.visit(self);
}
}

fn visit_entry(&mut self, key: Value<'_>, value: Value<'_>) {
print!("{}- {:?}: ", self.0, key);
value.visit(self);
}
}

#[derive(Valuable)]
struct Person {
name: String,
age: u32,
addresses: Vec<Address>,
}

#[derive(Valuable)]
struct Address {
street: String,
city: String,
zip: String,
}

fn main() {
let person = Person {
name: "Angela Ashton".to_string(),
age: 31,
addresses: vec![
Address {
street: "123 1st Ave".to_string(),
city: "Townsville".to_string(),
zip: "12345".to_string(),
},
Address {
street: "555 Main St.".to_string(),
city: "New Old Town".to_string(),
zip: "55555".to_string(),
},
],
};

let mut print = Print("".to_string());
print.visit_value(person.as_value());
}
11 changes: 5 additions & 6 deletions valuable/src/field.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[derive(Debug)]
pub enum Fields<'a> {
/// Named fields
Named(&'a [NamedField<'a>]),
Expand All @@ -6,10 +7,8 @@ pub enum Fields<'a> {
Unnamed,
}

pub struct NamedField<'a> {
/// Field name
name: &'a str,
}
#[derive(Debug)]
pub struct NamedField<'a>(&'a str);

impl Fields<'_> {
pub fn is_named(&self) -> bool {
Expand All @@ -23,10 +22,10 @@ impl Fields<'_> {

impl<'a> NamedField<'a> {
pub const fn new(name: &'a str) -> NamedField<'a> {
NamedField { name: name }
NamedField(name)
}

pub fn name(&self) -> &str {
self.name
self.0
}
}
22 changes: 20 additions & 2 deletions valuable/src/listable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ macro_rules! collection {
}

collection! {
#[cfg(feature = "alloc")]
(T: Valuable) alloc::collections::VecDeque<T>,
#[cfg(feature = "alloc")]
(T: Valuable) alloc::collections::LinkedList<T>,
#[cfg(feature = "alloc")]
Expand All @@ -121,6 +119,26 @@ collection! {
(T: Valuable + Eq + std::hash::Hash, H: std::hash::BuildHasher) std::collections::HashSet<T, H>,
}

#[cfg(feature = "alloc")]
impl<T: Valuable> Valuable for alloc::collections::VecDeque<T> {
fn as_value(&self) -> Value<'_> {
Value::Listable(self as &dyn Listable)
}

fn visit(&self, visit: &mut dyn Visit) {
let (first, second) = self.as_slices();
T::visit_slice(first, visit);
T::visit_slice(second, visit);
}
}

#[cfg(feature = "alloc")]
impl<T: Valuable> Listable for alloc::collections::VecDeque<T> {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
}

impl fmt::Debug for dyn Listable + '_ {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
struct DebugListable<'a, 'b> {
Expand Down
Loading

0 comments on commit fd0fb8e

Please sign in to comment.