From dfb5714045e99a09bf3f67890ae3cdeab47058b3 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Thu, 26 Dec 2024 17:50:54 +0100 Subject: feat(pkgs/back): Rewrite the `git-bug` interface code The previous code was more or less reverse engineered, whilst this code is based on the actually git-bug source code. This improves the whole issue and operation handling immensely and also makes the code better maintainable. Furthermore, it also adds support for the operations that had not already used in `vhack.eu/nixos-server.git`. --- .../by-name/ba/back/src/git_bug/issue/label/mod.rs | 85 ++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 pkgs/by-name/ba/back/src/git_bug/issue/label/mod.rs (limited to 'pkgs/by-name/ba/back/src/git_bug/issue/label') diff --git a/pkgs/by-name/ba/back/src/git_bug/issue/label/mod.rs b/pkgs/by-name/ba/back/src/git_bug/issue/label/mod.rs new file mode 100644 index 0000000..a971234 --- /dev/null +++ b/pkgs/by-name/ba/back/src/git_bug/issue/label/mod.rs @@ -0,0 +1,85 @@ +// Back - An extremely simple git issue tracking system. Inspired by tvix's +// panettone +// +// Copyright (C) 2024 Benedikt Peetz +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This file is part of Back. +// +// You should have received a copy of the License along with this program. +// If not, see . + +use std::fmt::Display; + +use serde::Deserialize; +use sha2::{Digest, Sha256}; + +use crate::git_bug::format::HtmlString; + +#[derive(Debug, Deserialize, PartialEq, Eq, Clone)] +pub struct Label { + value: HtmlString, +} + +impl Display for Label { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.value.fmt(f) + } +} + +impl Label { + /// RGBA from a Label computed in a deterministic way + /// This is taken completely from `git_bug` + pub fn associate_color(&self) -> Color { + // colors from: https://material-ui.com/style/color/ + let colors = vec![ + 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)] +pub struct Color { + pub red: u32, + pub green: u32, + pub blue: u32, + pub alpha: u32, +} + +impl Color { + pub fn from_rgba(red: u32, green: u32, blue: u32, alpha: u32) -> Self { + Self { + red, + green, + blue, + alpha, + } + } +} -- cgit 1.4.1