git_bug/entities/issue/data/
status.rs

1// git-bug-rs - A rust library for interfacing with git-bug repositories
2//
3// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
4// SPDX-License-Identifier: GPL-3.0-or-later
5//
6// This file is part of git-bug-rs/git-gub.
7//
8// You should have received a copy of the License along with this program.
9// If not, see <https://www.gnu.org/licenses/agpl.txt>.
10
11//! Concrete type of an [`Issue's`][`super::super::Issue`] Status.
12use std::{fmt::Display, str::FromStr};
13
14use serde::{Deserialize, Serialize};
15
16#[derive(Debug, Eq, PartialEq, Copy, Clone, Deserialize, Serialize)]
17/// The status of an issue.
18pub enum Status {
19    /// The Issue is open.
20    Open,
21
22    /// The Issue is closed.
23    Closed,
24}
25
26impl FromStr for Status {
27    type Err = decode::Error;
28
29    fn from_str(value: &str) -> Result<Self, Self::Err> {
30        let value = match value {
31            "open" => Self::Open,
32            "closed" => Self::Closed,
33            other => return Err(decode::Error::UnknownStatusString(other.to_owned())),
34        };
35        Ok(value)
36    }
37}
38impl TryFrom<u64> for Status {
39    type Error = decode::Error;
40
41    fn try_from(value: u64) -> Result<Self, Self::Error> {
42        let value = match value {
43            1 => Self::Open,
44            2 => Self::Closed,
45            other => return Err(decode::Error::UnknownStatus(other)),
46        };
47        Ok(value)
48    }
49}
50
51impl From<Status> for u64 {
52    fn from(value: Status) -> Self {
53        match value {
54            Status::Open => 1,
55            Status::Closed => 2,
56        }
57    }
58}
59impl Display for Status {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        match self {
62            Status::Open => f.write_str("open"),
63            Status::Closed => f.write_str("closed"),
64        }
65    }
66}
67
68#[allow(missing_docs)]
69pub mod decode {
70    #[derive(Debug, thiserror::Error)]
71    pub enum Error {
72        #[error("The status with id ({0}) is not known.")]
73        UnknownStatus(u64),
74
75        #[error("The status named ({0}) is not known.")]
76        UnknownStatusString(String),
77    }
78}