uxar 0.1.3

Opinionated Rust web framework built on Axum for Postgres-backed JSON APIs
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use std::{borrow::Cow, collections::BTreeMap, ops::Deref, fmt};

use axum::{extract::{FromRequest, FromRequestParts, Request}, http::{HeaderMap, StatusCode, header, request::Parts}, response::{Html, IntoResponse, Response}};

pub use uxar_macros::Validate;

pub use super::validators::*;

/// Represents a single validation failure with error code, message, and optional params.
///
/// # Example
/// ```ignore
/// let err = ValidationError::new("min_length", "Must be at least 3 characters");
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationError {
    pub code: Cow<'static, str>,
    pub message: Cow<'static, str>,
    pub params: Vec<(Cow<'static, str>, String)>,
}
impl ValidationError {
    pub fn new(code: impl Into<Cow<'static, str>>, msg: impl Into<Cow<'static, str>>) -> Self {
        Self { code: code.into(), message: msg.into(), params: Vec::new() }
    }

    pub fn custom(msg: impl Into<Cow<'static, str>>) -> Self {
        Self::new("custom", msg)
    }
}

/// A segment in a validation path, representing field access, array indexing, or map key.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PathSeg {
    Field(Cow<'static, str>),
    Index(usize),
    Key(String),
}

/// A validation path tracking the location of an error in nested structures.
///
/// Paths are built from segments (field names, array indices, map keys) and allow
/// precise error reporting for complex nested data.
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Path {
    segs: Vec<PathSeg>,
}
impl Path {
    /// Creates a root path (empty path).
    pub fn root() -> Self { Self { segs: Vec::new() } }
    
    /// Returns true if this is the root path.
    pub fn is_root(&self) -> bool { self.segs.is_empty() }
    
    /// Returns the path segments.
    pub fn segments(&self) -> &[PathSeg] { &self.segs }

    pub fn to_string(&self) -> String {
        self.segs.iter().map(|s| match s {
            PathSeg::Field(f) => f.to_string(),
            PathSeg::Index(i) => i.to_string(),
            PathSeg::Key(k) => k.clone(),
        }).collect::<Vec<_>>().join(".")
    }

    /// Returns a new path with the given segment prepended.
    pub fn prefixed(self, prefix: PathSeg) -> Self {
        let mut segs = Vec::with_capacity(self.segs.len() + 1);
        segs.push(prefix);
        segs.extend(self.segs);
        Self { segs }
    }

    pub fn at_field(mut self, name: impl Into<Cow<'static, str>>) -> Self {
        self.segs.push(PathSeg::Field(name.into()));
        self
    }

    pub fn at_index(mut self, idx: usize) -> Self {
        self.segs.push(PathSeg::Index(idx));
        self
    }

    pub fn at_key(mut self, key: impl Into<String>) -> Self {
        self.segs.push(PathSeg::Key(key.into()));
        self
    }
}

/// A validation error at a specific path in the data structure.
#[derive(Debug, Clone)]
pub struct ValidationIssue {
    pub path: Path,
    pub invalid: ValidationError,
}

/// Collection of validation issues, typically returned from `Validate::validate()`.
///
/// Can be converted to flat or nested JSON structures for API responses.
///
/// # Example
/// ```ignore
/// let mut report = ValidationReport::empty();
/// report.push_root(ValidationError::new("invalid", "Something is wrong"));
/// let json = report.to_nested_map();
/// ```
#[derive(Debug, Clone, Default)]
pub struct ValidationReport {
    pub issues: Vec<ValidationIssue>,
}

impl ValidationReport {
    /// Creates an empty report.
    pub fn empty() -> Self { Self { issues: Vec::new() } }
    
    /// Returns true if there are no validation errors.
    pub fn is_empty(&self) -> bool { self.issues.is_empty() }

    pub fn push(&mut self, path: Path, invalid: ValidationError) {
        self.issues.push(ValidationIssue { path, invalid });
    }

    pub fn push_root(&mut self, invalid: ValidationError) {
        self.push(Path::root(), invalid);
    }

    pub fn merge(&mut self, other: ValidationReport, prefix: Option<PathSeg>) {
        for mut issue in other.issues {
            if let Some(p) = &prefix {
                issue.path = issue.path.prefixed(p.clone());
            }
            self.issues.push(issue);
        }
    }

    pub fn has_error(&self, field: &str) -> bool {
        self.issues.iter().any(|i| i.path.to_string() == field)
    }

    /// Merges another report into this one.
    pub fn extend(&mut self, other: ValidationReport) {
        self.issues.extend(other.issues);
    }

    /// Prefix all issue paths with a segment (field/index/key).
    /// Useful when validating nested structures.
    pub fn prefix(mut self, seg: PathSeg) -> Self {
        for iss in &mut self.issues {
            let mut new_segs = Vec::with_capacity(iss.path.segs.len() + 1);
            new_segs.push(seg.clone());
            new_segs.extend(iss.path.segs.iter().cloned());
            iss.path.segs = new_segs;
        }
        self
    }

    /// Prefixes all paths with a field name.
    pub fn at_field(self, name: impl Into<Cow<'static, str>>) -> Self {
        self.prefix(PathSeg::Field(name.into()))
    }
    
    /// Prefixes all paths with an array index.
    pub fn at_index(self, idx: usize) -> Self {
        self.prefix(PathSeg::Index(idx))
    }
    
    /// Prefixes all paths with a map key.
    pub fn at_key(self, key: impl Into<String>) -> Self {
        self.prefix(PathSeg::Key(key.into()))
    }

    /// Optional convenience: collapse to DRF-ish flat `{field: [msgs]}` for non-nested paths only.
    pub fn to_field_map_flat(&self) -> BTreeMap<String, Vec<String>> {
        let mut m: BTreeMap<String, Vec<String>> = BTreeMap::new();
        for iss in &self.issues {
            if iss.path.is_root() {
                m.entry("non_field_errors".into())
                    .or_default()
                    .push(iss.invalid.message.to_string());
                continue;
            }
            // Only accept single Field("x") paths for this flat view.
            if let [PathSeg::Field(f)] = iss.path.segments() {
                m.entry(f.to_string())
                    .or_default()
                    .push(iss.invalid.message.to_string());
            }
        }
        m
    }

    /// Consuming variant of `to_field_map_flat` which takes ownership.
    pub fn into_field_map_flat(self) -> BTreeMap<String, Vec<String>> {
        let mut m: BTreeMap<String, Vec<String>> = BTreeMap::new();
        for iss in self.issues {
            if iss.path.is_root() {
                m.entry("non_field_errors".into())
                    .or_default()
                    .push(iss.invalid.message.to_string());
                continue;
            }
            if let [PathSeg::Field(f)] = iss.path.segments() {
                m.entry(f.to_string())
                    .or_default()
                    .push(iss.invalid.message.to_string());
            }
        }
        m
    }

    /// Produce a nested JSON-like map/array structure from full `Path`s.
    /// - Objects for `Field`/`Key` segments
    /// - Arrays for `Index` segments
    /// Leaves are arrays of messages (strings).
    pub fn to_nested_map(&self) -> serde_json::Value {
        let mut root = serde_json::Value::Object(serde_json::Map::new());

        fn insert_at(cur: &mut serde_json::Value, segs: &[PathSeg], msg: &serde_json::Value) {
            if segs.is_empty() {
                return;
            }
            match &segs[0] {
                PathSeg::Field(f) => {
                    let key = f.to_string();
                    if segs.len() == 1 {
                        if let Some(map) = cur.as_object_mut() {
                            let entry = map.entry(key).or_insert_with(|| serde_json::Value::Array(vec![]));
                            if let serde_json::Value::Array(arr) = entry {
                                arr.push(msg.clone());
                            }
                        }
                    } else {
                        if let Some(map) = cur.as_object_mut() {
                            let entry = map
                                .entry(key)
                                .or_insert_with(|| serde_json::Value::Object(serde_json::Map::new()));
                            if !entry.is_object() {
                                *entry = serde_json::Value::Object(serde_json::Map::new());
                            }
                            insert_at(entry, &segs[1..], msg);
                        }
                    }
                }
                PathSeg::Key(k) => {
                    let key = k.clone();
                    if segs.len() == 1 {
                        if let Some(map) = cur.as_object_mut() {
                            let entry = map.entry(key).or_insert_with(|| serde_json::Value::Array(vec![]));
                            if let serde_json::Value::Array(arr) = entry {
                                arr.push(msg.clone());
                            }
                        }
                    } else {
                        if let Some(map) = cur.as_object_mut() {
                            let entry = map
                                .entry(key)
                                .or_insert_with(|| serde_json::Value::Object(serde_json::Map::new()));
                            if !entry.is_object() {
                                *entry = serde_json::Value::Object(serde_json::Map::new());
                            }
                            insert_at(entry, &segs[1..], msg);
                        }
                    }
                }
                PathSeg::Index(idx) => {
                    if !cur.is_array() {
                        *cur = serde_json::Value::Array(vec![]);
                    }
                    if let Some(arr) = cur.as_array_mut() {
                        while arr.len() <= *idx {
                            arr.push(serde_json::Value::Null);
                        }

                        if let Some(elem) = arr.get_mut(*idx) {
                            if segs.len() == 1 {
                                if elem.is_null() {
                                    *elem = serde_json::Value::Array(vec![]);
                                }
                                if let serde_json::Value::Array(a) = elem {
                                    a.push(msg.clone());
                                }
                            } else {
                                if !elem.is_object() {
                                    *elem = serde_json::Value::Object(serde_json::Map::new());
                                }
                                insert_at(elem, &segs[1..], msg);
                            }
                        }
                    }
                }
            }
        }

        for iss in &self.issues {
            let msg = serde_json::Value::String(iss.invalid.message.to_string());
            if iss.path.is_root() {
                if let Some(map) = root.as_object_mut() {
                    let entry = map
                        .entry("non_field_errors")
                        .or_insert_with(|| serde_json::Value::Array(vec![]));
                    if let serde_json::Value::Array(arr) = entry {
                        arr.push(msg);
                    }
                }
                continue;
            }
            insert_at(&mut root, iss.path.segments(), &msg);
        }

        root
    }
}

impl fmt::Display for ValidationReport {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_empty() {
            return write!(f, "validation passed");
        }
        write!(f, "validation failed with {} error(s)", self.issues.len())
    }
}

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

/// Structural validation trait for types that can validate themselves.
///
/// # Example
/// ```ignore
/// impl Validate for MyStruct {
///     fn validate(&self) -> Result<(), ValidationReport> {
///         let mut report = ValidationReport::empty();\n///         if self.name.is_empty() {
///             report.push_root(ValidationError::new(\"required\", \"Name is required\"));
///         }
///         if report.is_empty() {
///             Ok(())
///         } else {
///             Err(report)
///         }
///     }
/// }
/// ```
pub trait Validate {
    fn validate(&self) -> Result<(), ValidationReport>;
}

/// Helper trait for applying validators to fields, handling Option automatically.
pub trait AsValidationTarget {
    type Target: ?Sized;
    fn as_validation_target(&self) -> Option<&Self::Target>;
}

impl<T> AsValidationTarget for Option<T> {
    type Target = T;
    fn as_validation_target(&self) -> Option<&Self::Target> {
        self.as_ref()
    }
}

impl AsValidationTarget for String {
    type Target = String;
    fn as_validation_target(&self) -> Option<&Self::Target> {
        Some(self)
    }
}

impl AsValidationTarget for &str {
    type Target = str;
    fn as_validation_target(&self) -> Option<&Self::Target> {
        Some(self)
    }
}

impl<T> AsValidationTarget for Vec<T> {
    type Target = Vec<T>;
    fn as_validation_target(&self) -> Option<&Self::Target> {
        Some(self)
    }
}

impl<T> AsValidationTarget for Box<T> {
    type Target = T;
    fn as_validation_target(&self) -> Option<&Self::Target> {
        Some(self)
    }
}

macro_rules! impl_as_validation_target {
    ($($t:ty),*) => {
        $(
            impl AsValidationTarget for $t {
                type Target = $t;
                fn as_validation_target(&self) -> Option<&Self::Target> {
                    Some(self)
                }
            }
        )*
    };
}

impl_as_validation_target!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64, bool);

impl<T: Validate> Validate for Option<T> {
    fn validate(&self) -> Result<(), ValidationReport> {
        match self {
            Some(v) => v.validate(),
            None => Ok(()),
        }
    }
}

impl<T: Validate> Validate for Vec<T> {
    fn validate(&self) -> Result<(), ValidationReport> {
        let mut report = ValidationReport::empty();
        for (i, v) in self.iter().enumerate() {
            if let Err(r) = v.validate() {
                report.extend(r.at_index(i));
            }
        }
        if report.is_empty() {
            Ok(())
        } else {
            Err(report)
        }
    }
}

impl<T: Validate> Validate for Box<T> {
    fn validate(&self) -> Result<(), ValidationReport> {
        self.as_ref().validate()
    }
}


/// Wrapper that validates extracted data before allowing access.
/// Use as `Valid<Json<T>>`, `Valid<Query<T>>`, or `Valid<Path<T>>`.
#[derive(Debug, Clone)]
pub struct Valid<E>(pub E);

impl<E> Valid<E> {
    /// Extract the inner value, consuming the wrapper.
    pub fn into_inner(self) -> E {
        self.0
    }
}

impl<E, T> Deref for Valid<E>
where
    E: Deref<Target = T>,
{
    type Target = T;
    fn deref(&self) -> &Self::Target {
        &*self.0
    }
}

/// Rejection type for `Valid<E>` extractor, forwarding inner rejections or validation failures.
#[derive(Debug)]
pub enum ValidRejection<R> {
    Inner(R),          // forwarded extractor rejection
    Invalid(Response), // our validation failure
}

impl<R: IntoResponse> IntoResponse for ValidRejection<R> {
    fn into_response(self) -> Response {
        match self {
            ValidRejection::Inner(r) => r.into_response(),
            ValidRejection::Invalid(resp) => resp,
        }
    }
}


fn wants_json(headers: &HeaderMap) -> bool {
    let accept = headers
        .get(header::ACCEPT)
        .and_then(|v| v.to_str().ok())
        .unwrap_or("*/*");

    // If client explicitly wants html, respect it.
    if accept.contains("text/html") {
        return false;
    }

    // Otherwise default to JSON (API-first).
    accept.contains("application/json") || accept.contains("*/*") || accept.contains("application/*")
}


fn drf_to_html(drf: &str) -> String {
    format!(
        "<!doctype html><html><body>\
         <h1>Validation error</h1>\
         <pre>{}</pre>\
         </body></html>",
        drf
    )
}

fn invalid_response(headers: &HeaderMap, errs: &ValidationReport) -> Response {
    let drf = errs.to_nested_map();
    if wants_json(headers) {
        (StatusCode::UNPROCESSABLE_ENTITY, axum::Json(drf)).into_response()
    } else {
        let drf = serde_json::to_string_pretty(&drf).unwrap_or_else(|_| "{}".to_string());
        (StatusCode::UNPROCESSABLE_ENTITY, Html(drf_to_html(&drf))).into_response()
    }
}

/// ---------- FromRequestParts (Query / Path / Headers) ----------
impl<S, E, T> FromRequestParts<S> for Valid<E>
where
    S: Send + Sync,
    E: FromRequestParts<S> + Deref<Target = T> + Send,
    T: Validate,
    <E as FromRequestParts<S>>::Rejection: IntoResponse,
{
    type Rejection = ValidRejection<<E as FromRequestParts<S>>::Rejection>;

    async fn from_request_parts(
        parts: &mut Parts,
        state: &S,
    ) -> Result<Self, Self::Rejection> {
        let extracted = E::from_request_parts(parts, state)
            .await
            .map_err(ValidRejection::Inner)?;

        match extracted.validate(){
            Ok(()) => Ok(Valid(extracted)),
            Err(errs) => Err(ValidRejection::Invalid(
                invalid_response(&parts.headers, &errs),
            )),
        }    
    }
}

/// ---------- FromRequest (Json / Form / Bytes) ----------
impl<S, E, T> FromRequest<S> for Valid<E>
where
    S: Send + Sync,
    E: FromRequest<S> + Deref<Target = T> + Send,
    T: Validate,
    <E as FromRequest<S>>::Rejection: IntoResponse,
{
    type Rejection = ValidRejection<<E as FromRequest<S>>::Rejection>;

    async fn from_request(
        req: Request,
        state: &S,
    ) -> Result<Self, Self::Rejection> {
        let headers = req.headers().clone();

        let extracted = E::from_request(req, state)
            .await
            .map_err(ValidRejection::Inner)?;

        match extracted.validate(){
            Ok(()) => Ok(Valid(extracted)),
            Err(errs) => Err(ValidRejection::Invalid(
                invalid_response(&headers, &errs),
            )),
        }
        
    }
}