Skip to main content

c2pa_ml/
error.rs

1use std::fmt;
2
3/// Errors returned by the crate.
4#[derive(Debug)]
5pub enum Error {
6    /// The bytes did not match any supported ML model container format.
7    UnknownFormat,
8    /// The container was structurally malformed for its detected format.
9    Malformed(String),
10    /// No C2PA manifest was present in the model.
11    NotFound,
12    /// A manifest source carried neither an embedded store nor a remote URI.
13    EmptySource,
14    /// A stored manifest reference could not be decoded (e.g. invalid Base64).
15    MalformedReference(String),
16    Io(std::io::Error),
17}
18
19impl fmt::Display for Error {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            Self::UnknownFormat => {
23                write!(f, "unrecognized ML model container format")
24            }
25            Self::Malformed(s) => write!(f, "malformed model container: {s}"),
26            Self::NotFound => write!(f, "no C2PA manifest found in model"),
27            Self::EmptySource => {
28                write!(f, "manifest source has neither an embedded store nor a URI")
29            }
30            Self::MalformedReference(s) => write!(f, "malformed manifest reference: {s}"),
31            Self::Io(e) => write!(f, "I/O error: {e}"),
32        }
33    }
34}
35
36impl std::error::Error for Error {}
37
38impl From<std::io::Error> for Error {
39    fn from(e: std::io::Error) -> Self {
40        Self::Io(e)
41    }
42}