authorizenet 0.2.0

A typed Authorize.Net API client with async and blocking support.
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
//! Checks of the XSD's facets: lengths, patterns, numeric ranges and digits, and how
//! many times a list element may occur.
//!
//! Every schema type implements [`Validate`]. The client validates requests before
//! sending them, so a value the API would reject with a terse `E00003` is reported
//! with the path of the offending field instead.

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

use rust_decimal::Decimal;

use crate::types::{RawXml, XmlDate, XmlDateTime};

/// A type whose values can be checked against the schema's facets.
pub trait Validate {
    /// Checks `self` and everything inside it, collecting every violation.
    fn validate(&self) -> Result<(), ValidationError> {
        let mut path = Path::default();
        let mut violations = Vec::new();
        self.validate_into(&mut path, &mut violations);
        if violations.is_empty() {
            Ok(())
        } else {
            Err(ValidationError { violations })
        }
    }

    #[doc(hidden)]
    fn validate_into(&self, path: &mut Path, out: &mut Vec<Violation>);
}

/// One value that breaks a facet.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Violation {
    /// Where the value is, by XML element names: `transactionRequest.lineItems[0].name`.
    pub path: String,
    /// What is wrong with it.
    pub message: String,
}

impl fmt::Display for Violation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.path, self.message)
    }
}

/// A value does not satisfy the schema.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationError {
    violations: Vec<Violation>,
}

impl ValidationError {
    /// Every violation found, in document order. Never empty.
    pub fn violations(&self) -> &[Violation] {
        &self.violations
    }
}

impl fmt::Display for ValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (first, rest) = self.violations.split_first().expect("never empty");
        write!(f, "{first}")?;
        match rest.len() {
            0 => Ok(()),
            1 => write!(f, " (and 1 more violation)"),
            n => write!(f, " (and {n} more violations)"),
        }
    }
}

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

/// The location being validated, maintained by derived code.
#[doc(hidden)]
#[derive(Debug, Default)]
pub struct Path {
    segments: Vec<Segment>,
}

#[derive(Debug)]
enum Segment {
    Field(&'static str),
    Index(usize),
}

impl Path {
    pub fn push_field(&mut self, name: &'static str) {
        self.segments.push(Segment::Field(name));
    }

    pub fn push_index(&mut self, index: usize) {
        self.segments.push(Segment::Index(index));
    }

    pub fn pop(&mut self) {
        self.segments.pop();
    }

    fn render(&self) -> String {
        let mut out = String::new();
        for segment in &self.segments {
            match segment {
                Segment::Field(name) => {
                    if !out.is_empty() {
                        out.push('.');
                    }
                    out.push_str(name);
                }
                Segment::Index(i) => out.push_str(&format!("[{i}]")),
            }
        }
        out
    }
}

fn violation(path: &Path, out: &mut Vec<Violation>, message: String) {
    out.push(Violation {
        path: path.render(),
        message,
    });
}

/// Types with no facets of their own.
macro_rules! no_facets {
    ($($ty:ty),*) => {$(
        impl Validate for $ty {
            fn validate_into(&self, _path: &mut Path, _out: &mut Vec<Violation>) {}
        }
    )*};
}

no_facets!(
    String,
    bool,
    i16,
    i32,
    i64,
    u32,
    u64,
    Decimal,
    XmlDate,
    XmlDateTime,
    RawXml
);

// Facet checks called by derived code. Lengths count characters, as in XSD.

#[doc(hidden)]
pub fn check_length(
    value: &str,
    min: Option<usize>,
    max: Option<usize>,
    exact: Option<usize>,
    path: &Path,
    out: &mut Vec<Violation>,
) {
    let len = value.chars().count();
    if let Some(exact) = exact
        && len != exact
    {
        violation(
            path,
            out,
            format!("must be exactly {exact} characters, not {len}"),
        );
    }
    if let Some(min) = min
        && len < min
    {
        violation(
            path,
            out,
            format!("must be at least {min} characters, not {len}"),
        );
    }
    if let Some(max) = max
        && len > max
    {
        violation(
            path,
            out,
            format!("must be at most {max} characters, not {len}"),
        );
    }
}

/// `allowed` is the character class of a `[class]+` pattern.
#[doc(hidden)]
pub fn check_pattern(
    value: &str,
    pattern: &'static str,
    allowed: fn(char) -> bool,
    path: &Path,
    out: &mut Vec<Violation>,
) {
    if value.is_empty() || !value.chars().all(allowed) {
        violation(path, out, format!("must match the pattern {pattern}"));
    }
}

#[doc(hidden)]
pub fn check_range<T>(
    value: &T,
    min: Option<&str>,
    max: Option<&str>,
    path: &Path,
    out: &mut Vec<Violation>,
) where
    T: PartialOrd + FromStr + fmt::Display,
{
    let bound = |text: &str| {
        T::from_str(text)
            .ok()
            .unwrap_or_else(|| panic!("schema bound {text:?} must parse as the field's type"))
    };
    if let Some(min) = min
        && *value < bound(min)
    {
        violation(path, out, format!("must be at least {min}, not {value}"));
    }
    if let Some(max) = max
        && *value > bound(max)
    {
        violation(path, out, format!("must be at most {max}, not {value}"));
    }
}

/// Digit counts ignore trailing fractional zeros, as XSD does: `45.00` has 2 digits
/// and no fraction digits.
#[doc(hidden)]
pub fn check_digits(
    value: &Decimal,
    total: Option<u32>,
    fraction: Option<u32>,
    path: &Path,
    out: &mut Vec<Violation>,
) {
    let normalized = value.normalize();
    if let Some(fraction) = fraction
        && normalized.scale() > fraction
    {
        violation(
            path,
            out,
            format!(
                "must have at most {fraction} digits after the decimal point, not {}",
                normalized.scale()
            ),
        );
    }
    if let Some(total) = total {
        let digits = normalized
            .mantissa()
            .unsigned_abs()
            .to_string()
            .trim_start_matches('0')
            .len()
            .max(1) as u32;
        if digits > total {
            violation(
                path,
                out,
                format!("must have at most {total} digits, not {digits}"),
            );
        }
    }
}

#[doc(hidden)]
pub fn check_occurs(
    len: usize,
    min: Option<usize>,
    max: Option<usize>,
    path: &Path,
    out: &mut Vec<Violation>,
) {
    if let Some(min) = min
        && len < min
    {
        violation(
            path,
            out,
            format!("must have at least {min} items, not {len}"),
        );
    }
    if let Some(max) = max
        && len > max
    {
        violation(
            path,
            out,
            format!("must have at most {max} items, not {len}"),
        );
    }
}

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

    fn run(check: impl FnOnce(&Path, &mut Vec<Violation>)) -> Vec<String> {
        let mut path = Path::default();
        path.push_field("transactionRequest");
        path.push_field("lineItems");
        path.push_index(2);
        path.push_field("name");
        let mut out = Vec::new();
        check(&path, &mut out);
        out.into_iter().map(|v| v.to_string()).collect()
    }

    fn dec(text: &str) -> Decimal {
        text.parse().unwrap()
    }

    #[test]
    fn path_renders_fields_and_indexes() {
        let out = run(|p, o| check_length("abc", None, Some(2), None, p, o));
        assert_eq!(
            out,
            ["transactionRequest.lineItems[2].name: must be at most 2 characters, not 3"]
        );
    }

    #[test]
    fn length_counts_characters_not_bytes() {
        assert!(run(|p, o| check_length("ééé", None, Some(3), None, p, o)).is_empty());
    }

    #[test]
    fn exact_length() {
        assert_eq!(
            run(|p, o| check_length("US", None, None, Some(3), p, o)).len(),
            1
        );
    }

    #[test]
    fn pattern_requires_at_least_one_allowed_char() {
        let digits = |c: char| c.is_ascii_digit();
        assert!(run(|p, o| check_pattern("123", "[0-9]+", digits, p, o)).is_empty());
        assert_eq!(
            run(|p, o| check_pattern("12a", "[0-9]+", digits, p, o)).len(),
            1
        );
        assert_eq!(
            run(|p, o| check_pattern("", "[0-9]+", digits, p, o)).len(),
            1
        );
    }

    #[test]
    fn decimal_range() {
        assert_eq!(
            run(|p, o| check_range(&dec("-1.00"), Some("0.00"), None, p, o)),
            ["transactionRequest.lineItems[2].name: must be at least 0.00, not -1.00"]
        );
        assert!(run(|p, o| check_range(&dec("0.01"), Some("0.01"), None, p, o)).is_empty());
    }

    #[test]
    fn integer_range() {
        assert_eq!(
            run(|p, o| check_range(&1001, Some("1"), Some("1000"), p, o)).len(),
            1
        );
    }

    #[test]
    fn fraction_digits_ignore_trailing_zeros() {
        assert!(run(|p, o| check_digits(&dec("45.1200"), None, Some(2), p, o)).is_empty());
        assert_eq!(
            run(|p, o| check_digits(&dec("1.23456"), None, Some(4), p, o)).len(),
            1
        );
    }

    #[test]
    fn total_digits() {
        assert!(run(|p, o| check_digits(&dec("0.12345"), Some(5), None, p, o)).is_empty());
        assert_eq!(
            run(|p, o| check_digits(&dec("123.456"), Some(5), None, p, o)).len(),
            1
        );
    }

    #[test]
    fn occurrences() {
        assert_eq!(run(|p, o| check_occurs(31, None, Some(30), p, o)).len(), 1);
        assert_eq!(run(|p, o| check_occurs(0, Some(1), None, p, o)).len(), 1);
    }

    #[test]
    fn error_display_counts_the_rest() {
        let v = |path: &str| Violation {
            path: path.into(),
            message: "bad".into(),
        };
        let err = ValidationError {
            violations: vec![v("a"), v("b"), v("c")],
        };
        assert_eq!(err.to_string(), "a: bad (and 2 more violations)");
    }
}