diff --git a/Cargo.toml b/Cargo.toml index e251902..d454e34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = [".", "derive"] [package] name = "rusty-value" -version = "0.2.1" +version = "0.3.0" edition = "2021" license = "Apache-2.0" repository = "https://github.com/Trivernis/rusty-value" diff --git a/README.md b/README.md index c358ff9..79ceef7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,34 @@ # Rusty Value +[![](https://img.shields.io/crates/v/rusty-value?style=for-the-badge)](https://crates.io/crates/rusty-value) +[![](https://img.shields.io/docsrs/rusty-value/latest?style=for-the-badge)](https://docs.rs/rusty-value/) This crate adds a `RustyValue` trait that can be derived for all types (except unions) to create a generic value that represents a rust value. -This can be used to implement serialization of types without having to rely on serde. \ No newline at end of file +This can be used to implement serialization of types without having to rely on serde. + +## Usage + +The trait `RustyValue` allows one to create a `rusty_value::Value` for any +type that implements it. This trait can be derived if the `derive` **feature** is enabled. + +```rust + +use rusty_value::RustyValue; + +#[derive(RustyValue)] +struct MyStruct { + foo: String, + bar: u8, +} + +fn main() { + MyStruct { + foo: "Hello World".to_string(), + bar: 12, + }.into_rusty_value(); +} +``` + +Converting a type into a rusty value cannot fail as `rusty_value::RustyValue` is +able to represent any safe rust data type. The trait `RustyValue` is already implemented for +most std types and can therefore be easily derived. \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index a10a664..00b3a8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![doc=include_str!("../README.md")] + pub(crate) mod value; pub(crate) mod value_trait; pub use value::*; diff --git a/src/value.rs b/src/value.rs index 43e33f6..f04009f 100644 --- a/src/value.rs +++ b/src/value.rs @@ -8,6 +8,7 @@ pub enum Value { Enum(Enum), Map(HashMap), List(Vec), + None, } /// Represents an enum with a given variant @@ -73,6 +74,7 @@ pub enum Float { pub enum HashableValue { Primitive(HashablePrimitive), List(Vec), + None, } /// A primitive that can be used as a hash map key diff --git a/src/value_trait.rs b/src/value_trait.rs index 105a85a..10ed8aa 100644 --- a/src/value_trait.rs +++ b/src/value_trait.rs @@ -114,6 +114,12 @@ impl HashableRustyValue for bool { } } +impl HashableRustyValue for () { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::None + } +} + impl RustyValue for HashableValue { fn into_rusty_value(self) -> Value { match self { @@ -126,6 +132,7 @@ impl RustyValue for HashableValue { HashableValue::List(l) => { Value::List(l.into_iter().map(|v| v.into_rusty_value()).collect()) } + HashableValue::None => Value::None, } } } diff --git a/tests/structs.rs b/tests/structs.rs index 0af208d..ad2abdc 100644 --- a/tests/structs.rs +++ b/tests/structs.rs @@ -4,6 +4,7 @@ use rusty_value::{Fields, RustyValue, Value}; struct TestStructNamed { foo: String, bar: u64, + none: (), } #[test] @@ -11,6 +12,7 @@ fn it_handles_named_fields() { let test_struct = TestStructNamed { foo: String::from("Hello World"), bar: 12, + none: (), }; let value = test_struct.into_rusty_value(); dbg!(&value); @@ -19,7 +21,7 @@ fn it_handles_named_fields() { assert_eq!(&s.name, "TestStructNamed"); if let Fields::Named(fields) = s.fields { - assert_eq!(fields.len(), 2); + assert_eq!(fields.len(), 3); } else { panic!("Struct wasn't serialized as named struct") }