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]
name = "rusty-value"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
license = "Apache-2.0"
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
#[derive(Clone, Debug, PartialEq)]
@ -41,6 +41,7 @@ pub enum Primitive {
Integer(Integer),
Float(Float),
String(String),
OsString(OsString),
Char(char),
Bool(bool),
}
@ -82,6 +83,7 @@ pub enum HashableValue {
pub enum HashablePrimitive {
Integer(Integer),
String(String),
OsString(OsString),
Char(char),
Bool(bool),
}
@ -93,6 +95,7 @@ impl ToString for HashablePrimitive {
HashablePrimitive::String(s) => s.to_owned(),
HashablePrimitive::Char(c) => c.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 {
fn into_rusty_value(self) -> Value;
@ -128,6 +128,7 @@ impl RustyValue for HashableValue {
HashablePrimitive::String(s) => Value::Primitive(Primitive::String(s)),
HashablePrimitive::Char(c) => Value::Primitive(Primitive::Char(c)),
HashablePrimitive::Bool(b) => Value::Primitive(Primitive::Bool(b)),
HashablePrimitive::OsString(o) => Value::Primitive(Primitive::OsString(o)),
},
HashableValue::List(l) => {
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> {
fn into_rusty_value(self) -> Value {
let value_vec = self

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

Loading…
Cancel
Save