scallion 0.1.0-rc.2

Library for identification of license texts based on the SPDX license list
Documentation
use std::io;
use std::path::PathBuf;

use crate::store::{CompressionFormat, CACHE_VERSION};

/// SPDX license list cache handling error.
#[derive(Debug, thiserror::Error)]
#[expect(missing_docs)]
pub enum Error {
    /// IO Error
    #[error("{}: {:?}", inner, path)]
    IO { inner: io::Error, path: Option<PathBuf> },
    /// SPDX license list data parsing error
    #[error("Failed to parse SPDX license list data file '{}' ({})", path.to_string_lossy(), inner)]
    SpdxData { inner: serde_json::Error, path: PathBuf },
    /// Unexpected cache version (created with an older or newer scallion version)
    #[error(
        "Unexpected cache version (expected '{}-????', found '{}'",
        String::from_utf8_lossy(CACHE_VERSION),
        String::from_utf8_lossy(header)
    )]
    CacheVersion { header: Vec<u8> },
    /// `MessagePack` deserialization failure
    #[error("Cache deserialization failure: {}", inner)]
    CacheParse {
        #[from]
        inner: rmp_serde::decode::Error,
    },
    /// `MessagePack` serialization failure
    #[error("Cache serialization failure: {}", inner)]
    CacheWrite {
        #[from]
        inner: rmp_serde::encode::Error,
    },
    /// Unsupported compression format
    #[error("Unsupported compression format (scallion was not compiled with {} support)", cf)]
    CacheFormat { cf: CompressionFormat },
}

impl Error {
    #[must_use]
    pub(crate) const fn io(inner: io::Error, path: Option<PathBuf>) -> Self {
        Error::IO { inner, path }
    }

    #[must_use]
    pub(crate) const fn spdx(inner: serde_json::Error, path: PathBuf) -> Self {
        Error::SpdxData { inner, path }
    }

    #[must_use]
    pub(crate) const fn cache_version(header: Vec<u8>) -> Self {
        Error::CacheVersion { header }
    }

    #[must_use]
    #[cfg(not(all(feature = "zstd", feature = "gzip")))]
    pub(crate) const fn cache_format(cf: CompressionFormat) -> Self {
        Error::CacheFormat { cf }
    }
}