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) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// 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`] Label.

use std::fmt::Display;

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use simd_json::{borrowed, derived::ValueTryIntoString, owned};

/// A label for an issue.
///
/// This can be an arbitrary string, but should never contain a newline.
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize)]
pub struct Label(pub(crate) String);

impl From<&str> for Label {
    fn from(value: &str) -> Self {
        Self(value.to_owned())
    }
}

impl TryFrom<owned::Value> for Label {
    type Error = value_parse::Error;

    fn try_from(value: owned::Value) -> Result<Self, Self::Error> {
        let string = value
            .try_into_string()
            .map_err(|err| value_parse::Error::StrExpected { err })?;

        Ok(Self(string))
    }
}

impl<'a> From<&'a Label> for borrowed::Value<'a> {
    fn from(val: &'a Label) -> Self {
        borrowed::Value::String(std::borrow::Cow::Borrowed(&val.0))
    }
}

#[allow(missing_docs)]
pub mod value_parse {
    #[derive(Debug, thiserror::Error)]
    pub enum Error {
        #[error("Expected string in json data, but found something else: {err}")]
        StrExpected { err: simd_json::TryTypeError },
    }
}

impl Display for Label {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl Label {
    /// RGBA from a Label computed in a deterministic way
    /// This is taken completely from `git-bug`
    #[must_use]
    pub fn associate_color(&self) -> Color {
        // colors from: https://material-ui.com/style/color/
        let colors = [
            Color::from_rgba(244, 67, 54, 255),   // red
            Color::from_rgba(233, 30, 99, 255),   // pink
            Color::from_rgba(156, 39, 176, 255),  // purple
            Color::from_rgba(103, 58, 183, 255),  // deepPurple
            Color::from_rgba(63, 81, 181, 255),   // indigo
            Color::from_rgba(33, 150, 243, 255),  // blue
            Color::from_rgba(3, 169, 244, 255),   // lightBlue
            Color::from_rgba(0, 188, 212, 255),   // cyan
            Color::from_rgba(0, 150, 136, 255),   // teal
            Color::from_rgba(76, 175, 80, 255),   // green
            Color::from_rgba(139, 195, 74, 255),  // lightGreen
            Color::from_rgba(205, 220, 57, 255),  // lime
            Color::from_rgba(255, 235, 59, 255),  // yellow
            Color::from_rgba(255, 193, 7, 255),   // amber
            Color::from_rgba(255, 152, 0, 255),   // orange
            Color::from_rgba(255, 87, 34, 255),   // deepOrange
            Color::from_rgba(121, 85, 72, 255),   // brown
            Color::from_rgba(158, 158, 158, 255), // grey
            Color::from_rgba(96, 125, 139, 255),  // blueGrey
        ];

        let hash = Sha256::digest(self.to_string().as_bytes());

        let id: usize = hash
            .into_iter()
            .map(|val| val as usize)
            .fold(0, |acc, val| (acc + val) % colors.len());

        colors[id]
    }
}

#[derive(Default, Clone, Copy, Debug)]
/// The possible Color of an Lable.
///
/// This encodes the rgba values of the color in the common format (0 -> 0%;
/// 255/[`u8::MAX`] -> 100%).
pub struct Color {
    /// The amount of red.
    pub red: u8,

    /// The amount of green.
    pub green: u8,

    /// The amount of blue.
    pub blue: u8,

    /// How transparent this color is.
    pub alpha: u8,
}

impl Color {
    /// Construct an Color from it's components.
    #[must_use]
    pub fn from_rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
        Self {
            red,
            green,
            blue,
            alpha,
        }
    }
}