Add package metadata

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 2 years ago
parent d007e6e569
commit 983be5d9a9
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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 <trivernis@proton.me>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

@ -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.

@ -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 <trivernis@proton.me>"]
[lib]
proc-macro = true

@ -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<HashablePrimitive, Value>),
Map(HashMap<HashableValue, Value>),
List(Vec<Value>),
}
/// 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<String, Value>),
@ -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<HashableValue>),
}
/// A primitive that can be used as a hash map key
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub enum HashablePrimitive {
Integer(Integer),

@ -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 RustyValue for u128 {
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 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<H: HashableRustyValue> 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<R: RustyValue> RustyValue for Vec<R> {
}
}
impl<R: RustyValue> RustyValue for HashMap<String, R> {
impl<R: RustyValue, H: HashableRustyValue> RustyValue for HashMap<H, R> {
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::<HashMap<_, _>>();
Value::Map(map)

Loading…
Cancel
Save