git-bug 0.2.4

A rust library for interfacing with git-bug repositories
Documentation
// git-bug-rs - A rust library for interfacing with git-bug repositories
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of git-bug-rs/git-gub.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/agpl.txt>.

//! Concrete type of an [`Issue's`][`super::super::Issue`] Status.
use std::{fmt::Display, str::FromStr};

use serde::{Deserialize, Serialize};

#[derive(Debug, Eq, PartialEq, Copy, Clone, Deserialize, Serialize)]
/// The status of an issue.
pub enum Status {
    /// The Issue is open.
    Open,

    /// The Issue is closed.
    Closed,
}

impl FromStr for Status {
    type Err = decode::Error;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let value = match value {
            "open" => Self::Open,
            "closed" => Self::Closed,
            other => return Err(decode::Error::UnknownStatusString(other.to_owned())),
        };
        Ok(value)
    }
}
impl TryFrom<u64> for Status {
    type Error = decode::Error;

    fn try_from(value: u64) -> Result<Self, Self::Error> {
        let value = match value {
            1 => Self::Open,
            2 => Self::Closed,
            other => return Err(decode::Error::UnknownStatus(other)),
        };
        Ok(value)
    }
}

impl From<Status> for u64 {
    fn from(value: Status) -> Self {
        match value {
            Status::Open => 1,
            Status::Closed => 2,
        }
    }
}
impl Display for Status {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Status::Open => f.write_str("open"),
            Status::Closed => f.write_str("closed"),
        }
    }
}

#[allow(missing_docs)]
pub mod decode {
    #[derive(Debug, thiserror::Error)]
    pub enum Error {
        #[error("The status with id ({0}) is not known.")]
        UnknownStatus(u64),

        #[error("The status named ({0}) is not known.")]
        UnknownStatusString(String),
    }
}