Add support for Option<T>

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

@ -3,7 +3,7 @@ members = [".", "derive"]
[package]
name = "rusty-value"
version = "0.4.0"
version = "0.4.1"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/Trivernis/rusty-value"

@ -1,6 +1,6 @@
use std::{collections::HashMap, ffi::OsString, path::PathBuf};
use crate::{Fields, Float, HashablePrimitive, HashableValue, Primitive, Struct, Value};
use crate::{Enum, Fields, Float, HashablePrimitive, HashableValue, Primitive, Struct, Value};
pub trait RustyValue {
fn into_rusty_value(self) -> Value;
@ -174,6 +174,25 @@ impl RustyValue for PathBuf {
}
}
impl<T: RustyValue> RustyValue for Option<T> {
fn into_rusty_value(self) -> Value {
let name = String::from("Option");
match self {
Some(val) => Value::Enum(Enum {
name,
variant: String::from("Some"),
fields: Fields::Unnamed(vec![val.into_rusty_value()]),
}),
None => Value::Enum(Enum {
name,
variant: String::from("None"),
fields: Fields::Unit,
}),
}
}
}
impl<R: RustyValue> RustyValue for Vec<R> {
fn into_rusty_value(self) -> Value {
let value_vec = self

@ -35,11 +35,11 @@ fn it_handles_named_fields() {
}
#[derive(RustyValue)]
struct TestStructUnnamed(String, u64);
struct TestStructUnnamed(String, u64, Option<String>);
#[test]
fn it_handles_unnamed_fields() {
let test_struct = TestStructUnnamed(String::from("Hello World"), 12);
let test_struct = TestStructUnnamed(String::from("Hello World"), 12, None);
let value = test_struct.into_rusty_value();
dbg!(&value);
@ -47,7 +47,7 @@ fn it_handles_unnamed_fields() {
assert_eq!(&s.name, "TestStructUnnamed");
if let Fields::Unnamed(fields) = s.fields {
assert_eq!(fields.len(), 2);
assert_eq!(fields.len(), 3);
} else {
panic!("Struct wasn't serialized as unnamed struct")
}

Loading…
Cancel
Save