Skip to main content

c2pa_text_binding/
error.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use std::fmt;
4
5/// Errors produced by the text soft-binding algorithms.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum Error {
8    /// Input has too little content for the requested algorithm (e.g. not
9    /// enough word boundaries to place a watermark payload).
10    ContentTooShort,
11    /// A fingerprint value could not be produced.
12    GenerationFailed(String),
13    /// A fingerprint comparison failed.
14    MatchFailed(String),
15    /// Reed-Solomon erasure coding/decoding failed.
16    Coding(String),
17    /// The watermark payload was present but its content-binding HMAC did not
18    /// verify against the recomputed content hash (transfer or tamper).
19    TagMismatch,
20    /// The watermark could not be recovered (too many stripped positions).
21    WatermarkUnrecoverable,
22    /// A caller-supplied argument was malformed.
23    InvalidInput(String),
24}
25
26impl fmt::Display for Error {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Self::ContentTooShort => {
30                write!(f, "text content too short for this soft-binding algorithm")
31            }
32            Self::GenerationFailed(s) => write!(f, "fingerprint generation failed: {s}"),
33            Self::MatchFailed(s) => write!(f, "fingerprint match failed: {s}"),
34            Self::Coding(s) => write!(f, "reed-solomon coding failed: {s}"),
35            Self::TagMismatch => write!(
36                f,
37                "watermark content-binding tag did not verify (transferred or modified content)"
38            ),
39            Self::WatermarkUnrecoverable => {
40                write!(
41                    f,
42                    "watermark could not be recovered from remaining positions"
43                )
44            }
45            Self::InvalidInput(s) => write!(f, "invalid input: {s}"),
46        }
47    }
48}
49
50impl std::error::Error for Error {}