Skip to content

Commit

Permalink
feat: add serde support
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodie committed Dec 2, 2021
1 parent c0ffee5 commit c0ffee7
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 4 deletions.
20 changes: 16 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ exclude = ["fixtures", ".github", ".gitignore", "*.json"]

[dependencies]
chrono = "0.4"
serde = { version = "1.0", optional = true, features = ["derive"] }
serde_json = { version = "1.0", optional = true }

[dependencies.nom]
version = "7"
Expand All @@ -35,20 +37,30 @@ parser = ["nom"]

[[example]]
name = "parse"
path= "examples/parse.rs"
path = "examples/parse.rs"
required-features = ["parser"]

[[example]]
name = "parse_advanced"
path= "examples/parse_advanced.rs"
path = "examples/parse_advanced.rs"
required-features = ["parser"]

[[example]]
name = "ical_to_json"
path = "examples/ical_to_json.rs"
required-features = ["parser", "serde", "serde_json"]

[[example]]
name = "json_to_ical"
path = "examples/json_to_ical.rs"
required-features = ["parser", "serde", "serde_json"]

[[example]]
name = "full_circle"
path= "examples/full_circle.rs"
path = "examples/full_circle.rs"
required-features = ["parser"]

[[example]]
name = "parsed_property"
path= "examples/custom_property_parsed.rs"
path = "examples/custom_property_parsed.rs"
required-features = ["parser"]
18 changes: 18 additions & 0 deletions examples/ical_to_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![cfg(feature = "parser")]
use icalendar::parser::unfold;

mod example_utils;
use example_utils::{content_from_arg, print_with_lines};

fn main() -> Result<(), Box<dyn std::error::Error>> {
if let Some(sample) = content_from_arg()? {
let unfolded = unfold(&sample);
//print_with_lines(&unfolded);

match icalendar::parser::read_calendar(&unfolded) {
Ok(read) => println!("{}", serde_json::to_string_pretty(&read)?),
Err(error) => println!("human-readable error\n{}", error),
}
}
Ok(())
}
15 changes: 15 additions & 0 deletions examples/json_to_ical.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![cfg(feature = "parser")]
use icalendar::parser::unfold;

mod example_utils;
use example_utils::{content_from_arg, print_with_lines};

fn main() -> Result<(), Box<dyn std::error::Error>> {
if let Some(content) = content_from_arg()? {
// print_with_lines(&unfolded);

let calendar = serde_json::from_str::<icalendar::parser::Calendar>(&content)?;
println!("{}", calendar);
}
Ok(())
}
1 change: 1 addition & 0 deletions src/parser/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use core::{

/// Helpertype for reserialization
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Calendar<'a> {
pub components: Vec<Component<'a>>,
}
Expand Down
1 change: 1 addition & 0 deletions src/parser/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use crate::{

/// The parsing equivalent of [`crate::components::Component`]
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Component<'a> {
pub name: ParseString<'a>,
pub properties: Vec<Property<'a>>,
Expand Down
1 change: 1 addition & 0 deletions src/parser/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::{parsed_string::ParseString, utils::valid_key_sequence_cow};

/// Zero-copy version of [`crate::properties::Parameter`]
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Parameter<'a> {
pub key: ParseString<'a>,
pub val: Option<ParseString<'a>>,
Expand Down
1 change: 1 addition & 0 deletions src/parser/parsed_string.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::borrow::Cow;

#[derive(Debug, Eq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ParseString<'a>(Cow<'a, str>);

impl ParseString<'_> {
Expand Down
1 change: 1 addition & 0 deletions src/parser/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use nom::error::ErrorKind;

/// Zero-copy version of [`crate::properties::Property`]
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Property<'a> {
pub name: ParseString<'a>,
pub val: ParseString<'a>,
Expand Down

0 comments on commit c0ffee7

Please sign in to comment.