A generic rust value
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trivernis 63e18899b2
Change how Options get serialized
5 months ago
.helix Add IntoJson trait to convert a rusty value into json 6 months ago
derive Fix crate derivation at the cost of having to put all of rusty value into scope 6 months ago
src Change how Options get serialized 5 months ago
tests Add IntoJson trait to convert a rusty value into json 6 months ago
.gitignore Add derive macro for structs 6 months ago
Cargo.toml Change how Options get serialized 5 months ago
LICENSE Create LICENSE 6 months ago
README.md Add IntoJson trait to convert a rusty value into json 6 months ago

README.md

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.

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.

use rusty_value::*;

#[derive(RustyValue)]
struct MyStruct {
  foo: String,
  bar: u8,
}

fn main() {
  let value = MyStruct {
    foo: "Hello World".to_string(),
    bar: 12,
  }.into_rusty_value();

  match value {
      Value::Primitive(p) => match p {
          rusty_value::Primitive::Integer(_) => println!("is an integer"),
          rusty_value::Primitive::Float(_) => println!("is a float"),
          rusty_value::Primitive::String(_) => println!("is a string"),
          rusty_value::Primitive::OsString(_) => println!("is a os string"),
          rusty_value::Primitive::Char(_) => println!("is a char"),
          rusty_value::Primitive::Bool(_) => println!("is a boolean"),
      },
      Value::Struct(s) => println!("is a struct with name {}", s.name),
      Value::Enum(e) => println!("is an enum with name {} of variant {}", e.name, e.variant),
      Value::Map(_) => println!("is a map"),
      Value::List(_) => println!("is a list"),
      Value::None => println!("is none"),
  }
}

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.