diff --git a/Cargo.toml b/Cargo.toml index bf703c8..3e52ef9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,7 @@ tokio = { version = "1.33", features = ["full"] } criterion = "0.5" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -edn-derive = "0.5.0" +edn-derive = { git = "https://github.com/Grinkers/edn-derive.git", branch = "no_move" } [dev-dependencies.cargo-husky] version = "1" diff --git a/benches/serialize.rs b/benches/serialize.rs index a6d2018..2693457 100644 --- a/benches/serialize.rs +++ b/benches/serialize.rs @@ -46,11 +46,12 @@ mod edn { use criterion::Criterion; use edn_derive::Serialize; - use edn_rs::{map, set}; + use edn_rs::{map, set, Serialize}; use std::collections::{BTreeMap, BTreeSet}; pub fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("edn", |b| b.iter(|| edn_rs::to_string(val()))); + let val = val(); + c.bench_function("edn", |b| b.iter(|| val.serialize())); } fn val() -> ValEdn { diff --git a/examples/serialize.rs b/examples/serialize.rs index f6280e3..47071de 100644 --- a/examples/serialize.rs +++ b/examples/serialize.rs @@ -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)] @@ -29,7 +29,7 @@ fn serialize() -> String { nothing: (), }; - edn_rs::to_string(edn) + edn.serialize() } fn main() { diff --git a/examples/simple_serialize.rs b/examples/simple_serialize.rs new file mode 100644 index 0000000..9510131 --- /dev/null +++ b/examples/simple_serialize.rs @@ -0,0 +1,31 @@ +use edn_rs::Serialize; + +struct Foo<'a> { + value: u64, + say: &'a str, +} + +impl Serialize for Foo<'_> { + fn serialize(&self) -> String { + format!("{{:value {}, :say {:?}}}", self.value, self.say) + } +} + +fn serialize() -> String { + let say = "Hello, World!"; + let foo = Foo { + value: 42, + say: say, + }; + + foo.serialize() +} + +fn main() { + println!("{}", serialize()); +} + +#[test] +fn test_serialize() { + assert_eq!(serialize(), "{:value 42, :say \"Hello, World!\"}"); +} diff --git a/src/lib.rs b/src/lib.rs index 610e766..4a59f7d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,8 +8,6 @@ extern crate alloc; #[cfg(feature = "std")] extern crate std; -use alloc::string::String; - /// Edn type implementation pub mod edn; @@ -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 { @@ -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), } /// } ///``` @@ -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>, -/// set: BTreeSet, -/// 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: T) -> String { - t.serialize() -} diff --git a/src/serialize/mod.rs b/src/serialize/mod.rs index a87aa16..7f0626c 100644 --- a/src/serialize/mod.rs +++ b/src/serialize/mod.rs @@ -12,7 +12,7 @@ use alloc::vec::Vec; /// struct YourType; /// /// impl Serialize for YourType { -/// fn serialize(self) -> String { +/// fn serialize(&self) -> String { /// format!("{:?}", self) /// } /// } @@ -20,7 +20,7 @@ use alloc::vec::Vec; /// /// Implemented for all generic types. pub trait Serialize { - fn serialize(self) -> String; + fn serialize(&self) -> String; } macro_rules! ser_primitives { @@ -28,7 +28,7 @@ macro_rules! ser_primitives { $( impl Serialize for $name { - fn serialize(self) -> String { + fn serialize(&self) -> String { format!("{:?}", self) } } @@ -40,9 +40,9 @@ impl Serialize for Vec where T: Serialize, { - fn serialize(self) -> String { + fn serialize(&self) -> String { let aux_vec = self - .into_iter() + .iter() .map(Serialize::serialize) .collect::>(); let mut s = String::new(); @@ -58,9 +58,9 @@ impl Serialize for std::collections::HashSet where T: Serialize, { - fn serialize(self) -> String { + fn serialize(&self) -> String { let aux_vec = self - .into_iter() + .iter() .map(Serialize::serialize) .collect::>(); let mut s = String::new(); @@ -76,9 +76,9 @@ impl Serialize for BTreeSet where T: Serialize, { - fn serialize(self) -> String { + fn serialize(&self) -> String { let aux_vec = self - .into_iter() + .iter() .map(Serialize::serialize) .collect::>(); let mut s = String::new(); @@ -94,9 +94,9 @@ impl Serialize for LinkedList where T: Serialize, { - fn serialize(self) -> String { + fn serialize(&self) -> String { let aux_vec = self - .into_iter() + .iter() .map(Serialize::serialize) .collect::>(); let mut s = String::new(); @@ -112,9 +112,9 @@ impl Serialize for std::collections::HashMap String { + fn serialize(&self) -> String { let aux_vec = self - .into_iter() + .iter() .map(|(k, v)| format!(":{} {}", k.replace([' ', '_'], "-"), v.serialize())) .collect::>(); let mut s = String::new(); @@ -130,9 +130,9 @@ impl Serialize for std::collections::HashMap<&st where T: Serialize, { - fn serialize(self) -> String { + fn serialize(&self) -> String { let aux_vec = self - .into_iter() + .iter() .map(|(k, v)| format!(":{} {}", k.replace([' ', '_'], "-"), v.serialize())) .collect::>(); let mut s = String::new(); @@ -147,9 +147,9 @@ impl Serialize for BTreeMap where T: Serialize, { - fn serialize(self) -> String { + fn serialize(&self) -> String { let aux_vec = self - .into_iter() + .iter() .map(|(k, v)| format!(":{} {}", k.replace([' ', '_'], "-"), v.serialize())) .collect::>(); let mut s = String::new(); @@ -164,16 +164,10 @@ impl Serialize for BTreeMap<&str, T> where T: Serialize, { - fn serialize(self) -> String { + fn serialize(&self) -> String { let aux_vec = self - .into_iter() - .map(|(k, v)| { - format!( - ":{} {}", - k.to_string().replace([' ', '_'], "-"), - v.serialize() - ) - }) + .iter() + .map(|(k, v)| format!(":{} {}", k.replace([' ', '_'], "-"), v.serialize())) .collect::>(); let mut s = String::new(); s.push('{'); @@ -187,25 +181,25 @@ where ser_primitives![i8, i16, i32, i64, u8, u16, u32, u64, f32, f64, bool]; impl Serialize for () { - fn serialize(self) -> String { + fn serialize(&self) -> String { "nil".to_string() } } impl Serialize for String { - fn serialize(self) -> String { + fn serialize(&self) -> String { format!("{self:?}") } } impl Serialize for &str { - fn serialize(self) -> String { + fn serialize(&self) -> String { format!("{self:?}") } } impl Serialize for char { - fn serialize(self) -> String { + fn serialize(&self) -> String { format!("\\{self}") } } @@ -214,8 +208,8 @@ impl Serialize for Option where T: Serialize, { - fn serialize(self) -> String { - self.map_or_else( + fn serialize(&self) -> String { + self.as_ref().map_or_else( || String::from("nil"), crate::serialize::Serialize::serialize, ) @@ -224,19 +218,19 @@ where // Complex types impl Serialize for (A,) { - fn serialize(self) -> String { + fn serialize(&self) -> String { format!("({})", self.0.serialize()) } } impl Serialize for (A, B) { - fn serialize(self) -> String { + fn serialize(&self) -> String { format!("({}, {})", self.0.serialize(), self.1.serialize()) } } impl Serialize for (A, B, C) { - fn serialize(self) -> String { + fn serialize(&self) -> String { format!( "({}, {}, {})", self.0.serialize(), @@ -247,7 +241,7 @@ impl Serialize for (A, B, C) { } impl Serialize for (A, B, C, D) { - fn serialize(self) -> String { + fn serialize(&self) -> String { format!( "({}, {}, {}, {})", self.0.serialize(), @@ -261,7 +255,7 @@ impl Serialize for (A, B impl Serialize for (A, B, C, D, E) { - fn serialize(self) -> String { + fn serialize(&self) -> String { format!( "({}, {}, {}, {}, {})", self.0.serialize(), @@ -276,7 +270,7 @@ impl Seria impl Serialize for (A, B, C, D, E, F) { - fn serialize(self) -> String { + fn serialize(&self) -> String { format!( "({}, {}, {}, {}, {}, {})", self.0.serialize(), diff --git a/tests/ser.rs b/tests/ser.rs index 55b14b6..b7cedd1 100644 --- a/tests/ser.rs +++ b/tests/ser.rs @@ -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() { @@ -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] @@ -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, }], }, }"); } }