forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBa7e1d4cTahinli2 months ago3424 commits

error.rs110 lines, 3.0 KiBHistoryRawDownload
1//! `flacenc::error`: verification wrappers and the error types the encoder
2//! hands back.
3
4use std::fmt;
5use std::marker::PhantomData;
6
7use crate::bitsink::BitSink;
8
9/// A value that has passed [`Verify::verify`].
10///
11/// Derefs to the value, which is how callers read `config.block_size` off a
12/// verified config.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct Verified<T>(T);
15
16impl<T> std::ops::Deref for Verified<T> {
17 type Target = T;
18 fn deref(&self) -> &T {
19 &self.0
20 }
21}
22
23/// Configuration structs that can check their own consistency.
24pub trait Verify: Sized {
25 /// Check every field; [`VerifyError`] names the first bad one.
26 fn verify(&self) -> Result<(), VerifyError>;
27
28 /// Wrap into [`Verified`], or hand the value back with the reason.
29 fn into_verified(self) -> Result<Verified<Self>, (Self, VerifyError)> {
30 match self.verify() {
31 Ok(()) => Ok(Verified(self)),
32 Err(e) => Err((self, e)),
33 }
34 }
35}
36
37/// A configuration field that is out of range, with the path to it.
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct VerifyError {
40 components: Vec<String>,
41 reason: String,
42}
43
44impl VerifyError {
45 /// An error for an invalid field `component`.
46 pub fn new(component: &str, reason: &str) -> Self {
47 VerifyError {
48 components: vec![component.to_owned()],
49 reason: reason.to_owned(),
50 }
51 }
52
53 /// Prefix the field path with an enclosing struct's field name.
54 pub fn within(mut self, component: &str) -> Self {
55 self.components.insert(0, component.to_owned());
56 self
57 }
58}
59
60impl fmt::Display for VerifyError {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62 write!(
63 f,
64 "verification error: `{}` is not valid. reason: {}",
65 self.components.join("."),
66 self.reason
67 )
68 }
69}
70
71impl std::error::Error for VerifyError {}
72
73/// Encoding failed: either the source or the configuration.
74#[derive(Debug, Clone, PartialEq, Eq)]
75pub struct EncodeError(pub(crate) String);
76
77impl fmt::Display for EncodeError {
78 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79 f.write_str(&self.0)
80 }
81}
82
83impl std::error::Error for EncodeError {}
84
85/// Writing a component to a [`BitSink`] failed.
86///
87/// Generic over the sink, as the incumbent is, so `write` signatures match; our
88/// only sink is infallible, so this is never constructed in practice.
89#[derive(Debug, Clone, PartialEq, Eq)]
90pub struct OutputError<S: BitSink> {
91 reason: String,
92 sink: PhantomData<fn() -> S>,
93}
94
95impl<S: BitSink> OutputError<S> {
96 pub(crate) fn new(reason: impl Into<String>) -> Self {
97 OutputError {
98 reason: reason.into(),
99 sink: PhantomData,
100 }
101 }
102}
103
104impl<S: BitSink> fmt::Display for OutputError<S> {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 f.write_str(&self.reason)
107 }
108}
109
110impl<S: BitSink + fmt::Debug> std::error::Error for OutputError<S> {}