Skip to main content

qubit_codec/
codec_error.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! Shared codec error type.
11
12use std::string::FromUtf8Error;
13
14use thiserror::Error;
15
16/// Result alias returned by codec operations.
17pub type CodecResult<T> = Result<T, CodecError>;
18
19/// Error returned by codec operations.
20#[derive(Debug, Error)]
21pub enum CodecError {
22    /// A configured prefix was required but missing.
23    #[error("missing required prefix '{prefix}'")]
24    MissingPrefix {
25        /// Required prefix.
26        prefix: String,
27    },
28
29    /// Hex input contained an odd number of digits.
30    #[error("hex input contains an odd number of digits: {digits}")]
31    OddHexLength {
32        /// Number of hex digits seen after normalization.
33        digits: usize,
34    },
35
36    /// Hex input contained a non-hexadecimal digit.
37    #[error("invalid hex digit '{character}' at index {index}")]
38    InvalidHexDigit {
39        /// Character index in the original input.
40        index: usize,
41        /// Invalid character.
42        character: char,
43    },
44
45    /// Base64 input was malformed.
46    #[error("invalid base64 input: {source}")]
47    InvalidBase64 {
48        /// Underlying Base64 decoder error.
49        #[from]
50        source: base64::DecodeError,
51    },
52
53    /// Percent input contained a malformed `%XX` escape.
54    #[error("invalid percent escape at index {index}")]
55    InvalidPercentEscape {
56        /// Byte index of the `%` marker in the input.
57        index: usize,
58    },
59
60    /// Decoded bytes were not valid UTF-8.
61    #[error("decoded bytes are not valid UTF-8: {source}")]
62    InvalidUtf8 {
63        /// Underlying UTF-8 conversion error.
64        #[from]
65        source: FromUtf8Error,
66    },
67}