Skip to content

Commit

Permalink
Implementation of --no-default-features.
Browse files Browse the repository at this point in the history
  • Loading branch information
Grinkers committed Nov 7, 2023
1 parent def0c4f commit f5adbac
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jobs:
run: cargo test --example edn_to_json --features "json"
- name: example_async
run: cargo run --example async --features "async"
- name: example_no_sets
run: cargo run --example struct_from_str --no-default-features

fmt:
runs-on: ubuntu-latest
Expand Down
15 changes: 14 additions & 1 deletion src/deserialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::str::FromStr;

pub mod parse;

#[cfg(feature = "sets")]
use ordered_float::OrderedFloat;

/// public trait to be used to `Deserialize` structs.
Expand Down Expand Up @@ -73,6 +74,7 @@ impl Deserialize for () {
}
}

#[cfg(feature = "sets")]
impl Deserialize for OrderedFloat<f64> {
fn deserialize(edn: &Edn) -> Result<Self, Error> {
edn.to_float()
Expand Down Expand Up @@ -168,6 +170,7 @@ where
.ok_or_else(|| Error::Iter(format!("Could not create iter from {edn:?}")))?
.map(|e| Deserialize::deserialize(e))
.collect::<Result<Self, Error>>()?),
#[cfg(feature = "sets")]
Edn::Set(_) => Ok(edn
.iter_some()
.ok_or_else(|| Error::Iter(format!("Could not create iter from {edn:?}")))?
Expand Down Expand Up @@ -267,6 +270,7 @@ where
}
}

#[cfg(feature = "sets")]
impl<T, H> Deserialize for HashSet<T, H>
where
T: std::cmp::Eq + std::hash::Hash + Deserialize,
Expand All @@ -291,6 +295,7 @@ where
}
}

#[cfg(feature = "sets")]
impl<T> Deserialize for BTreeSet<T>
where
T: std::cmp::Eq + std::hash::Hash + std::cmp::Ord + Deserialize,
Expand Down Expand Up @@ -440,7 +445,9 @@ pub fn from_edn<T: Deserialize>(edn: &Edn) -> Result<T, Error> {
#[cfg(test)]
mod test {
use super::*;
use crate::edn::{List, Map, Set, Vector};
#[cfg(feature = "sets")]
use crate::edn::Set;
use crate::edn::{List, Map, Vector};
use crate::{hmap, hset, map, set};

#[test]
Expand All @@ -452,6 +459,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn deser_btreeset_with_error() {
let edn = "#{\"a\", 5, \"b\"}";
let err: Result<BTreeSet<u64>, Error> = from_str(edn);
Expand Down Expand Up @@ -498,6 +506,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn from_str_list_with_set() {
let edn = "(1 -10 \"2\" 3.3 :b #{true \\c})";

Expand Down Expand Up @@ -528,6 +537,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn from_str_complex_map() {
let edn = "{:a \"2\" :b [true false] :c #{:A {:a :b} nil}}";

Expand Down Expand Up @@ -633,6 +643,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn test_more_sym() {
let edn: Edn = Edn::from_str("(a \\b \"c\" 5 #{hello world})").unwrap();
let expected = Edn::List(List::new(vec![
Expand Down Expand Up @@ -722,6 +733,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn deser_btreeset() {
let set = Edn::Set(Set::new(set! {
Edn::UInt(4),
Expand All @@ -737,6 +749,7 @@ mod test {
assert_eq!(deser_set, expected);
}

#[cfg(feature = "sets")]
#[test]
fn deser_hashset() {
use ordered_float::OrderedFloat;
Expand Down
47 changes: 41 additions & 6 deletions src/deserialize/parse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::edn::{Edn, Error, List, Map, Set, Vector};
#[cfg(feature = "sets")]
use crate::edn::Set;
use crate::edn::{Edn, Error, List, Map, Vector};
use std::collections::{BTreeMap, BTreeSet};

const DELIMITERS: [char; 8] = [',', ']', '}', ')', ';', '(', '[', '{'];
Expand Down Expand Up @@ -413,6 +415,7 @@ fn read_list(chars: &mut std::iter::Enumerate<std::str::Chars>) -> Result<Edn, E
}
}

#[cfg(feature = "sets")]
fn read_set(chars: &mut std::iter::Enumerate<std::str::Chars>) -> Result<Edn, Error> {
let _discard_brackets = chars.next();
let i = chars
Expand All @@ -438,6 +441,13 @@ fn read_set(chars: &mut std::iter::Enumerate<std::str::Chars>) -> Result<Edn, Er
}
}

#[cfg(not(feature = "sets"))]
fn read_set(chars: &mut std::iter::Enumerate<std::str::Chars>) -> Result<Edn, Error> {
Err(Error::ParseEdn(format!(
"Could not parse set due to feature not being enabled"
)))
}

fn read_namespaced_map(chars: &mut std::iter::Enumerate<std::str::Chars>) -> Result<Edn, Error> {
let i = chars
.clone()
Expand Down Expand Up @@ -524,11 +534,11 @@ fn read_if_not_container_end(
#[cfg(test)]
mod test {
use super::*;
use crate::edn::{Map, Set};
use crate::edn::Map;
#[cfg(feature = "sets")]
use crate::edn::Set;
use crate::{map, set};

use ordered_float::OrderedFloat;

#[test]
fn parse_empty() {
let mut edn = "".chars().enumerate();
Expand Down Expand Up @@ -641,6 +651,7 @@ mod test {

#[test]
fn parse_number() {
use crate::edn;
let mut uint = "143".chars().enumerate();
let mut int = "-435143".chars().enumerate();
let mut f = "-43.5143".chars().enumerate();
Expand All @@ -650,15 +661,15 @@ mod test {
assert_eq!(parse_edn(int.next(), &mut int).unwrap(), Edn::Int(-435143));
assert_eq!(
parse_edn(f.next(), &mut f).unwrap(),
Edn::Double(OrderedFloat(-43.5143))
Edn::Double(edn::Double::from(-43.5143))
);
assert_eq!(
parse_edn(r.next(), &mut r).unwrap(),
Edn::Rational("43/5143".to_string())
);
assert_eq!(
parse_edn(big_f64.next(), &mut big_f64).unwrap(),
Edn::Double(OrderedFloat(1e21f64))
Edn::Double(edn::Double::from(1e21f64))
);
}

Expand Down Expand Up @@ -854,6 +865,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn parse_set() {
let mut edn = "#{true \\c 3 }".chars().enumerate();

Expand All @@ -868,6 +880,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn parse_set_with_commas() {
let mut edn = "#{true, \\c, 3,four, }".chars().enumerate();

Expand All @@ -883,6 +896,21 @@ mod test {
}

#[test]
#[cfg(not(feature = "sets"))]
fn parse_set_without_set_feature() {
let mut edn = "#{true, \\c, 3,four, }".chars().enumerate();
let res = parse(edn.next(), &mut edn);

assert_eq!(
res,
Err(Error::ParseEdn(
"Could not parse set due to feature not being enabled".to_string()
))
)
}

#[test]
#[cfg(feature = "sets")]
fn parse_comment_in_set() {
let mut edn = "#{true ; bool true in a set\n \\c 3 }".chars().enumerate();

Expand All @@ -897,6 +925,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn parse_true_false_nil_with_comments_in_set() {
let mut edn = "#{true;this is true\nfalse;this is false\nnil;this is nil\n}"
.chars()
Expand All @@ -909,6 +938,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn parse_comment_in_set_end() {
let mut edn = "#{true \\c 3; int 3 in a set\n}".chars().enumerate();

Expand All @@ -923,6 +953,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn parse_complex() {
let mut edn = "[:b ( 5 \\c #{true \\c 3 } ) ]".chars().enumerate();

Expand All @@ -944,6 +975,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn parse_comment_complex() {
let mut edn = "[:b ( 5 \\c #{true \\c; char c in a set\n3 } ) ]"
.chars()
Expand Down Expand Up @@ -994,6 +1026,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn parse_edn_with_inst() {
let mut edn =
"#{ :a :b {:c :d :date #inst \"2020-07-16T21:53:14.628-00:00\" ::c ::d} nil}"
Expand Down Expand Up @@ -1056,6 +1089,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn parse_discard_space_invalid() {
let mut edn = "#_ ,, #{hello, this will be discarded} #_{so will this} #{this is invalid"
.chars()
Expand Down Expand Up @@ -1357,6 +1391,7 @@ mod test {
}

#[test]
#[cfg(feature = "sets")]
fn parse_tagged_set() {
let mut edn = "#domain/model #{1 2 3}".chars().enumerate();
let res = parse(edn.next(), &mut edn).unwrap();
Expand Down
Loading

0 comments on commit f5adbac

Please sign in to comment.