Skip to content

Commit

Permalink
Add mixed example
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Jul 29, 2024
1 parent f3b3ffd commit dd147e9
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 38 deletions.
80 changes: 76 additions & 4 deletions examples/mixed.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,81 @@
use std::marker::PhantomData;
use std::borrow::Cow;

use merde_json::{JsonValue, ToRustValue};
use jiter::JsonArray;
use merde_json::{
Fantome, JsonArrayExt, JsonDeserialize, JsonSerialize, JsonSerializer, JsonValue, JsonValueExt,
ToRustValue,
};

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

items: Vec<&'borrow JsonValue<'inner>>,
_phantom: PhantomData<(&'inner (), &'borrow ())>,
}

merde_json::derive! {
impl(JsonSerialize, JsonDeserialize) for MixedArray { items }
}

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

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

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

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

impl<'inner, 'borrow> JsonDeserialize<'inner, 'borrow> for Items<'inner, 'borrow>
where
'inner: 'borrow,
{
fn json_deserialize(
value: Option<&'borrow JsonValue<'inner>>,
) -> Result<Self, merde_json::MerdeJsonError> {
let arr = value
.ok_or(merde_json::MerdeJsonError::MissingValue)?
.as_array()?;

Ok(Items {
_boo: Default::default(),

number: arr.must_get(0)?,
string: arr.must_get(1)?,
boolean: arr.must_get(2)?,
})
}
}

impl JsonSerialize for Items<'_, '_> {
fn json_serialize(&self, serializer: &mut JsonSerializer) {
serializer
.write_arr()
.elem(&self.number)
.elem(&self.string)
.elem(&self.boolean);
}
}

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

items: Items<'inner, 'borrow>,
}
merde_json::derive! {
impl(JsonSerialize, JsonDeserialize) for MixedArray3 { items }
}

fn main() {
let input = r#"
{
Expand All @@ -24,4 +88,12 @@ fn main() {
let ma = merde_json::from_str(input).unwrap();
let ma: MixedArray = ma.to_rust_value().unwrap();
println!("{:?}", ma);

let ma = merde_json::from_str(input).unwrap();
let ma: MixedArray2 = ma.to_rust_value().unwrap();
println!("{:?}", ma);

let ma = merde_json::from_str(input).unwrap();
let ma: MixedArray3 = ma.to_rust_value().unwrap();
println!("{:?}", ma);
}
10 changes: 5 additions & 5 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{borrow::Cow, marker::PhantomData};

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

fn main() {
let input = r#"
Expand Down Expand Up @@ -36,12 +36,12 @@ fn main() {
#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq)]
struct Address<'inner, 'borrow> {
_boo: Fantome<'inner, 'borrow>,

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

_phantom: PhantomData<(&'borrow (), &'inner ())>,
}

merde_json::derive! {
Expand All @@ -56,11 +56,11 @@ merde_json::derive! {
#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq)]
struct Person<'inner, 'borrow> {
_boo: Fantome<'inner, 'borrow>,

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

_phantom: PhantomData<(&'inner (), &'borrow ())>,
}

merde_json::derive! {
Expand Down
15 changes: 15 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ pub enum MerdeJsonError {
/// We expected an object to have a certain property, but it was missing.
MissingProperty(&'static str),

/// We tried to access an array index that was out of bounds.
IndexOutOfBounds {
/// The index we tried to access.
index: usize,
/// The length of the array.
len: usize,
},

/// We encountered a property that we didn't expect.
UnknownProperty(String),

Expand Down Expand Up @@ -82,6 +90,13 @@ impl std::fmt::Display for MerdeJsonError {
MerdeJsonError::MissingProperty(prop) => {
write!(f, "Missing property: {}", prop)
}
MerdeJsonError::IndexOutOfBounds { index, len: length } => {
write!(
f,
"Index out of bounds: index {} is not valid for length {}",
index, length
)
}
MerdeJsonError::UnknownProperty(prop) => {
write!(f, "Unknown property: {}", prop)
}
Expand Down
Loading

0 comments on commit dd147e9

Please sign in to comment.