From 983be5d9a9248651ab34c1200d6b636712af6223 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 3 Oct 2022 14:21:15 +0200 Subject: [PATCH] Add package metadata Signed-off-by: trivernis --- Cargo.toml | 4 ++ README.md | 5 ++ derive/Cargo.toml | 4 ++ src/value.rs | 16 +++++- src/value_trait.rs | 120 ++++++++++++++++++++++++++++++++------------- 5 files changed, 113 insertions(+), 36 deletions(-) create mode 100644 README.md diff --git a/Cargo.toml b/Cargo.toml index 0c8c1ea..55e37a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,10 @@ members = [".", "derive"] name = "rusty-value" version = "0.1.0" edition = "2021" +license = "Apache-2.0" +repository = "https://github.com/Trivernis/rusty-value" +description = "Create a generic inspectable value from any rust type" +authors = ["trivernis "] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..c358ff9 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# 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 diff --git a/derive/Cargo.toml b/derive/Cargo.toml index 0e3d3f1..68d175b 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -2,6 +2,10 @@ name = "rusty-value-derive" version = "0.1.0" edition = "2021" +license = "Apache-2.0" +repository = "https://github.com/Trivernis/rusty-value" +description = "Derive implementaton for rusty-value" +authors = ["trivernis "] [lib] proc-macro = true diff --git a/src/value.rs b/src/value.rs index 379e64c..97d88e2 100644 --- a/src/value.rs +++ b/src/value.rs @@ -1,14 +1,17 @@ use std::collections::HashMap; +/// Represents a generic rust value #[derive(Clone, Debug, PartialEq)] pub enum Value { Primitive(Primitive), Struct(Struct), Enum(Enum), - Map(HashMap), + Map(HashMap), List(Vec), } +/// Represents an enum with a given variant +/// And fields depending on that variant #[derive(Clone, Debug, PartialEq)] pub struct Enum { pub name: String, @@ -16,12 +19,14 @@ pub struct Enum { pub fields: Fields, } +/// Represents a struct with fields #[derive(Clone, Debug, PartialEq)] pub struct Struct { pub name: String, pub fields: Fields, } +/// Fields of a struct or an enum that are either named, unnamed or not defined (Unit enums/structs) #[derive(Clone, Debug, PartialEq)] pub enum Fields { Named(HashMap), @@ -29,6 +34,7 @@ pub enum Fields { Unit, } +/// A rust primitive value #[derive(Clone, Debug, PartialEq, PartialOrd)] pub enum Primitive { Integer(Integer), @@ -38,8 +44,11 @@ pub enum Primitive { Bool(bool), } +/// A primitive integer value #[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)] pub enum Integer { + USize(usize), + ISize(isize), U8(u8), I8(i8), U16(u16), @@ -52,16 +61,21 @@ pub enum Integer { I128(i128), } +/// A primitive float value #[derive(Clone, Debug, PartialEq, PartialOrd)] pub enum Float { F32(f32), F64(f64), } +/// A value that can be used as a key inside a hash map +#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)] pub enum HashableValue { Primitive(HashablePrimitive), + List(Vec), } +/// A primitive that can be used as a hash map key #[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)] pub enum HashablePrimitive { Integer(Integer), diff --git a/src/value_trait.rs b/src/value_trait.rs index 4643466..725041c 100644 --- a/src/value_trait.rs +++ b/src/value_trait.rs @@ -1,80 +1,130 @@ use std::collections::HashMap; -use crate::{HashablePrimitive, Primitive, Value}; +use crate::{Float, HashablePrimitive, HashableValue, Primitive, Value}; pub trait RustyValue { fn into_rusty_value(self) -> Value; } -impl RustyValue for u8 { - fn into_rusty_value(self) -> Value { - Value::Primitive(Primitive::Integer(crate::Integer::U8(self))) +pub trait HashableRustyValue { + fn into_hashable_rusty_value(self) -> HashableValue; +} + +impl HashableRustyValue for usize { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::USize(self))) } } -impl RustyValue for i8 { - fn into_rusty_value(self) -> Value { - Value::Primitive(Primitive::Integer(crate::Integer::I8(self))) +impl HashableRustyValue for isize { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::ISize(self))) } } -impl RustyValue for u16 { - fn into_rusty_value(self) -> Value { - Value::Primitive(Primitive::Integer(crate::Integer::U16(self))) +impl HashableRustyValue for u8 { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U8(self))) } } -impl RustyValue for i16 { - fn into_rusty_value(self) -> Value { - Value::Primitive(Primitive::Integer(crate::Integer::I16(self))) +impl HashableRustyValue for i8 { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I8(self))) } } -impl RustyValue for u32 { - fn into_rusty_value(self) -> Value { - Value::Primitive(Primitive::Integer(crate::Integer::U32(self))) +impl HashableRustyValue for u16 { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U16(self))) } } -impl RustyValue for i32 { - fn into_rusty_value(self) -> Value { - Value::Primitive(Primitive::Integer(crate::Integer::I32(self))) +impl HashableRustyValue for i16 { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I16(self))) } } -impl RustyValue for u64 { - fn into_rusty_value(self) -> Value { - Value::Primitive(Primitive::Integer(crate::Integer::U64(self))) +impl HashableRustyValue for u32 { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U32(self))) } } -impl RustyValue for i64 { - fn into_rusty_value(self) -> Value { - Value::Primitive(Primitive::Integer(crate::Integer::I64(self))) +impl HashableRustyValue for i32 { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I32(self))) + } +} + +impl HashableRustyValue for u64 { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U64(self))) + } +} + +impl HashableRustyValue for i64 { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I64(self))) + } +} + +impl HashableRustyValue for u128 { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U128(self))) + } +} + +impl HashableRustyValue for i128 { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I128(self))) + } +} + +impl HashableRustyValue for String { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::String(self)) + } +} + +impl HashableRustyValue for bool { + fn into_hashable_rusty_value(self) -> HashableValue { + HashableValue::Primitive(HashablePrimitive::Bool(self)) } } -impl RustyValue for u128 { +impl RustyValue for HashableValue { fn into_rusty_value(self) -> Value { - Value::Primitive(Primitive::Integer(crate::Integer::U128(self))) + match self { + HashableValue::Primitive(p) => match p { + HashablePrimitive::Integer(i) => Value::Primitive(Primitive::Integer(i)), + HashablePrimitive::String(s) => Value::Primitive(Primitive::String(s)), + HashablePrimitive::Char(c) => Value::Primitive(Primitive::Char(c)), + HashablePrimitive::Bool(b) => Value::Primitive(Primitive::Bool(b)), + }, + HashableValue::List(l) => { + Value::List(l.into_iter().map(|v| v.into_rusty_value()).collect()) + } + } } } -impl RustyValue for i128 { +impl RustyValue for H { fn into_rusty_value(self) -> Value { - Value::Primitive(Primitive::Integer(crate::Integer::I128(self))) + self.into_hashable_rusty_value().into_rusty_value() } } -impl RustyValue for String { +impl RustyValue for f32 { fn into_rusty_value(self) -> Value { - Value::Primitive(Primitive::String(self)) + Value::Primitive(Primitive::Float(Float::F32(self))) } } -impl RustyValue for bool { +impl RustyValue for f64 { fn into_rusty_value(self) -> Value { - Value::Primitive(Primitive::Bool(self)) + Value::Primitive(Primitive::Float(Float::F64(self))) } } @@ -89,11 +139,11 @@ impl RustyValue for Vec { } } -impl RustyValue for HashMap { +impl RustyValue for HashMap { fn into_rusty_value(self) -> Value { let map = self .into_iter() - .map(|(k, v)| (HashablePrimitive::String(k), v.into_rusty_value())) + .map(|(k, v)| (k.into_hashable_rusty_value(), v.into_rusty_value())) .collect::>(); Value::Map(map)