Lightweight tagged data library.
1//! Errors for producing and consuming tags.
2
3#[cfg(doc)]
4use crate::parse::Or;
5#[cfg(doc)]
6use crate::storage::Storage;
7#[cfg(doc)]
8use crate::tag::MultipartTag;
9#[cfg(doc)]
10use crate::tag::Tag;
11#[cfg(doc)]
12use crate::TagManager;
13use std::error::Error as StdError;
14use std::fmt::Display;
15use std::fmt::Formatter;
16use std::fmt::Result as FmtResult;
17#[cfg(doc)]
18use std::sync::Mutex;
19
20/// Error arising during parsing of new [`Tag`]s.
21#[derive(Debug)]
22#[non_exhaustive]
23pub enum ParseError {
24 /// Can't create an empty tag.
25 EmptyTag,
26
27 /// Key-value tag is missing a key.
28 MissingKey,
29
30 /// Key-value tag is missing a value.
31 MissingValue,
32
33 /// Key-value tag is ambiguous; key-value tags must have one separator.
34 AmbiguousKeyValueTag,
35
36 /// Tag didn't match a regular expression.
37 TagDidntMatchRegex,
38
39 /// Tag is more characters long than allowed.
40 TagTooManyChars,
41
42 /// Tag is more bytes long than allowed.
43 TagTooManyBytes,
44
45 /// Could not lock the parser prior to parsing.
46 CouldNotLock,
47
48 /// Tried to parse a single-part [`MultipartTag`].
49 SinglePartMultipart,
50
51 /// Failed an [`Or`] match.
52 FailedOr(Box<ParseError>, Box<ParseError>),
53
54 /// An underlying storage error arose.
55 StorageError(StorageError),
56}
57
58impl From<StorageError> for ParseError {
59 fn from(e: StorageError) -> Self {
60 ParseError::StorageError(e)
61 }
62}
63
64impl Display for ParseError {
65 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
66 match self {
67 ParseError::EmptyTag => write!(f, "can't create an empty tag"),
68 ParseError::MissingKey => write!(f, "missing key in a key-value tag"),
69 ParseError::MissingValue => write!(f, "missing value in a key-value tag"),
70 ParseError::AmbiguousKeyValueTag => {
71 write!(f, "ambiguous key-tag value; should have just one separator")
72 }
73 ParseError::TagDidntMatchRegex => write!(f, "tag didn't match the regular expression"),
74 ParseError::TagTooManyChars => write!(f, "tag is too many characters long"),
75 ParseError::TagTooManyBytes => write!(f, "tag is too many bytes long"),
76 ParseError::CouldNotLock => write!(f, "could not lock parser"),
77 ParseError::SinglePartMultipart => {
78 write!(f, "can't accept a single-part multipart tag")
79 }
80 ParseError::FailedOr(e1, e2) => {
81 write!(f, "failed two parsers with errors '{e1}' and '{e2}'")
82 }
83 ParseError::StorageError(e) => write!(f, "{e}"),
84 }
85 }
86}
87
88impl StdError for ParseError {
89 fn source(&self) -> Option<&(dyn StdError + 'static)> {
90 match self {
91 ParseError::StorageError(e) => Some(e),
92 _ => None,
93 }
94 }
95}
96
97/// Errors arising when resolving [`Tag`]s.
98#[derive(Debug)]
99#[non_exhaustive]
100pub enum ResolveError {
101 /// Tag wasn't found in the [`TagManager`].
102 TagNotFound,
103
104 /// Key wasn't found in the [`TagManager`].
105 KeyNotFound,
106
107 /// Value wasn't found in the [`TagManager`].
108 ValueNotFound,
109
110 /// Part wasn't found in the [`TagManager`].
111 PartNotFound,
112
113 /// An underlying [`Storage`] error occurred.
114 StorageError(StorageError),
115}
116
117impl From<StorageError> for ResolveError {
118 fn from(e: StorageError) -> Self {
119 ResolveError::StorageError(e)
120 }
121}
122
123impl Display for ResolveError {
124 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
125 match self {
126 ResolveError::TagNotFound => write!(f, "tag wasn't found in tag manager"),
127 ResolveError::KeyNotFound => write!(f, "key wasn't found in tag manager"),
128 ResolveError::ValueNotFound => write!(f, "value wasn't found in tag manager"),
129 ResolveError::PartNotFound => write!(f, "part wasn't found in tag manager"),
130 ResolveError::StorageError(e) => write!(f, "{e}"),
131 }
132 }
133}
134
135impl StdError for ResolveError {
136 fn source(&self) -> Option<&(dyn StdError + 'static)> {
137 match self {
138 ResolveError::StorageError(e) => Some(e),
139 _ => None,
140 }
141 }
142}
143
144/// Errors arising when interacting with [`Storage`]s.
145#[derive(Debug)]
146#[non_exhaustive]
147pub enum StorageError {
148 /// Failed to lock the storage, likely because the [`Mutex`] is poisoned.
149 CouldNotLock,
150}
151
152impl Display for StorageError {
153 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
154 match self {
155 StorageError::CouldNotLock => write!(f, "could not lock storage"),
156 }
157 }
158}
159
160impl StdError for StorageError {}