Skip to main content

ferrin_message/
error.rs

1//! Message-layer errors.
2
3use std::path::PathBuf;
4
5/// Binary or data-URL content could not be decoded.
6#[derive(Debug, thiserror::Error)]
7#[error("invalid data content: {message}")]
8pub struct InvalidDataContentError {
9    /// Explanation.
10    pub message: String,
11    /// Underlying decoding error, if any.
12    #[source]
13    pub cause: Option<Box<dyn std::error::Error + Send + Sync>>,
14}
15
16impl InvalidDataContentError {
17    /// Creates an error from a message.
18    #[must_use]
19    pub fn new(message: impl Into<String>) -> Self {
20        Self {
21            message: message.into(),
22            cause: None,
23        }
24    }
25
26    /// Attaches the underlying cause.
27    #[must_use]
28    pub fn with_cause(mut self, cause: impl std::error::Error + Send + Sync + 'static) -> Self {
29        self.cause = Some(Box::new(cause));
30        self
31    }
32}
33
34/// A [`FileSource`](crate::FileSource) could not be converted into provider
35/// file data.
36#[derive(Debug, thiserror::Error)]
37#[non_exhaustive]
38pub enum FileSourceError {
39    /// The inline content is not valid base64 or a valid data URL.
40    #[error(transparent)]
41    InvalidDataContent(#[from] InvalidDataContentError),
42    /// A local path must be read by the caller before conversion.
43    #[error("file source path `{}` must be read before conversion", path.display())]
44    UnreadPath {
45        /// The path.
46        path: PathBuf,
47    },
48}