diff --git a/hugr-sexpr/src/lib.rs b/hugr-sexpr/src/lib.rs index c433e661a..3eaae3d06 100644 --- a/hugr-sexpr/src/lib.rs +++ b/hugr-sexpr/src/lib.rs @@ -50,41 +50,46 @@ //! In particular, the [`output::Output`] and [`input::Input`] traits can be derived //! automatically for structs with named fields. //! -//! ``` -//! # use hugr_sexpr::input::Input; -//! # use hugr_sexpr::output::Output; -//! #[derive(Debug, PartialEq, Input, Output)] -//! pub struct Person { -//! name: String, -//! #[sexpr(required)] -//! company: String, -//! #[sexpr(optional)] -//! birthday: Option, -//! #[sexpr(repeated)] -//! #[sexpr(rename = "email")] -//! email_addresses: Vec, -//! } -//! -//! let person = Person { -//! name: "John Doe".to_string(), -//! company: "ACME".to_string(), -//! birthday: None, -//! email_addresses: vec![ -//! "john@doe.com".to_string(), -//! "john.doe@acme.com".to_string() -//! ], -//! }; -//! -//! let sexpr = r#" -//! "John Doe" -//! (company "ACME") -//! (email "john@doe.com") -//! (email "john.doe@acme.com") -//! "#; -//! -//! let imported = hugr_sexpr::from_str::(sexpr).unwrap(); -//! assert_eq!(imported, person); -//! ``` +#[cfg_attr( + feature = "derive", + doc = r##" +``` +# use hugr_sexpr::input::Input; +# use hugr_sexpr::output::Output; +#[derive(Debug, PartialEq, Input, Output)] +pub struct Person { + name: String, + #[sexpr(required)] + company: String, + #[sexpr(optional)] + birthday: Option, + #[sexpr(repeated)] + #[sexpr(rename = "email")] + email_addresses: Vec, +} + +let person = Person { + name: "John Doe".to_string(), + company: "ACME".to_string(), + birthday: None, + email_addresses: vec![ + "john@doe.com".to_string(), + "john.doe@acme.com".to_string() + ], +}; + +let sexpr = r#" + "John Doe" + (company "ACME") + (email "john@doe.com") + (email "john.doe@acme.com") +"#; + +let imported = hugr_sexpr::from_str::(sexpr).unwrap(); +assert_eq!(imported, person); +``` +"## +)] use ordered_float::OrderedFloat; use smol_str::SmolStr; use std::fmt::Display; diff --git a/hugr-sexpr/tests/derive_input.rs b/hugr-sexpr/tests/derive_input.rs index fb86872dd..55d794439 100644 --- a/hugr-sexpr/tests/derive_input.rs +++ b/hugr-sexpr/tests/derive_input.rs @@ -1,6 +1,7 @@ use hugr_sexpr::{from_str, input::Input, read::ReadError, Symbol, Value}; #[test] +#[cfg(feature = "derive")] pub fn positional() { #[derive(Input)] struct Test { @@ -15,6 +16,7 @@ pub fn positional() { } #[test] +#[cfg(feature = "derive")] pub fn optional_given() { #[derive(Input)] struct Test { @@ -27,6 +29,8 @@ pub fn optional_given() { assert_eq!(test.field.unwrap(), "string"); } +#[test] +#[cfg(feature = "derive")] pub fn optional_absent() { #[derive(Input)] struct Test { @@ -40,6 +44,7 @@ pub fn optional_absent() { } #[test] +#[cfg(feature = "derive")] pub fn optional_duplicate() { #[derive(Debug, Input)] struct Test { @@ -57,6 +62,7 @@ pub fn optional_duplicate() { } #[test] +#[cfg(feature = "derive")] pub fn required_given() { #[derive(Input)] struct Test { @@ -69,12 +75,14 @@ pub fn required_given() { assert_eq!(test.field, "string"); } +#[test] +#[cfg(feature = "derive")] pub fn required_absent() { #[derive(Input)] struct Test { #[allow(dead_code)] - #[sexpr(optional)] - field: Option, + #[sexpr(required)] + field: String, } let result = from_str::(r#""#); @@ -83,6 +91,7 @@ pub fn required_absent() { } #[test] +#[cfg(feature = "derive")] pub fn required_duplicate() { #[derive(Input)] struct Test { @@ -97,6 +106,7 @@ pub fn required_duplicate() { } #[test] +#[cfg(feature = "derive")] pub fn repeated() { #[derive(Input)] struct Test { @@ -117,6 +127,7 @@ pub fn repeated() { } #[test] +#[cfg(feature = "derive")] pub fn resursive_field() { #[derive(Input, PartialEq, Eq, Debug)] struct Outer { diff --git a/hugr-sexpr/tests/derive_output.rs b/hugr-sexpr/tests/derive_output.rs index 63d1257f1..6e6046eb7 100644 --- a/hugr-sexpr/tests/derive_output.rs +++ b/hugr-sexpr/tests/derive_output.rs @@ -1,6 +1,7 @@ use hugr_sexpr::{from_str, output::Output, to_values, Value}; #[test] +#[cfg(feature = "derive")] pub fn positional() { #[derive(Output)] pub struct Test { @@ -19,6 +20,7 @@ pub fn positional() { } #[test] +#[cfg(feature = "derive")] pub fn required() { #[derive(Output)] pub struct Test { @@ -38,6 +40,7 @@ pub fn required() { } #[test] +#[cfg(feature = "derive")] pub fn optional_given() { #[derive(Output)] pub struct Test { @@ -57,6 +60,7 @@ pub fn optional_given() { } #[test] +#[cfg(feature = "derive")] pub fn optional_absent() { #[derive(Output)] pub struct Test { @@ -76,6 +80,7 @@ pub fn optional_absent() { } #[test] +#[cfg(feature = "derive")] pub fn repeated() { #[derive(Output)] struct Test {