amazon_cloudfront_client_routing_lib/
errors.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use std::fmt;
5
6/// Error struct used when decoding a client routing label key of an improper
7/// length.
8///
9/// # Examples:
10/// ```
11/// use amazon_cloudfront_client_routing_lib::errors::DecodeLengthError;
12///
13/// let error = DecodeLengthError {
14///     num_chars: 10,
15///     expected_num_chars: 29,
16/// };
17///
18/// assert_eq!("Passed 10 - expected 29 characters", error.to_string());
19/// ```
20#[derive(Debug, Copy, Clone)]
21pub struct DecodeLengthError {
22    pub num_chars: usize,
23    pub expected_num_chars: usize,
24}
25
26impl std::error::Error for DecodeLengthError {}
27
28impl fmt::Display for DecodeLengthError {
29    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30        write!(
31            f,
32            "Passed {} - expected {} characters",
33            self.num_chars, self.expected_num_chars,
34        )
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::DecodeLengthError;
41
42    #[test]
43    fn validate_decode_length_error_text() {
44        let error = DecodeLengthError {
45            num_chars: 10,
46            expected_num_chars: 29,
47        };
48
49        assert_eq!(error.to_string(), "Passed 10 - expected 29 characters");
50    }
51}