Add support for OsString and PathBuf

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

@ -3,7 +3,7 @@ members = [".", "derive"]
[package] [package]
name = "rusty-value" name = "rusty-value"
version = "0.3.0" version = "0.4.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,4 +1,4 @@
use std::collections::HashMap; use std::{collections::HashMap, ffi::OsString};
/// Represents a generic rust value /// Represents a generic rust value
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@ -41,6 +41,7 @@ pub enum Primitive {
Integer(Integer), Integer(Integer),
Float(Float), Float(Float),
String(String), String(String),
OsString(OsString),
Char(char), Char(char),
Bool(bool), Bool(bool),
} }
@ -82,6 +83,7 @@ pub enum HashableValue {
pub enum HashablePrimitive { pub enum HashablePrimitive {
Integer(Integer), Integer(Integer),
String(String), String(String),
OsString(OsString),
Char(char), Char(char),
Bool(bool), Bool(bool),
} }
@ -93,6 +95,7 @@ impl ToString for HashablePrimitive {
HashablePrimitive::String(s) => s.to_owned(), HashablePrimitive::String(s) => s.to_owned(),
HashablePrimitive::Char(c) => c.to_string(), HashablePrimitive::Char(c) => c.to_string(),
HashablePrimitive::Bool(b) => b.to_string(), HashablePrimitive::Bool(b) => b.to_string(),
HashablePrimitive::OsString(o) => o.to_string_lossy().into_owned(),
} }
} }
} }

@ -1,6 +1,6 @@
use std::collections::HashMap; use std::{collections::HashMap, ffi::OsString, path::PathBuf};
use crate::{Float, HashablePrimitive, HashableValue, Primitive, Value}; use crate::{Fields, Float, HashablePrimitive, HashableValue, Primitive, Struct, Value};
pub trait RustyValue { pub trait RustyValue {
fn into_rusty_value(self) -> Value; fn into_rusty_value(self) -> Value;
@ -128,6 +128,7 @@ impl RustyValue for HashableValue {
HashablePrimitive::String(s) => Value::Primitive(Primitive::String(s)), HashablePrimitive::String(s) => Value::Primitive(Primitive::String(s)),
HashablePrimitive::Char(c) => Value::Primitive(Primitive::Char(c)), HashablePrimitive::Char(c) => Value::Primitive(Primitive::Char(c)),
HashablePrimitive::Bool(b) => Value::Primitive(Primitive::Bool(b)), HashablePrimitive::Bool(b) => Value::Primitive(Primitive::Bool(b)),
HashablePrimitive::OsString(o) => Value::Primitive(Primitive::OsString(o)),
}, },
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())
@ -158,6 +159,21 @@ impl RustyValue for f64 {
} }
} }
impl HashableRustyValue for OsString {
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::OsString(self))
}
}
impl RustyValue for PathBuf {
fn into_rusty_value(self) -> Value {
Value::Struct(Struct {
name: String::from("PathBuf"),
fields: Fields::Unnamed(vec![self.into_os_string().into_rusty_value()]),
})
}
}
impl<R: RustyValue> RustyValue for Vec<R> { impl<R: RustyValue> RustyValue for Vec<R> {
fn into_rusty_value(self) -> Value { fn into_rusty_value(self) -> Value {
let value_vec = self let value_vec = self

@ -1,3 +1,5 @@
use std::path::PathBuf;
use rusty_value::{Fields, RustyValue, Value}; use rusty_value::{Fields, RustyValue, Value};
#[derive(RustyValue)] #[derive(RustyValue)]
@ -5,6 +7,7 @@ struct TestStructNamed {
foo: String, foo: String,
bar: u64, bar: u64,
none: (), none: (),
path: PathBuf,
} }
#[test] #[test]
@ -13,6 +16,7 @@ fn it_handles_named_fields() {
foo: String::from("Hello World"), foo: String::from("Hello World"),
bar: 12, bar: 12,
none: (), none: (),
path: PathBuf::new(),
}; };
let value = test_struct.into_rusty_value(); let value = test_struct.into_rusty_value();
dbg!(&value); dbg!(&value);
@ -21,7 +25,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(), 3); assert_eq!(fields.len(), 4);
} else { } else {
panic!("Struct wasn't serialized as named struct") panic!("Struct wasn't serialized as named struct")
} }

Loading…
Cancel
Save