Add trait implementation for () type

main
trivernis 2 years ago
parent 6f7c10bdd0
commit 4fbdda3564
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -3,7 +3,7 @@ members = [".", "derive"]
[package] [package]
name = "rusty-value" name = "rusty-value"
version = "0.2.1" version = "0.3.0"
edition = "2021" edition = "2021"
license = "Apache-2.0" license = "Apache-2.0"
repository = "https://github.com/Trivernis/rusty-value" repository = "https://github.com/Trivernis/rusty-value"

@ -1,5 +1,34 @@
# Rusty Value # 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) 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. 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. 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.

@ -1,3 +1,5 @@
#![doc=include_str!("../README.md")]
pub(crate) mod value; pub(crate) mod value;
pub(crate) mod value_trait; pub(crate) mod value_trait;
pub use value::*; pub use value::*;

@ -8,6 +8,7 @@ pub enum Value {
Enum(Enum), Enum(Enum),
Map(HashMap<HashableValue, Value>), Map(HashMap<HashableValue, Value>),
List(Vec<Value>), List(Vec<Value>),
None,
} }
/// Represents an enum with a given variant /// Represents an enum with a given variant
@ -73,6 +74,7 @@ pub enum Float {
pub enum HashableValue { pub enum HashableValue {
Primitive(HashablePrimitive), Primitive(HashablePrimitive),
List(Vec<HashableValue>), List(Vec<HashableValue>),
None,
} }
/// A primitive that can be used as a hash map key /// A primitive that can be used as a hash map key

@ -114,6 +114,12 @@ impl HashableRustyValue for bool {
} }
} }
impl HashableRustyValue for () {
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::None
}
}
impl RustyValue for HashableValue { impl RustyValue for HashableValue {
fn into_rusty_value(self) -> Value { fn into_rusty_value(self) -> Value {
match self { match self {
@ -126,6 +132,7 @@ impl RustyValue for HashableValue {
HashableValue::List(l) => { HashableValue::List(l) => {
Value::List(l.into_iter().map(|v| v.into_rusty_value()).collect()) Value::List(l.into_iter().map(|v| v.into_rusty_value()).collect())
} }
HashableValue::None => Value::None,
} }
} }
} }

@ -4,6 +4,7 @@ use rusty_value::{Fields, RustyValue, Value};
struct TestStructNamed { struct TestStructNamed {
foo: String, foo: String,
bar: u64, bar: u64,
none: (),
} }
#[test] #[test]
@ -11,6 +12,7 @@ fn it_handles_named_fields() {
let test_struct = TestStructNamed { let test_struct = TestStructNamed {
foo: String::from("Hello World"), foo: String::from("Hello World"),
bar: 12, bar: 12,
none: (),
}; };
let value = test_struct.into_rusty_value(); let value = test_struct.into_rusty_value();
dbg!(&value); dbg!(&value);
@ -19,7 +21,7 @@ fn it_handles_named_fields() {
assert_eq!(&s.name, "TestStructNamed"); assert_eq!(&s.name, "TestStructNamed");
if let Fields::Named(fields) = s.fields { if let Fields::Named(fields) = s.fields {
assert_eq!(fields.len(), 2); assert_eq!(fields.len(), 3);
} else { } else {
panic!("Struct wasn't serialized as named struct") panic!("Struct wasn't serialized as named struct")
} }

Loading…
Cancel
Save