summaryrefslogtreecommitdiffstats
path: root/vendor/valuable/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/valuable/examples
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/valuable/examples')
-rw-r--r--vendor/valuable/examples/derive.rs26
-rw-r--r--vendor/valuable/examples/hello_world.rs68
-rw-r--r--vendor/valuable/examples/print.rs106
3 files changed, 200 insertions, 0 deletions
diff --git a/vendor/valuable/examples/derive.rs b/vendor/valuable/examples/derive.rs
new file mode 100644
index 000000000..b67aaf3a7
--- /dev/null
+++ b/vendor/valuable/examples/derive.rs
@@ -0,0 +1,26 @@
+use valuable::Valuable;
+
+use std::collections::HashMap;
+
+// `Debug` not implemented for struct, the debug implementation is going via
+// valuable.
+#[derive(Valuable)]
+struct Person {
+ name: String,
+ age: u8,
+ phones: Vec<String>,
+ favorites: HashMap<String, String>,
+}
+
+fn main() {
+ let mut p = Person {
+ name: "John Doe".to_string(),
+ age: 42,
+ phones: vec!["876-5309".to_string()],
+ favorites: HashMap::new(),
+ };
+
+ p.favorites.insert("color".to_string(), "blue".to_string());
+
+ println!("{:#?}", p.as_value());
+}
diff --git a/vendor/valuable/examples/hello_world.rs b/vendor/valuable/examples/hello_world.rs
new file mode 100644
index 000000000..7c3d173f0
--- /dev/null
+++ b/vendor/valuable/examples/hello_world.rs
@@ -0,0 +1,68 @@
+use valuable::*;
+
+struct HelloWorld {
+ hello: &'static str,
+ world: World,
+}
+
+struct World {
+ answer: usize,
+}
+
+static HELLO_WORLD_FIELDS: &[NamedField<'static>] =
+ &[NamedField::new("hello"), NamedField::new("world")];
+
+impl Structable for HelloWorld {
+ fn definition(&self) -> StructDef<'_> {
+ StructDef::new_static("HelloWorld", Fields::Named(HELLO_WORLD_FIELDS))
+ }
+}
+
+impl Valuable for HelloWorld {
+ fn as_value(&self) -> Value<'_> {
+ Value::Structable(self)
+ }
+
+ fn visit(&self, v: &mut dyn Visit) {
+ v.visit_named_fields(&NamedValues::new(
+ HELLO_WORLD_FIELDS,
+ &[Value::String(self.hello), Value::Structable(&self.world)],
+ ));
+ }
+}
+
+static WORLD_FIELDS: &[NamedField<'static>] = &[NamedField::new("answer")];
+
+impl Valuable for World {
+ fn as_value(&self) -> Value<'_> {
+ Value::Structable(self)
+ }
+
+ fn visit(&self, v: &mut dyn Visit) {
+ v.visit_named_fields(&NamedValues::new(
+ WORLD_FIELDS,
+ &[Value::Usize(self.answer)],
+ ));
+ }
+}
+
+impl Structable for World {
+ fn definition(&self) -> StructDef<'_> {
+ StructDef::new_static("World", Fields::Named(WORLD_FIELDS))
+ }
+}
+
+fn main() {
+ let hello_world = HelloWorld {
+ hello: "wut",
+ world: World { answer: 42 },
+ };
+
+ let value = Value::Structable(&hello_world);
+ println!("{:#?}", value);
+
+ let slice = &[1, 2, 3][..];
+
+ let value = &slice as &dyn Valuable;
+ println!("{:?}", value);
+}
diff --git a/vendor/valuable/examples/print.rs b/vendor/valuable/examples/print.rs
new file mode 100644
index 000000000..b6998d37a
--- /dev/null
+++ b/vendor/valuable/examples/print.rs
@@ -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 {
+ 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());
+ valuable::visit(&person, &mut print);
+}