diff options
Diffstat (limited to 'pkgs/by-name/ba/back/src/git_bug/issue/operation')
-rw-r--r-- | pkgs/by-name/ba/back/src/git_bug/issue/operation/mod.rs | 124 | ||||
-rw-r--r-- | pkgs/by-name/ba/back/src/git_bug/issue/operation/operation_type.rs | 51 |
2 files changed, 175 insertions, 0 deletions
diff --git a/pkgs/by-name/ba/back/src/git_bug/issue/operation/mod.rs b/pkgs/by-name/ba/back/src/git_bug/issue/operation/mod.rs new file mode 100644 index 0000000..7f861a7 --- /dev/null +++ b/pkgs/by-name/ba/back/src/git_bug/issue/operation/mod.rs @@ -0,0 +1,124 @@ +// Back - An extremely simple git issue tracking system. Inspired by tvix's +// panettone +// +// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de> +// 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 <https://www.gnu.org/licenses/agpl.txt>. + +use std::convert::Infallible; + +use operation_type::OperationType; +use serde::Deserialize; +use serde_json::Value; + +use crate::{ + get, + git_bug::format::{MarkDown, TimeStamp}, +}; + +use super::{entity, label::Label, Status}; + +pub mod operation_type; + +#[derive(Deserialize, Debug)] +#[serde(try_from = "Value")] +pub enum Operation { + AddComment { + timestamp: TimeStamp, + message: MarkDown, + }, + Create { + timestamp: TimeStamp, + title: MarkDown, + message: MarkDown, + }, + EditComment { + timestamp: TimeStamp, + target: entity::Id, + message: MarkDown, + }, + LabelChange { + timestamp: TimeStamp, + added: Vec<Label>, + removed: Vec<Label>, + }, + SetStatus { + timestamp: TimeStamp, + status: Status, + }, + SetTitle { + timestamp: TimeStamp, + title: MarkDown, + was: MarkDown, + }, + + // These seem to be just weird non-operation, operations. + // defined in: git-bug/entities/bug/operation.go + NoOp {}, + SetMetadata {}, +} + +impl TryFrom<Value> for Operation { + type Error = Infallible; + + fn try_from(value: Value) -> Result<Self, Self::Error> { + let operation_type = OperationType::from_json_int( + value + .get("type") + .expect("Should exist") + .as_u64() + .expect("This should work"), + ); + + let op = match operation_type { + OperationType::AddComment => Self::AddComment { + timestamp: get! {value, "timestamp" }, + message: get! {value, "message"}, + }, + OperationType::Create => Self::Create { + timestamp: get! {value, "timestamp"}, + title: get! {value, "title"}, + message: get! {value, "message"}, + }, + OperationType::EditComment => Self::EditComment { + timestamp: get! {value, "timestamp"}, + target: get! {value, "target"}, + message: get! {value, "message"}, + }, + OperationType::LabelChange => Self::LabelChange { + timestamp: get! {value, "timestamp"}, + added: serde_json::from_value( + value + .get("added") + .expect("This should be available") + .to_owned(), + ) + .expect("This should be parsable"), + removed: serde_json::from_value( + value + .get("removed") + .expect("This should be available") + .to_owned(), + ) + .expect("This should be parsable"), + }, + OperationType::SetStatus => Self::SetStatus { + timestamp: get! {value, "timestamp"}, + status: get! {value, "status"}, + }, + OperationType::SetTitle => Self::SetTitle { + timestamp: get! {value, "timestamp"}, + title: get! {value, "title"}, + was: get! {value, "was"}, + }, + OperationType::NoOp => Self::NoOp {}, + OperationType::SetMetadata => Self::SetMetadata {}, + }; + + Ok(op) + } +} diff --git a/pkgs/by-name/ba/back/src/git_bug/issue/operation/operation_type.rs b/pkgs/by-name/ba/back/src/git_bug/issue/operation/operation_type.rs new file mode 100644 index 0000000..69d272f --- /dev/null +++ b/pkgs/by-name/ba/back/src/git_bug/issue/operation/operation_type.rs @@ -0,0 +1,51 @@ +// Back - An extremely simple git issue tracking system. Inspired by tvix's +// panettone +// +// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de> +// 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 <https://www.gnu.org/licenses/agpl.txt>. + +pub enum OperationType { + AddComment, + Create, + EditComment, + LabelChange, + NoOp, + SetMetadata, + SetStatus, + SetTitle, +} + +impl OperationType { + // NOTE(@bpeetz): This mapping should always be the same as `git_bug`'s. + // The mapping is defined in `git-bug/entities/bug/operation.go`. <2024-12-26> + pub fn to_json_int(self) -> u64 { + match self { + OperationType::Create => 1, + OperationType::SetTitle => 2, + OperationType::AddComment => 3, + OperationType::SetStatus => 4, + OperationType::LabelChange => 5, + OperationType::EditComment => 6, + OperationType::NoOp => 7, + OperationType::SetMetadata => 8, + } + } + pub fn from_json_int(value: u64) -> Self { + match value { + 1 => OperationType::Create, + 2 => OperationType::SetTitle, + 3 => OperationType::AddComment, + 4 => OperationType::SetStatus, + 5 => OperationType::LabelChange, + 6 => OperationType::EditComment, + 7 => OperationType::NoOp, + 8 => OperationType::SetMetadata, + other => unimplemented!("The operation type {other} is not recognized."), + } + } +} |