From 91d2652027cacf1c21b214a0a114f024ce7c126a Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Mon, 25 Jul 2022 22:48:17 +0300 Subject: [PATCH] refac: restructure modules Closes #20 --- Cargo.toml | 11 +++---- examples/calc.rs | 3 +- examples/dotenv.rs | 4 ++- src/agg.rs | 2 ++ src/core.rs | 19 ----------- src/core/prim.rs | 14 -------- src/lib.rs | 11 ++++++- src/{core => }/low.rs | 0 src/{core => }/low/trim.rs | 4 --- src/std.rs | 8 +++++ src/{core/prim => std}/bool.rs | 0 src/{core/prim => std}/number.rs | 1 - src/structs.rs | 10 ++++++ src/{core/tuple.rs => structs/pair.rs} | 32 +++++++----------- src/{core/vec.rs => structs/sep_vec.rs} | 43 +++++++++++-------------- 15 files changed, 69 insertions(+), 93 deletions(-) create mode 100644 src/agg.rs delete mode 100644 src/core/prim.rs rename src/{core => }/low.rs (100%) rename src/{core => }/low/trim.rs (86%) create mode 100644 src/std.rs rename src/{core/prim => std}/bool.rs (100%) rename src/{core/prim => std}/number.rs (97%) create mode 100644 src/structs.rs rename src/{core/tuple.rs => structs/pair.rs} (82%) rename src/{core/vec.rs => structs/sep_vec.rs} (78%) diff --git a/Cargo.toml b/Cargo.toml index 1850fbf..87630ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,12 +17,9 @@ all-features = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -prim = ["number", "bool"] -number = [] -bool = [] -vec = [] -tuple = [] low-level = [] +aggs = [] +structs = [] [dependencies] @@ -31,8 +28,8 @@ maintenance = { status = "actively-developed" } [[example]] name = "calc" -required-features = ["vec", "number"] +required-features = ["structs"] [[example]] name = "dotenv" -required-features = ["vec", "tuple", "low-level"] +required-features = ["structs", "low-level"] diff --git a/examples/calc.rs b/examples/calc.rs index 27ea9e4..996e2f6 100644 --- a/examples/calc.rs +++ b/examples/calc.rs @@ -1,4 +1,5 @@ -use estring::{EString, SepVec}; +use estring::structs::SepVec; +use estring::EString; type PlusVec = SepVec; type MulVec = SepVec; diff --git a/examples/dotenv.rs b/examples/dotenv.rs index a13d441..9c71cb4 100644 --- a/examples/dotenv.rs +++ b/examples/dotenv.rs @@ -1,4 +1,6 @@ -use estring::{EString, Pair, SepVec, Trim}; +use estring::low::Trim; +use estring::structs::{Pair, SepVec}; +use estring::EString; const DOTENV_CONTENT: &str = " DATABASE_URL=postgres://user:password@localhost:5432/recipes diff --git a/src/agg.rs b/src/agg.rs new file mode 100644 index 0000000..2d6b12f --- /dev/null +++ b/src/agg.rs @@ -0,0 +1,2 @@ +//! This module will contain aggregate functions (Sum, Product, etc) +//! diff --git a/src/core.rs b/src/core.rs index 79c991c..f4bc8f9 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,25 +1,6 @@ //! Contains the ``EString`` type, as well as the basic implementation of conversions to //! string types //! -#[cfg(any(feature = "number", feature = "bool"))] -pub mod prim; -#[cfg(any(feature = "number", feature = "bool"))] -pub use prim::*; - -#[cfg(feature = "vec")] -pub mod vec; -#[cfg(feature = "vec")] -pub use vec::*; - -#[cfg(feature = "tuple")] -pub mod tuple; -#[cfg(feature = "tuple")] -pub use tuple::*; - -#[cfg(feature = "low-level")] -pub mod low; -#[cfg(feature = "low-level")] -pub use low::*; use crate::ParseError; use std::convert::Infallible; diff --git a/src/core/prim.rs b/src/core/prim.rs deleted file mode 100644 index ff19878..0000000 --- a/src/core/prim.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! Contains the implementations to primitive types (number, boolean) -//! -//! **NOTE**: Require the enabling of the same-name features -//! - -#[cfg(feature = "bool")] -mod bool; -#[cfg(feature = "bool")] -pub use self::bool::*; - -#[cfg(feature = "number")] -mod number; -#[cfg(feature = "number")] -pub use self::number::*; diff --git a/src/lib.rs b/src/lib.rs index d5bc9b4..90a9ac2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,8 +31,17 @@ #![allow(clippy::module_name_repetitions)] #![warn(missing_docs)] -pub mod core; mod error; +pub mod core; +pub mod std; + +#[cfg(feature = "aggs")] +pub mod agg; +#[cfg(feature = "low-level")] +pub mod low; +#[cfg(feature = "structs")] +pub mod structs; + pub use crate::core::*; pub use crate::error::ParseError; diff --git a/src/core/low.rs b/src/low.rs similarity index 100% rename from src/core/low.rs rename to src/low.rs diff --git a/src/core/low/trim.rs b/src/low/trim.rs similarity index 86% rename from src/core/low/trim.rs rename to src/low/trim.rs index 068610f..f2b32e6 100644 --- a/src/core/low/trim.rs +++ b/src/low/trim.rs @@ -1,9 +1,5 @@ use crate::core::EString; -//===========================================================================// -// TRIM // -//===========================================================================// - /// Wrapper that allow to trim substring before continue /// /// **NOTE**: Required the enabling of the `low-level` feature. diff --git a/src/std.rs b/src/std.rs new file mode 100644 index 0000000..3d33818 --- /dev/null +++ b/src/std.rs @@ -0,0 +1,8 @@ +//! Contains implementations for standard types (`bool`, numbers, `Option`, etc.) +//! + +mod bool; +mod number; + +pub use self::bool::*; +pub use number::*; diff --git a/src/core/prim/bool.rs b/src/std/bool.rs similarity index 100% rename from src/core/prim/bool.rs rename to src/std/bool.rs diff --git a/src/core/prim/number.rs b/src/std/number.rs similarity index 97% rename from src/core/prim/number.rs rename to src/std/number.rs index 1462cdc..0a3d034 100644 --- a/src/core/prim/number.rs +++ b/src/std/number.rs @@ -4,7 +4,6 @@ use crate::core::EString; macro_rules! from_env_string_numbers_impl { ($($ty:ty),+$(,)?) => { $( - #[cfg(feature = "number")] impl TryFrom for $ty { type Error = <$ty as std::str::FromStr>::Err; diff --git a/src/structs.rs b/src/structs.rs new file mode 100644 index 0000000..00f1e8d --- /dev/null +++ b/src/structs.rs @@ -0,0 +1,10 @@ +//! Contains the predefined types (``SepVec``, ``Pair``, etc.) +//! +//! **NOTE**: Require the enabling the `structs` feature. +//! + +mod pair; +mod sep_vec; + +pub use pair::*; +pub use sep_vec::*; diff --git a/src/core/tuple.rs b/src/structs/pair.rs similarity index 82% rename from src/core/tuple.rs rename to src/structs/pair.rs index 519ee51..5c70305 100644 --- a/src/core/tuple.rs +++ b/src/structs/pair.rs @@ -1,6 +1,4 @@ -//! Contains the implementations to tuple type -//! -//! **NOTE**: Require the enabling of the `tuple` features +//! Contains the implementations to pair tuple type //! use crate::core::EString; @@ -87,6 +85,7 @@ where #[cfg(test)] mod tests { use super::*; + use crate::structs::SepVec; type EqPair = Pair; - use super::*; - - type LineVec = SepVec; - - #[test] - fn should_parse_vec_of_pairs() { - let estr = EString::from( - "foo=bar + #[test] + fn should_parse_vec_of_pairs() { + let estr = EString::from( + "foo=bar hello=bar", - ); - match estr.parse::>>() { - Ok(res) => assert_eq!(res, SepVec(vec![Pair("foo", "bar"), Pair("hello", "bar"),])), - _ => unreachable!(), - }; - } + ); + match estr.parse::>>() { + Ok(res) => assert_eq!(res, SepVec(vec![Pair("foo", "bar"), Pair("hello", "bar"),])), + _ => unreachable!(), + }; } } diff --git a/src/core/vec.rs b/src/structs/sep_vec.rs similarity index 78% rename from src/core/vec.rs rename to src/structs/sep_vec.rs index f2a0f49..b48ea7a 100644 --- a/src/core/vec.rs +++ b/src/structs/sep_vec.rs @@ -1,7 +1,5 @@ //! Contains the implementations to vec type //! -//! **NOTE**: Require the enabling of the `vec` features -//! use crate::core::EString; use std::fmt::Write; @@ -78,6 +76,7 @@ where #[cfg(test)] mod tests { use super::*; + use crate::ParseError; const COMMA: char = ','; const SEMI: char = ';'; @@ -122,29 +121,23 @@ d,e"; }; } - #[cfg(feature = "number")] - mod numbers { - use super::*; - use crate::ParseError; + #[test] + fn should_parse_into_num_vec() { + let estr = EString::from("1,2,3,4,5"); + match estr.parse::>() { + Ok(res) => assert_eq!(*res, vec![1, 2, 3, 4, 5]), + _ => unreachable!(), + }; + } - #[test] - fn should_parse_into_num_vec() { - let estr = EString::from("1,2,3,4,5"); - match estr.parse::>() { - Ok(res) => assert_eq!(*res, vec![1, 2, 3, 4, 5]), - _ => unreachable!(), - }; - } - - #[test] - fn should_throw_parse_vec_error() { - let estr = EString::from("1,2,3,4,5"); - match estr.parse::>() { - Err(ParseError(orig)) => { - assert_eq!(orig, String::from("1,2,3,4,5")); - } - _ => unreachable!(), - }; - } + #[test] + fn should_throw_parse_vec_error() { + let estr = EString::from("1,2,3,4,5"); + match estr.parse::>() { + Err(ParseError(orig)) => { + assert_eq!(orig, String::from("1,2,3,4,5")); + } + _ => unreachable!(), + }; } }