Skip to content

Commit

Permalink
Show off ToStatic
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Jul 29, 2024
1 parent 5a97f58 commit e28f62a
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 159 deletions.
30 changes: 15 additions & 15 deletions examples/mixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,40 @@ use merde_json::{
};

#[derive(Debug, PartialEq)]
struct MixedArray<'inner, 'borrow> {
_boo: Fantome<'inner, 'borrow>,
struct MixedArray<'src, 'val> {
_boo: Fantome<'src, 'val>,

items: Vec<&'borrow JsonValue<'inner>>,
items: Vec<&'val JsonValue<'src>>,
}
merde_json::derive! {
impl(JsonSerialize, JsonDeserialize) for MixedArray { items }
}

#[derive(Debug, PartialEq)]
struct MixedArray2<'inner, 'borrow> {
_boo: Fantome<'inner, 'borrow>,
struct MixedArray2<'src, 'val> {
_boo: Fantome<'src, 'val>,

items: &'borrow JsonArray<'inner>,
items: &'val JsonArray<'src>,
}
merde_json::derive! {
impl(JsonSerialize, JsonDeserialize) for MixedArray2 { items }
}

#[derive(Debug, PartialEq)]
struct Items<'inner, 'borrow> {
_boo: Fantome<'inner, 'borrow>,
struct Items<'src, 'val> {
_boo: Fantome<'src, 'val>,

number: u32,
string: Cow<'borrow, str>,
string: Cow<'val, str>,
boolean: bool,
}

impl<'inner, 'borrow> JsonDeserialize<'inner, 'borrow> for Items<'inner, 'borrow>
impl<'src, 'val> JsonDeserialize<'src, 'val> for Items<'src, 'val>
where
'inner: 'borrow,
'src: 'val,
{
fn json_deserialize(
value: Option<&'borrow JsonValue<'inner>>,
value: Option<&'val JsonValue<'src>>,
) -> Result<Self, merde_json::MerdeJsonError> {
let arr = value
.ok_or(merde_json::MerdeJsonError::MissingValue)?
Expand All @@ -67,10 +67,10 @@ impl JsonSerialize for Items<'_, '_> {
}

#[derive(Debug, PartialEq)]
struct MixedArray3<'inner, 'borrow> {
_boo: Fantome<'inner, 'borrow>,
struct MixedArray3<'src, 'val> {
_boo: Fantome<'src, 'val>,

items: Items<'inner, 'borrow>,
items: Items<'src, 'val>,
}
merde_json::derive! {
impl(JsonSerialize, JsonDeserialize) for MixedArray3 { items }
Expand Down
18 changes: 9 additions & 9 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ fn main() {

#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq)]
struct Address<'inner, 'borrow> {
_boo: Fantome<'inner, 'borrow>,
struct Address<'src, 'val> {
_boo: Fantome<'src, 'val>,

street: Cow<'borrow, str>,
city: Cow<'borrow, str>,
state: Cow<'borrow, str>,
street: Cow<'val, str>,
city: Cow<'val, str>,
state: Cow<'val, str>,
zip: u16,
}

Expand All @@ -55,12 +55,12 @@ merde_json::derive! {

#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq)]
struct Person<'inner, 'borrow> {
_boo: Fantome<'inner, 'borrow>,
struct Person<'src, 'val> {
_boo: Fantome<'src, 'val>,

name: Cow<'borrow, str>,
name: Cow<'val, str>,
age: u8,
address: Address<'inner, 'borrow>,
address: Address<'src, 'val>,
}

merde_json::derive! {
Expand Down
61 changes: 61 additions & 0 deletions examples/to_static.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::borrow::Cow;

use merde_json::{Fantome, ToRustValue, ToStatic};

#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq)]
struct Address<'src, 'val> {
_boo: Fantome<'src, 'val>,

street: Cow<'val, str>,
city: Cow<'val, str>,
state: Cow<'val, str>,
zip: u16,
}

merde_json::derive! {
impl (JsonDeserialize, ToStatic) for Address {
street,
city,
state,
zip
}
}

#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq)]
struct Person<'src, 'val> {
_boo: Fantome<'src, 'val>,

name: Cow<'val, str>,
age: u8,
address: Address<'src, 'val>,
}

merde_json::derive! {
impl (JsonDeserialize, ToStatic) for Person { name, age, address }
}

fn get_person() -> Person<'static, 'static> {
let input = r#"
{
"name": "John Doe",
"age": 42,
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip": 12345
}
}
"#;

let person = merde_json::from_str(input).unwrap();
let person: Person = person.to_rust_value().unwrap();
person.to_static()
}

fn main() {
let person = get_person();
println!("{:?}", person);
}
Loading

0 comments on commit e28f62a

Please sign in to comment.