From 0aae7a73119cd8c0359b4235663b97e6cc0ab2f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABlle=20Huisman?= Date: Sun, 15 Feb 2026 00:46:36 +0100 Subject: [PATCH] feat: use `constant-string` crate --- Cargo.lock | 18 ++- Cargo.toml | 1 + packages/fortifier-macros-tests/Cargo.toml | 1 + .../tests/validations/custom/options_pass.rs | 5 +- packages/fortifier/Cargo.toml | 4 +- packages/fortifier/src/error_code.rs | 117 ------------------ packages/fortifier/src/lib.rs | 1 - .../src/validations/email_address.rs | 5 +- packages/fortifier/src/validations/length.rs | 5 +- .../fortifier/src/validations/phone_number.rs | 5 +- packages/fortifier/src/validations/range.rs | 4 +- packages/fortifier/src/validations/regex.rs | 5 +- packages/fortifier/src/validations/url.rs | 5 +- 13 files changed, 35 insertions(+), 141 deletions(-) delete mode 100644 packages/fortifier/src/error_code.rs diff --git a/Cargo.lock b/Cargo.lock index 09f7759..7727533 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -175,9 +175,9 @@ dependencies = [ [[package]] name = "bytes" -version = "1.11.0" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" +checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" [[package]] name = "cc" @@ -215,6 +215,16 @@ dependencies = [ "windows-link", ] +[[package]] +name = "constant-string" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff33a2f7482fb56d66e3f53cb8b8f776283291bb71bcdc8d73974291a800af10" +dependencies = [ + "serde", + "utoipa", +] + [[package]] name = "convert_case" version = "0.10.0" @@ -330,6 +340,7 @@ version = "0.2.11" dependencies = [ "cfg-if", "chrono", + "constant-string", "email_address", "fortifier-macros", "indexmap", @@ -401,6 +412,7 @@ name = "fortifier-macros-tests" version = "0.2.11" dependencies = [ "chrono", + "constant-string", "email_address", "fortifier", "indexmap", @@ -1762,7 +1774,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 8ec918b..bcacb6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ version = "0.2.11" [workspace.dependencies] chrono = "0.4.42" +constant-string = "0.0.2" email_address = { version = "0.2.9", default-features = false } fortifier = { path = "./packages/fortifier", version = "0.2.0" } fortifier-macros = { path = "./packages/fortifier-macros", version = "0.2.0" } diff --git a/packages/fortifier-macros-tests/Cargo.toml b/packages/fortifier-macros-tests/Cargo.toml index 4f28d36..dfc7430 100644 --- a/packages/fortifier-macros-tests/Cargo.toml +++ b/packages/fortifier-macros-tests/Cargo.toml @@ -20,6 +20,7 @@ ignored = ["chrono", "rust_decimal", "uuid"] [dev-dependencies] chrono.workspace = true +constant-string.workspace = true email_address.workspace = true fortifier = { workspace = true, features = [ "all-types", diff --git a/packages/fortifier-macros-tests/tests/validations/custom/options_pass.rs b/packages/fortifier-macros-tests/tests/validations/custom/options_pass.rs index 1a9e3cb..af00811 100644 --- a/packages/fortifier-macros-tests/tests/validations/custom/options_pass.rs +++ b/packages/fortifier-macros-tests/tests/validations/custom/options_pass.rs @@ -1,4 +1,5 @@ -use fortifier::{Validate, error_code}; +use constant_string::constant_string; +use fortifier::Validate; use serde::{Deserialize, Serialize}; #[derive(Deserialize, Serialize, Validate)] @@ -35,7 +36,7 @@ struct CustomData<'a> { strip_to_two_options_from_three: Option>>, } -error_code!(CustomErrorCode, CUSTOM_ERROR_CODE, "custom"); +constant_string!(CustomErrorCode, CUSTOM_ERROR_CODE, "custom"); #[derive(Debug, Deserialize, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] diff --git a/packages/fortifier/Cargo.toml b/packages/fortifier/Cargo.toml index d06a1d1..c46218c 100644 --- a/packages/fortifier/Cargo.toml +++ b/packages/fortifier/Cargo.toml @@ -26,16 +26,18 @@ regex = ["dep:regex"] serde = [ "dep:serde", "chrono?/serde", + "constant-string/serde", "email_address?/serde_support", "fortifier-macros?/serde", "uuid?/serde", ] url = ["dep:url"] -utoipa = ["dep:utoipa", "fortifier-macros?/utoipa"] +utoipa = ["dep:utoipa", "constant-string/utoipa", "fortifier-macros?/utoipa"] uuid = ["dep:uuid"] [dependencies] chrono = { workspace = true, optional = true } +constant-string.workspace = true email_address = { workspace = true, default-features = false, optional = true } fortifier-macros = { workspace = true, optional = true } indexmap = { workspace = true, optional = true } diff --git a/packages/fortifier/src/error_code.rs b/packages/fortifier/src/error_code.rs deleted file mode 100644 index caeecb7..0000000 --- a/packages/fortifier/src/error_code.rs +++ /dev/null @@ -1,117 +0,0 @@ -#[cfg(all(feature = "serde", feature = "utoipa"))] -/// Implement an error code. -#[macro_export] -macro_rules! error_code { - ($name:ident, $code_name:ident, $code:literal) => { - $crate::error_code_base!($name, $code_name, $code); - $crate::error_code_serde!($name, $code_name, $code); - $crate::error_code_utoipa!($name, $code_name, $code); - }; -} - -#[cfg(all(feature = "serde", not(feature = "utoipa")))] -/// Implement an error code. -#[macro_export] -macro_rules! error_code { - ($name:ident, $code_name:ident, $code:literal) => { - $crate::error_code_base!($name, $code_name, $code); - $crate::error_code_serde!($name, $code_name, $code); - }; -} - -#[cfg(all(not(feature = "serde"), feature = "utoipa"))] -/// Implement an error code. -#[macro_export] -macro_rules! error_code { - ($name:ident, $code_name:ident, $code:literal) => { - $crate::error_code_base!($name, $code_name, $code); - $crate::error_code_utoipa!($name, $code_name, $code); - }; -} - -#[cfg(all(not(feature = "serde"), not(feature = "utoipa")))] -/// Implement an error code. -#[macro_export] -macro_rules! error_code { - ($name:ident, $code_name:ident, $code:literal) => { - $crate::error_code_base!($name, $code_name, $code); - }; -} - -/// Implement an error code. -#[macro_export] -macro_rules! error_code_base { - ($name:ident, $code_name:ident, $code:literal) => { - const $code_name: &str = $code; - - /// Email address error code. - #[derive(Eq, PartialEq)] - pub struct $name; - - impl Default for $name { - fn default() -> Self { - Self - } - } - - impl ::std::ops::Deref for $name { - type Target = str; - - fn deref(&self) -> &Self::Target { - $code_name - } - } - - impl ::std::fmt::Debug for $name { - fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { - ::std::fmt::Debug::fmt(&**self, f) - } - } - }; -} - -/// Implement [`serde`] traits for an error code. -#[cfg(feature = "serde")] -#[macro_export] -macro_rules! error_code_serde { - ($name:ident, $code_name:ident, $code:literal) => { - impl<'de> ::serde::Deserialize<'de> for $name { - fn deserialize(deserializer: D) -> Result - where - D: ::serde::Deserializer<'de>, - { - deserializer - .deserialize_any($crate::serde::MustBeStrVisitor($code_name)) - .map(|()| Self) - } - } - - impl ::serde::Serialize for $name { - fn serialize(&self, serializer: S) -> Result - where - S: ::serde::Serializer, - { - serializer.serialize_str($code_name) - } - } - }; -} - -/// Implement [`utoipa`] traits for an error code. -#[cfg(feature = "utoipa")] -#[macro_export] -macro_rules! error_code_utoipa { - ($name:ident, $code_name:ident, $code:literal) => { - impl ::utoipa::PartialSchema for $name { - fn schema() -> ::utoipa::openapi::RefOr<::utoipa::openapi::schema::Schema> { - ::utoipa::openapi::schema::ObjectBuilder::new() - .schema_type(::utoipa::openapi::schema::Type::String) - .enum_values(Some([$code_name])) - .build() - .into() - } - } - - impl ::utoipa::ToSchema for $name {} - }; -} diff --git a/packages/fortifier/src/lib.rs b/packages/fortifier/src/lib.rs index 83b6547..0dd832a 100644 --- a/packages/fortifier/src/lib.rs +++ b/packages/fortifier/src/lib.rs @@ -3,7 +3,6 @@ //! Fortifier. mod error; -mod error_code; mod integrations; mod validate; mod validations; diff --git a/packages/fortifier/src/validations/email_address.rs b/packages/fortifier/src/validations/email_address.rs index 1035102..fc1f962 100644 --- a/packages/fortifier/src/validations/email_address.rs +++ b/packages/fortifier/src/validations/email_address.rs @@ -7,12 +7,11 @@ use std::{ sync::Arc, }; +use constant_string::constant_string; use email_address::EmailAddress; pub use email_address::Options as EmailAddressOptions; -use crate::error_code; - -error_code!( +constant_string!( EmailAddressErrorCode, EMAIL_ADDRESS_ERROR_CODE, "emailAddress" diff --git a/packages/fortifier/src/validations/length.rs b/packages/fortifier/src/validations/length.rs index 98a4f09..456b2f6 100644 --- a/packages/fortifier/src/validations/length.rs +++ b/packages/fortifier/src/validations/length.rs @@ -8,12 +8,11 @@ use std::{ sync::Arc, }; +use constant_string::constant_string; #[cfg(feature = "indexmap")] use indexmap::{IndexMap, IndexSet}; -use crate::error_code; - -error_code!(LengthErrorCode, LENGTH_ERROR_CODE, "length"); +constant_string!(LengthErrorCode, LENGTH_ERROR_CODE, "length"); /// Length validation error. #[derive(Debug, Eq, PartialEq)] diff --git a/packages/fortifier/src/validations/phone_number.rs b/packages/fortifier/src/validations/phone_number.rs index 2b5b2a3..a81ddcd 100644 --- a/packages/fortifier/src/validations/phone_number.rs +++ b/packages/fortifier/src/validations/phone_number.rs @@ -7,12 +7,11 @@ use std::{ sync::Arc, }; +use constant_string::constant_string; pub use phonenumber::country::Id as PhoneNumberCountry; use phonenumber::{ParseError, PhoneNumber}; -use crate::error_code; - -error_code!(PhoneNumberErrorCode, PHONE_NUMBER_ERROR_CODE, "phoneNumber"); +constant_string!(PhoneNumberErrorCode, PHONE_NUMBER_ERROR_CODE, "phoneNumber"); /// Phone number validation error. #[derive(Debug, Eq, PartialEq)] diff --git a/packages/fortifier/src/validations/range.rs b/packages/fortifier/src/validations/range.rs index cf67397..c1bebb7 100644 --- a/packages/fortifier/src/validations/range.rs +++ b/packages/fortifier/src/validations/range.rs @@ -6,9 +6,9 @@ use std::{ sync::Arc, }; -use crate::error_code; +use constant_string::constant_string; -error_code!(RangeErrorCode, RANGE_ERROR_CODE, "range"); +constant_string!(RangeErrorCode, RANGE_ERROR_CODE, "range"); /// Range validation error. #[derive(Debug, Eq, PartialEq)] diff --git a/packages/fortifier/src/validations/regex.rs b/packages/fortifier/src/validations/regex.rs index 071b3ad..7999438 100644 --- a/packages/fortifier/src/validations/regex.rs +++ b/packages/fortifier/src/validations/regex.rs @@ -7,10 +7,9 @@ use std::{ sync::{Arc, LazyLock}, }; +use constant_string::constant_string; use regex::Regex; -use crate::error_code; - /// Convert to a regular expression. pub trait AsRegex { /// Convert to a regular expression. @@ -38,7 +37,7 @@ where } } -error_code!(RegexErrorCode, REGEX_ERROR_CODE, "regex"); +constant_string!(RegexErrorCode, REGEX_ERROR_CODE, "regex"); /// Regular expression validation error. #[derive(Debug, Eq, PartialEq)] diff --git a/packages/fortifier/src/validations/url.rs b/packages/fortifier/src/validations/url.rs index c19739e..4a08eef 100644 --- a/packages/fortifier/src/validations/url.rs +++ b/packages/fortifier/src/validations/url.rs @@ -7,11 +7,10 @@ use std::{ sync::Arc, }; +use constant_string::constant_string; use url::{ParseError, Url}; -use crate::error_code; - -error_code!(UrlErrorCode, URL_ERROR_CODE, "url"); +constant_string!(UrlErrorCode, URL_ERROR_CODE, "url"); /// URL validation error. #[derive(Debug, Eq, PartialEq)]