Skip to content

Commit

Permalink
Remove edn::to_string() in favor of Serialize trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
Grinkers committed Dec 23, 2023
1 parent d51121e commit e6c5e86
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 41 deletions.
4 changes: 2 additions & 2 deletions examples/serialize.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use edn_derive::Serialize;
use edn_rs::{hmap, hset, map, set};
use edn_rs::{hmap, hset, map, set, Serialize};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

#[derive(Debug, Clone, Serialize)]
Expand Down Expand Up @@ -29,7 +29,7 @@ fn serialize() -> String {
nothing: (),
};

edn_rs::to_string(&edn)
edn.serialize()
}

fn main() {
Expand Down
38 changes: 2 additions & 36 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

use alloc::string::String;

/// Edn type implementation
pub mod edn;

Expand All @@ -21,7 +19,7 @@ pub mod edn;
/// ```rust
/// use std::collections::{BTreeMap, BTreeSet};
/// use edn_derive::Serialize;
/// use edn_rs::{set, map, edn::Edn};
/// use edn_rs::{set, map, edn::Edn, Serialize};
///
/// #[derive(Serialize)]
/// struct ExampleEdn {
Expand All @@ -35,7 +33,7 @@ pub mod edn;
/// set: set!{3i64, 4i64, 5i64},
/// tuples: (3i32, true, 'd')
/// };
/// println!("{}", edn_rs::to_string(&edn));
/// println!("{}", edn.serialize());
/// // { :map {:this-is-a-key ["with", "many", "keys"]}, :set #{3, 4, 5}, :tuples (3, true, \d), }
/// }
///```
Expand Down Expand Up @@ -105,35 +103,3 @@ pub use edn::Error as EdnError;
pub use edn::Set;
pub use edn::{Edn, List, Map, Vector};
pub use serialize::Serialize;

/// Function for converting Rust types into EDN Strings.
/// For it to work, the type must implement the Serialize trait.
/// Use `#[derive(Serialize)]` from `edn-derive` crate.
///
/// Example:
/// ```rust
/// use std::collections::{BTreeMap, BTreeSet};
/// use edn_derive::Serialize;
/// use edn_rs::{set, map, edn::Edn};
///
/// #[derive(Debug, Serialize)]
/// struct ExampleEdn {
/// map: BTreeMap<String, Vec<String>>,
/// set: BTreeSet<i64>,
/// tuples: (i32, bool, char),
/// }
///
/// fn main() {
/// let edn = ExampleEdn {
/// map: map!{"this is a key".to_string() => vec!["with".to_string(), "many".to_string(), "keys".to_string()]},
/// set: set!{3i64, 4i64, 5i64},
/// tuples: (3i32, true, 'd')
/// };
/// println!("{}", edn_rs::to_string(&edn));
/// // { :map {:this-is-a-key ["with", "many", "keys"]}, :set #{3, 4, 5}, :tuples (3, true, \d), }
/// }
///```
#[allow(clippy::needless_doctest_main)]
pub fn to_string<T: Serialize>(t: &T) -> String {
t.serialize()
}
6 changes: 3 additions & 3 deletions tests/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

use edn_derive::Serialize;
use edn_rs::{hmap, hset, map, set};
use edn_rs::{hmap, hset, map, set, Serialize};

#[test]
fn serializes_a_complex_structure() {
Expand All @@ -24,7 +24,7 @@ mod tests {
tuples: (3i32, true, 'd'),
};

assert_eq!(edn_rs::to_string(&edn), "{ :btreemap {:this-is-a-key [\"with\", \"many\", \"keys\"]}, :btreeset #{3, 4, 5}, :hashmap {:this-is-a-key [\"with\", \"many\", \"keys\"]}, :hashset #{3}, :tuples (3, true, \\d), }");
assert_eq!(edn.serialize(), "{ :btreemap {:this-is-a-key [\"with\", \"many\", \"keys\"]}, :btreeset #{3, 4, 5}, :hashmap {:this-is-a-key [\"with\", \"many\", \"keys\"]}, :hashset #{3}, :tuples (3, true, \\d), }");
}

#[test]
Expand Down Expand Up @@ -54,7 +54,7 @@ mod tests {
},
};

assert_eq!(edn_rs::to_string(&edn), "{ :value 3.4, :bar { :value \"data\", :foo-vec [{ :value false, }, { :value true, }], }, }");
assert_eq!(edn.serialize(), "{ :value 3.4, :bar { :value \"data\", :foo-vec [{ :value false, }, { :value true, }], }, }");
}
}

Expand Down

0 comments on commit e6c5e86

Please sign in to comment.