From 5bd25c1fe09e44971d0bf8c389b2e4de025b4d7f Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 5 Oct 2022 12:50:53 +0200 Subject: [PATCH] Add support for OsString and PathBuf --- Cargo.toml | 2 +- src/value.rs | 5 ++++- src/value_trait.rs | 20 ++++++++++++++++++-- tests/structs.rs | 6 +++++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d454e34..f3d59b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/value.rs b/src/value.rs index f04009f..70e80f7 100644 --- a/src/value.rs +++ b/src/value.rs @@ -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(), } } } diff --git a/src/value_trait.rs b/src/value_trait.rs index 10ed8aa..fda3cc8 100644 --- a/src/value_trait.rs +++ b/src/value_trait.rs @@ -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 RustyValue for Vec { fn into_rusty_value(self) -> Value { let value_vec = self diff --git a/tests/structs.rs b/tests/structs.rs index ad2abdc..ebfc3f1 100644 --- a/tests/structs.rs +++ b/tests/structs.rs @@ -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") }