Skip to content

Commit

Permalink
feat: every rule have one feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tu6ge committed Jun 11, 2024
1 parent 164b28e commit 7c77bc0
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
32 changes: 30 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,35 @@ repository = "https://github.com/tu6ge/valitron"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
full = ["serde/derive", "idna", "regex"]
default = []
full = [
"message",
"compare",
"confirm",
"contains",
"email",
"end_with",
"length",
"not",
"range",
"regex",
"required",
"start_with",
"trim",
]
message = ["serde/derive"]
compare = []
confirm = []
contains = []
email = ["idna"]
end_with = []
length = []
not = []
range = []
regex = ["regex_"]
required = []
start_with = []
trim = []

[package.metadata.docs.rs]
all-features = true
Expand Down Expand Up @@ -53,7 +81,7 @@ harness = false
[dependencies]
serde = { version = "^1.0" }
idna = { version = "0.5", optional = true }
regex = { version = "1", default-features = false, optional = true }
regex_ = { package="regex", version = "1", default-features = false, optional = true }

[dev-dependencies]
serde = { version = "^1.0", features = ["derive"] }
Expand Down
56 changes: 56 additions & 0 deletions src/rule/available/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,86 @@ use std::fmt::Display;

use serde::Serialize;

#[cfg(all(feature = "message", feature = "compare"))]
pub mod compare;

#[cfg(all(feature = "message", feature = "confirm"))]
pub mod confirm;

#[cfg(all(feature = "message", feature = "contains"))]
pub mod contains;

#[cfg(all(feature = "message", feature = "email"))]
pub mod email;

#[cfg(all(feature = "message", feature = "end_with"))]
pub mod end_with;

#[cfg(all(feature = "message", feature = "length"))]
pub mod length;

#[cfg(all(feature = "message", feature = "not"))]
pub mod not;

#[cfg(all(feature = "message", feature = "range"))]
pub mod range;

#[cfg(all(feature = "message", feature = "regex"))]
pub mod regex;

#[cfg(all(feature = "message", feature = "required"))]
pub mod required;

#[cfg(all(feature = "message", feature = "start_with"))]
pub mod start_with;

#[cfg(all(feature = "message", feature = "trim"))]
pub mod trim;

#[cfg(feature = "compare")]
pub use compare::{Egt, Elt, Gt, Lt};

#[cfg(feature = "confirm")]
pub use confirm::Confirm;

#[cfg(feature = "contains")]
pub use contains::Contains;

#[cfg(feature = "email")]
pub use email::Email;

#[cfg(feature = "end_with")]
pub use end_with::EndsWith;

#[cfg(feature = "length")]
pub use length::Length;

#[cfg(feature = "not")]
pub use not::Not;

#[cfg(feature = "range")]
pub use range::Range;

#[cfg(feature = "regex")]
pub use regex::Regex;

#[cfg(feature = "required")]
pub use required::Required;

#[cfg(feature = "start_with")]
pub use start_with::StartWith;

#[cfg(feature = "trim")]
pub use trim::Trim;

/// Error message, it is returned when build-in rules validate fail
#[cfg(any(feature = "full", feature = "message"))]
#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub struct Message {
kind: MessageKind,
}

#[cfg(any(feature = "full", feature = "message"))]
#[non_exhaustive]
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum MessageKind {
Expand Down Expand Up @@ -75,6 +123,7 @@ pub enum MessageKind {
Fallback(String),
}

#[cfg(any(feature = "full", feature = "message"))]
impl Serialize for MessageKind {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -97,6 +146,7 @@ impl Serialize for MessageKind {
}
}

#[cfg(any(feature = "full", feature = "message"))]
impl Message {
pub fn new(kind: MessageKind) -> Self {
Message { kind }
Expand All @@ -116,11 +166,13 @@ impl Message {
}
}

#[cfg(any(feature = "full", feature = "message"))]
impl From<Message> for String {
fn from(msg: Message) -> Self {
msg.to_string()
}
}
#[cfg(any(feature = "full", feature = "message"))]
impl From<String> for Message {
fn from(content: String) -> Self {
Self {
Expand All @@ -129,18 +181,21 @@ impl From<String> for Message {
}
}

#[cfg(any(feature = "full", feature = "message"))]
impl From<&str> for Message {
fn from(content: &str) -> Self {
content.to_string().into()
}
}

#[cfg(any(feature = "full", feature = "message"))]
impl Display for Message {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.kind.fmt(f)
}
}

#[cfg(any(feature = "full", feature = "message"))]
impl Display for MessageKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand All @@ -164,6 +219,7 @@ impl Display for MessageKind {
}
}

#[cfg(any(feature = "full", feature = "message"))]
impl PartialEq<Message> for String {
fn eq(&self, other: &Message) -> bool {
self == &other.to_string()
Expand Down
4 changes: 2 additions & 2 deletions src/rule/available/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a> Rule for Regex<'a> {
fn call(&mut self, data: &mut crate::Value) -> bool {
match data {
crate::Value::String(s) => {
let reg = regex::Regex::new(self.0)
let reg = regex_::Regex::new(self.0)
.unwrap_or_else(|_| panic!("regex \"{}\" have syntax error", self.0));
reg.is_match(s)
}
Expand All @@ -79,7 +79,7 @@ impl<'a> StringRule for Regex<'a> {
}

fn call(&mut self, data: &mut String) -> bool {
let reg = regex::Regex::new(self.0)
let reg = regex_::Regex::new(self.0)
.unwrap_or_else(|_| panic!("regex \"{}\" have syntax error", self.0));
reg.is_match(data)
}
Expand Down
17 changes: 16 additions & 1 deletion src/rule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,22 @@ use crate::{

use self::boxed::{ErasedRule, RuleIntoBoxed};

#[cfg(feature = "full")]
#[cfg(any(
feature = "full",
feature = "message",
feature = "compare",
feature = "confirm",
feature = "contains",
feature = "email",
feature = "end_with",
feature = "length",
feature = "not",
feature = "range",
feature = "regex",
feature = "required",
feature = "start_with",
feature = "trim"
))]
pub mod available;
mod boxed;
pub mod string;
Expand Down

0 comments on commit 7c77bc0

Please sign in to comment.