umbral-core 0.0.4

umbral internals: ORM, migrations, routing, DB backends, the Plugin trait. Do not depend on this directly; use the `umbral` facade.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! `Slug`, `Email`, `Url` — newtype wrappers over `String` with
//! type-level guarantees and constructor validation.
//!
//! Closes BUG-11 / BUG-12 / BUG-13 from `bugs/tests/testBugs.md`.
//!
//! Each type:
//! - stores a plain `String` (so sqlx/serde round-trip without changes
//!   to the SQL layer — DDL is still `TEXT`);
//! - provides `new(s) -> Result<Self, ValidatorError>` that runs the
//!   format check;
//! - provides `unchecked(s)` for code paths that have already
//!   validated (post-DB-fetch hydration, test fixtures).
//!
//! The umbral-rest dynamic write path and the OpenAPI plugin read the
//! field-level `text_format` marker the macro emits (see
//! `FieldSpec::text_format`) to know which validator to call without
//! a downcast — the wrapper type and the marker stay in sync because
//! the macro's classifier sets both from the same single match.

use std::fmt;
use std::str::FromStr;

use serde::{Deserialize, Serialize};

/// Reason a `Slug` / `Email` / `Url` constructor rejected an input.
/// Kept narrow on purpose — every variant carries the offending
/// value so the framework can surface a structured 400 with the
/// field name and what the user submitted. Closes BUG-11/12/13.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidatorError {
    /// `Slug::new` rejected: must match `[A-Za-z0-9_-]+`.
    InvalidSlug(String),
    /// `Email::new` rejected: must contain `@` and a non-empty
    /// local + domain.
    InvalidEmail(String),
    /// `Url::new` rejected: must parse as `http(s)://...` with a
    /// non-empty host.
    InvalidUrl(String),
}

impl fmt::Display for ValidatorError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ValidatorError::InvalidSlug(s) => write!(f, "invalid slug: `{s}`"),
            ValidatorError::InvalidEmail(s) => write!(f, "invalid email: `{s}`"),
            ValidatorError::InvalidUrl(s) => write!(f, "invalid url: `{s}`"),
        }
    }
}

impl std::error::Error for ValidatorError {}

/// `[A-Za-z0-9_-]+` — URL-safe identifier. Closes BUG-11.
///
/// Stored as `TEXT`. Use `Slug::new(s)` for user input, `Slug::unchecked(s)`
/// for round-trips from already-validated sources (database, fixtures).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Slug(String);

/// `"<local>@<domain>"` — minimal structural check. Closes BUG-12.
///
/// The validation is intentionally lightweight (the framework rejects
/// obviously-broken values without trying to match every quirk of
/// RFC 5322). For stricter requirements, layer a custom permission /
/// validator on top.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Email(String);

/// `http(s)://...` — must parse via `url::Url` and have a host.
/// Closes BUG-13.
///
/// We accept the same string the application would store, parsing
/// it only for the validity check. Leaning on the `url` crate keeps
/// the rules consistent across this and any URL-aware plugin.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Url(String);

impl Slug {
    /// Validate and wrap. Empty strings and any character outside
    /// `[A-Za-z0-9_-]` reject with `ValidatorError::InvalidSlug`.
    pub fn new(s: impl Into<String>) -> Result<Self, ValidatorError> {
        let s = s.into();
        if validate_slug_str(&s) {
            Ok(Self(s))
        } else {
            Err(ValidatorError::InvalidSlug(s))
        }
    }
    /// Wrap without validation. Use only when the source is trusted
    /// (database round-trip, deterministic test fixtures).
    pub fn unchecked(s: String) -> Self {
        Self(s)
    }
    pub fn as_str(&self) -> &str {
        &self.0
    }
    pub fn into_inner(self) -> String {
        self.0
    }
}

impl Email {
    pub fn new(s: impl Into<String>) -> Result<Self, ValidatorError> {
        let s = s.into();
        if validate_email_str(&s) {
            Ok(Self(s))
        } else {
            Err(ValidatorError::InvalidEmail(s))
        }
    }
    pub fn unchecked(s: String) -> Self {
        Self(s)
    }
    pub fn as_str(&self) -> &str {
        &self.0
    }
    pub fn into_inner(self) -> String {
        self.0
    }
}

impl Url {
    pub fn new(s: impl Into<String>) -> Result<Self, ValidatorError> {
        let s = s.into();
        if validate_url_str(&s) {
            Ok(Self(s))
        } else {
            Err(ValidatorError::InvalidUrl(s))
        }
    }
    pub fn unchecked(s: String) -> Self {
        Self(s)
    }
    pub fn as_str(&self) -> &str {
        &self.0
    }
    pub fn into_inner(self) -> String {
        self.0
    }
}

// `Deref<Target = str>` would let `&Slug` flow into every `&str` call
// site, but it also enables a footgun where the inner string can be
// mutated through `DerefMut`. Stick to explicit `as_str()` for v1.

impl AsRef<str> for Slug {
    fn as_ref(&self) -> &str {
        &self.0
    }
}
impl AsRef<str> for Email {
    fn as_ref(&self) -> &str {
        &self.0
    }
}
impl AsRef<str> for Url {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for Slug {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}
impl fmt::Display for Email {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}
impl fmt::Display for Url {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl FromStr for Slug {
    type Err = ValidatorError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s.to_string())
    }
}
impl FromStr for Email {
    type Err = ValidatorError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s.to_string())
    }
}
impl FromStr for Url {
    type Err = ValidatorError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s.to_string())
    }
}

// sqlx hooks: store/load as TEXT through the inner String. The
// `Type<DB>` impl borrows the inner str so sqlx uses the same
// path it does for plain `String` / `&str` fields, no separate
// codec needed.

impl<DB: sqlx::Database> sqlx::Type<DB> for Slug
where
    String: sqlx::Type<DB>,
{
    fn type_info() -> DB::TypeInfo {
        <String as sqlx::Type<DB>>::type_info()
    }
    fn compatible(ty: &DB::TypeInfo) -> bool {
        <String as sqlx::Type<DB>>::compatible(ty)
    }
}
impl<DB: sqlx::Database> sqlx::Type<DB> for Email
where
    String: sqlx::Type<DB>,
{
    fn type_info() -> DB::TypeInfo {
        <String as sqlx::Type<DB>>::type_info()
    }
    fn compatible(ty: &DB::TypeInfo) -> bool {
        <String as sqlx::Type<DB>>::compatible(ty)
    }
}
impl<DB: sqlx::Database> sqlx::Type<DB> for Url
where
    String: sqlx::Type<DB>,
{
    fn type_info() -> DB::TypeInfo {
        <String as sqlx::Type<DB>>::type_info()
    }
    fn compatible(ty: &DB::TypeInfo) -> bool {
        <String as sqlx::Type<DB>>::compatible(ty)
    }
}

impl<'r, DB: sqlx::Database> sqlx::Decode<'r, DB> for Slug
where
    String: sqlx::Decode<'r, DB>,
{
    fn decode(
        value: <DB as sqlx::Database>::ValueRef<'r>,
    ) -> Result<Self, sqlx::error::BoxDynError> {
        let s = <String as sqlx::Decode<'r, DB>>::decode(value)?;
        Ok(Slug::unchecked(s))
    }
}
impl<'r, DB: sqlx::Database> sqlx::Decode<'r, DB> for Email
where
    String: sqlx::Decode<'r, DB>,
{
    fn decode(
        value: <DB as sqlx::Database>::ValueRef<'r>,
    ) -> Result<Self, sqlx::error::BoxDynError> {
        let s = <String as sqlx::Decode<'r, DB>>::decode(value)?;
        Ok(Email::unchecked(s))
    }
}
impl<'r, DB: sqlx::Database> sqlx::Decode<'r, DB> for Url
where
    String: sqlx::Decode<'r, DB>,
{
    fn decode(
        value: <DB as sqlx::Database>::ValueRef<'r>,
    ) -> Result<Self, sqlx::error::BoxDynError> {
        let s = <String as sqlx::Decode<'r, DB>>::decode(value)?;
        Ok(Url::unchecked(s))
    }
}

impl<'q, DB: sqlx::Database> sqlx::Encode<'q, DB> for Slug
where
    String: sqlx::Encode<'q, DB>,
{
    fn encode_by_ref(
        &self,
        buf: &mut <DB as sqlx::Database>::ArgumentBuffer<'q>,
    ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
        <String as sqlx::Encode<'q, DB>>::encode_by_ref(&self.0, buf)
    }
}
impl<'q, DB: sqlx::Database> sqlx::Encode<'q, DB> for Email
where
    String: sqlx::Encode<'q, DB>,
{
    fn encode_by_ref(
        &self,
        buf: &mut <DB as sqlx::Database>::ArgumentBuffer<'q>,
    ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
        <String as sqlx::Encode<'q, DB>>::encode_by_ref(&self.0, buf)
    }
}
impl<'q, DB: sqlx::Database> sqlx::Encode<'q, DB> for Url
where
    String: sqlx::Encode<'q, DB>,
{
    fn encode_by_ref(
        &self,
        buf: &mut <DB as sqlx::Database>::ArgumentBuffer<'q>,
    ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
        <String as sqlx::Encode<'q, DB>>::encode_by_ref(&self.0, buf)
    }
}

/// Bare-string validator used by the dynamic write path so it can
/// pre-check user input without owning the typed wrapper. The
/// `format` argument is the `FieldSpec::text_format` marker the
/// macro emits. Closes BUG-11/12/13 — validation is a single source
/// of truth.
pub fn validate_text_format(format: &str, value: &str) -> Result<(), ValidatorError> {
    match format {
        "slug" => {
            if validate_slug_str(value) {
                Ok(())
            } else {
                Err(ValidatorError::InvalidSlug(value.to_string()))
            }
        }
        "email" => {
            if validate_email_str(value) {
                Ok(())
            } else {
                Err(ValidatorError::InvalidEmail(value.to_string()))
            }
        }
        "url" => {
            if validate_url_str(value) {
                Ok(())
            } else {
                Err(ValidatorError::InvalidUrl(value.to_string()))
            }
        }
        // Unknown marker → treat as plain text. Future formats land
        // here without breaking the dynamic write path.
        _ => Ok(()),
    }
}

fn validate_slug_str(s: &str) -> bool {
    !s.is_empty()
        && s.chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
}

fn validate_email_str(s: &str) -> bool {
    let Some((local, domain)) = s.split_once('@') else {
        return false;
    };
    if local.is_empty() || domain.is_empty() {
        return false;
    }
    // Reject multiple `@` and obvious whitespace problems. Anything
    // that survives that filter goes to the SMTP server to actually
    // bounce or accept.
    if domain.contains('@') || s.contains(char::is_whitespace) {
        return false;
    }
    // A valid domain has at least one `.` and a non-empty TLD-ish
    // suffix. Loose check — `localhost` rejects, `a.b` passes.
    domain
        .rsplit_once('.')
        .map(|(left, right)| !left.is_empty() && !right.is_empty())
        .unwrap_or(false)
}

fn validate_url_str(s: &str) -> bool {
    let lower = s.to_ascii_lowercase();
    if !(lower.starts_with("http://") || lower.starts_with("https://")) {
        return false;
    }
    // Bare-bones: scheme + `://` + at least one non-`/` char.
    let after = &s[s.find("://").map(|i| i + 3).unwrap_or(s.len())..];
    let host = after.split('/').next().unwrap_or("");
    !host.is_empty() && !host.contains(char::is_whitespace) && host.contains('.')
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn slug_accepts_url_safe() {
        assert!(Slug::new("hello-world_2").is_ok());
        assert!(Slug::new("A").is_ok());
    }
    #[test]
    fn slug_rejects_empty_and_special_chars() {
        assert!(matches!(Slug::new(""), Err(ValidatorError::InvalidSlug(_))));
        assert!(matches!(
            Slug::new("hello world"),
            Err(ValidatorError::InvalidSlug(_))
        ));
        assert!(matches!(
            Slug::new("hi/there"),
            Err(ValidatorError::InvalidSlug(_))
        ));
    }
    #[test]
    fn email_accepts_structural_shape() {
        assert!(Email::new("a@b.c").is_ok());
        assert!(Email::new("user+tag@example.com").is_ok());
    }
    #[test]
    fn email_rejects_obvious_breaks() {
        assert!(matches!(
            Email::new("plain"),
            Err(ValidatorError::InvalidEmail(_))
        ));
        assert!(matches!(
            Email::new("@no-local.com"),
            Err(ValidatorError::InvalidEmail(_))
        ));
        assert!(matches!(
            Email::new("no-at"),
            Err(ValidatorError::InvalidEmail(_))
        ));
        assert!(matches!(
            Email::new("two@@sign.com"),
            Err(ValidatorError::InvalidEmail(_))
        ));
        assert!(matches!(
            Email::new("a@localhost"),
            Err(ValidatorError::InvalidEmail(_))
        ));
    }
    #[test]
    fn url_accepts_http_and_https() {
        assert!(Url::new("http://example.com/path?x=1").is_ok());
        assert!(Url::new("https://example.com/").is_ok());
    }
    #[test]
    fn url_rejects_non_http_or_missing_host() {
        assert!(matches!(
            Url::new("ftp://example.com/"),
            Err(ValidatorError::InvalidUrl(_))
        ));
        assert!(matches!(
            Url::new("https:///path"),
            Err(ValidatorError::InvalidUrl(_))
        ));
        assert!(matches!(
            Url::new("not a url"),
            Err(ValidatorError::InvalidUrl(_))
        ));
    }
    #[test]
    fn validate_text_format_dispatches() {
        assert!(validate_text_format("slug", "ok-1").is_ok());
        assert!(validate_text_format("slug", "no spaces").is_err());
        assert!(validate_text_format("email", "a@b.c").is_ok());
        assert!(validate_text_format("url", "https://x.y/").is_ok());
        // Unknown marker degrades to plain text — no error.
        assert!(validate_text_format("unknown", "anything").is_ok());
    }
}