pib-service-inventory 0.13.1

Inventory interface library to be used in pib-service
Documentation
// SPDX-FileCopyrightText: Politik im Blick developers
// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
//
// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2

use oparl_types::namespace::ErrorNamespaceUrl;
use snafu::Snafu;

#[derive(Debug, Snafu)]
#[snafu(visibility(pub))]
pub enum Error {
    #[snafu(display("Entry not found"))]
    NotFound,

    #[snafu(display("Error interfacing with inventory backend"))]
    Inventory {
        source: Box<dyn std::error::Error + Send + Sync>,
    },
}

impl Error {
    pub const fn http_status_code(&self) -> u16 {
        match self {
            Error::NotFound => 404,
            Error::Inventory { .. } => 500,
        }
    }

    pub const fn is_not_found(&self) -> bool {
        matches!(self, Self::NotFound)
    }
}

impl From<Error> for oparl_types::Error {
    fn from(value: Error) -> Self {
        oparl_types::Error {
            namespace: ErrorNamespaceUrl::Identifier,
            message: value.to_string(),
            debug: None,
            extensions: serde_json::Map::new(),
        }
    }
}