Skip to content

Commit 6930fde

Browse files
authored
new: Add RegexSetting type. (#168)
1 parent 2a1a075 commit 6930fde

23 files changed

+176
-32
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
#### 🚀 Updates
6+
7+
- Added a `RegexSetting` type that wraps `regex::Regex` and implements additional traits so that it
8+
can be used in configs.
9+
- Requires the `type_regex` feature.
10+
- Default implementation uses `.` (match all) pattern.
11+
312
## 0.18.10
413

514
#### ⚙️ Internal

Cargo.lock

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/schematic/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ tracing = { workspace = true }
2828
garde = { version = "0.22.0", default-features = false, optional = true, features = [
2929
"regex",
3030
] }
31+
regex = { workspace = true, optional = true }
3132
serde = { workspace = true }
3233
serde_path_to_error = { version = "0.1.17", optional = true }
3334
starbase_styles = { version = "0.6.3", optional = true }
@@ -93,7 +94,7 @@ renderer_typescript = ["schema"]
9394
# Types
9495
type_chrono = ["schematic_types/chrono"]
9596
type_indexmap = ["schematic_types/indexmap"]
96-
type_regex = ["schematic_types/regex"]
97+
type_regex = ["schematic_types/regex", "dep:regex"]
9798
type_relative_path = ["schematic_types/relative_path"]
9899
type_rust_decimal = ["schematic_types/rust_decimal", "garde?/rust_decimal"]
99100
type_semver = ["schematic_types/semver"]
@@ -145,7 +146,6 @@ derive_more = { version = "2.0.1", features = ["try_into", "as_ref"] }
145146
# Types
146147
chrono = { workspace = true, features = ["serde"] }
147148
indexmap = { workspace = true }
148-
regex = { workspace = true }
149149
relative-path = { workspace = true, features = ["serde"] }
150150
rust_decimal = { workspace = true }
151151
semver = { workspace = true, features = ["serde"] }

crates/schematic/src/config/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod loader;
99
mod merger;
1010
mod parser;
1111
mod path;
12+
mod settings;
1213
mod source;
1314
#[cfg(feature = "validate")]
1415
mod validator;
@@ -23,6 +24,7 @@ pub use loader::*;
2324
pub use merger::*;
2425
pub use parser::*;
2526
pub use path::*;
27+
pub use settings::*;
2628
pub use source::*;
2729
#[cfg(feature = "validate")]
2830
pub use validator::*;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[cfg(feature = "type_regex")]
2+
mod regex;
3+
4+
#[cfg(feature = "type_regex")]
5+
pub use regex::*;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use crate::schema::{Schema, SchemaBuilder, Schematic};
2+
use regex::{Error, Regex};
3+
use serde::{Deserialize, Serialize};
4+
use std::hash::{Hash, Hasher};
5+
use std::ops::Deref;
6+
use std::str::FromStr;
7+
8+
#[derive(Clone, Debug, Deserialize, Serialize)]
9+
#[serde(try_from = "String", into = "String")]
10+
pub struct RegexSetting(pub Regex);
11+
12+
impl RegexSetting {
13+
pub fn new(value: impl AsRef<str>) -> Result<Self, Error> {
14+
Ok(Self(Regex::new(value.as_ref())?))
15+
}
16+
}
17+
18+
impl Default for RegexSetting {
19+
fn default() -> Self {
20+
Self(Regex::new(".").unwrap())
21+
}
22+
}
23+
24+
impl Deref for RegexSetting {
25+
type Target = Regex;
26+
27+
fn deref(&self) -> &Self::Target {
28+
&self.0
29+
}
30+
}
31+
32+
impl FromStr for RegexSetting {
33+
type Err = Error;
34+
35+
fn from_str(value: &str) -> Result<Self, Self::Err> {
36+
Self::new(value)
37+
}
38+
}
39+
40+
impl TryFrom<&str> for RegexSetting {
41+
type Error = Error;
42+
43+
fn try_from(value: &str) -> Result<Self, Self::Error> {
44+
Self::new(value)
45+
}
46+
}
47+
48+
impl TryFrom<String> for RegexSetting {
49+
type Error = Error;
50+
51+
fn try_from(value: String) -> Result<Self, Self::Error> {
52+
Self::new(value)
53+
}
54+
}
55+
56+
#[allow(clippy::from_over_into)]
57+
impl Into<String> for RegexSetting {
58+
fn into(self) -> String {
59+
self.to_string()
60+
}
61+
}
62+
63+
impl PartialEq<RegexSetting> for RegexSetting {
64+
fn eq(&self, other: &RegexSetting) -> bool {
65+
self.as_str() == other.as_str()
66+
}
67+
}
68+
69+
impl Eq for RegexSetting {}
70+
71+
impl Hash for RegexSetting {
72+
fn hash<H: Hasher>(&self, state: &mut H) {
73+
state.write(self.0.as_str().as_bytes());
74+
}
75+
}
76+
77+
impl Schematic for RegexSetting {
78+
fn build_schema(_: SchemaBuilder) -> Schema {
79+
SchemaBuilder::generate::<Regex>()
80+
}
81+
}

crates/schematic/tests/generator_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct GenConfig {
6464
decimal: rust_decimal::Decimal,
6565
time: chrono::NaiveTime,
6666
path: PathBuf,
67+
regex: RegexSetting,
6768
rel_path: relative_path::RelativePathBuf,
6869
url: Option<url::Url>,
6970
uuid: uuid::Uuid,

crates/schematic/tests/snapshots/generator_test__json_schema__defaults.snap

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
source: crates/schematic/tests/generator_test.rs
3-
assertion_line: 184
43
expression: "fs::read_to_string(file).unwrap()"
54
---
65
{
@@ -24,6 +23,7 @@ expression: "fs::read_to_string(file).unwrap()"
2423
"nested",
2524
"number",
2625
"path",
26+
"regex",
2727
"relPath",
2828
"string",
2929
"time",
@@ -130,6 +130,10 @@ expression: "fs::read_to_string(file).unwrap()"
130130
"type": "string",
131131
"format": "path"
132132
},
133+
"regex": {
134+
"type": "string",
135+
"format": "regex"
136+
},
133137
"relPath": {
134138
"type": "string",
135139
"format": "path"

crates/schematic/tests/snapshots/generator_test__json_schema__not_required.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ expression: "fs::read_to_string(file).unwrap()"
102102
"type": "string",
103103
"format": "path"
104104
},
105+
"regex": {
106+
"type": "string",
107+
"format": "regex"
108+
},
105109
"relPath": {
106110
"type": "string",
107111
"format": "path"

crates/schematic/tests/snapshots/generator_test__json_schema__partials.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ expression: "fs::read_to_string(file).unwrap()"
188188
}
189189
]
190190
},
191+
"regex": {
192+
"anyOf": [
193+
{
194+
"type": "string",
195+
"format": "regex"
196+
},
197+
{
198+
"type": "null"
199+
}
200+
]
201+
},
191202
"relPath": {
192203
"anyOf": [
193204
{
@@ -395,6 +406,7 @@ expression: "fs::read_to_string(file).unwrap()"
395406
"nested",
396407
"number",
397408
"path",
409+
"regex",
398410
"relPath",
399411
"string",
400412
"time",
@@ -501,6 +513,10 @@ expression: "fs::read_to_string(file).unwrap()"
501513
"type": "string",
502514
"format": "path"
503515
},
516+
"regex": {
517+
"type": "string",
518+
"format": "regex"
519+
},
504520
"relPath": {
505521
"type": "string",
506522
"format": "path"

0 commit comments

Comments
 (0)