diff --git a/src/cli/commands/patch/apply.rs b/src/cli/commands/patch/apply.rs index f0ca7da..5213d0b 100644 --- a/src/cli/commands/patch/apply.rs +++ b/src/cli/commands/patch/apply.rs @@ -17,12 +17,11 @@ use clap::Args; use nostr::hashes::sha1::Hash as Sha1Hash; -use super::PatchPrStatus; use crate::{ cli::{ CliOptions, traits::{CommandRunner, VecNostrEventExt}, - types::{EntityType, NaddrOrSet, NostrEvent}, + types::{EntityType, NaddrOrSet, NostrEvent, PatchPrStatus}, }, error::{N34Error, N34Result}, }; diff --git a/src/cli/commands/patch/close.rs b/src/cli/commands/patch/close.rs index b7b45a1..55cfb89 100644 --- a/src/cli/commands/patch/close.rs +++ b/src/cli/commands/patch/close.rs @@ -16,12 +16,11 @@ use clap::Args; -use super::PatchPrStatus; use crate::{ cli::{ CliOptions, traits::CommandRunner, - types::{EntityType, NaddrOrSet, NostrEvent}, + types::{EntityType, NaddrOrSet, NostrEvent, PatchPrStatus}, }, error::{N34Error, N34Result}, }; diff --git a/src/cli/commands/patch/draft.rs b/src/cli/commands/patch/draft.rs index 22d465a..b054aaf 100644 --- a/src/cli/commands/patch/draft.rs +++ b/src/cli/commands/patch/draft.rs @@ -16,12 +16,11 @@ use clap::Args; -use super::PatchPrStatus; use crate::{ cli::{ CliOptions, traits::CommandRunner, - types::{EntityType, NaddrOrSet, NostrEvent}, + types::{EntityType, NaddrOrSet, NostrEvent, PatchPrStatus}, }, error::{N34Error, N34Result}, }; diff --git a/src/cli/commands/patch/merge.rs b/src/cli/commands/patch/merge.rs index 15a4602..59fbd2d 100644 --- a/src/cli/commands/patch/merge.rs +++ b/src/cli/commands/patch/merge.rs @@ -17,12 +17,11 @@ use clap::Args; use nostr::hashes::sha1::Hash as Sha1Hash; -use super::PatchPrStatus; use crate::{ cli::{ CliOptions, traits::{CommandRunner, VecNostrEventExt}, - types::{EntityType, NaddrOrSet, NostrEvent}, + types::{EntityType, NaddrOrSet, NostrEvent, PatchPrStatus}, }, error::{N34Error, N34Result}, }; diff --git a/src/cli/commands/patch/mod.rs b/src/cli/commands/patch/mod.rs index 87aaea7..cff1b00 100644 --- a/src/cli/commands/patch/mod.rs +++ b/src/cli/commands/patch/mod.rs @@ -34,14 +34,12 @@ mod send; mod tests; use std::{ - fmt, path::{Path, PathBuf}, str::FromStr, sync::LazyLock, }; use clap::Subcommand; -use nostr::event::Kind; use regex::Regex; use self::apply::ApplyArgs; @@ -108,103 +106,6 @@ pub struct GitPatch { pub body: String, } -#[derive(Debug)] -pub enum PatchPrStatus { - /// The patch/pr is currently open - Open, - /// The patch/pr has been merged/applied - MergedApplied, - /// The patch/pr has been closed - Closed, - /// A patch/pr that has been drafted but not yet applied. - Draft, -} - -impl PatchPrStatus { - /// Returns all status kinds - #[inline] - pub const fn all_kinds() -> [Kind; 4] { - [ - Kind::GitStatusOpen, - Kind::GitStatusApplied, - Kind::GitStatusClosed, - Kind::GitStatusDraft, - ] - } - - /// Maps the patch/pr status to its corresponding Nostr kind. - #[inline] - pub fn kind(&self) -> Kind { - match self { - Self::Open => Kind::GitStatusOpen, - Self::MergedApplied => Kind::GitStatusApplied, - Self::Closed => Kind::GitStatusClosed, - Self::Draft => Kind::GitStatusDraft, - } - } - - /// Returns the string representation of the patch/pr status. - pub const fn as_str(&self) -> &'static str { - match self { - Self::Open => "Open", - Self::MergedApplied => "Merged/Applied", - Self::Closed => "Closed", - Self::Draft => "Draft", - } - } - - /// Check if the status is open. - #[inline] - pub fn is_open(&self) -> bool { - matches!(self, Self::Open) - } - - /// Check if the status is merged/applied. - #[inline] - pub fn is_merged_or_applied(&self) -> bool { - matches!(self, Self::MergedApplied) - } - - /// Check if the status is closed. - #[inline] - pub fn is_closed(&self) -> bool { - matches!(self, Self::Closed) - } - - /// Check if the status is draft - #[inline] - pub fn is_drafted(&self) -> bool { - matches!(self, Self::Draft) - } -} - -impl From<&PatchPrStatus> for Kind { - fn from(status: &PatchPrStatus) -> Self { - status.kind() - } -} - -impl fmt::Display for PatchPrStatus { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.as_str()) - } -} - - -impl TryFrom for PatchPrStatus { - type Error = N34Error; - - fn try_from(kind: Kind) -> Result { - match kind { - Kind::GitStatusOpen => Ok(Self::Open), - Kind::GitStatusApplied => Ok(Self::MergedApplied), - Kind::GitStatusClosed => Ok(Self::Closed), - Kind::GitStatusDraft => Ok(Self::Draft), - _ => Err(N34Error::InvalidPatchStatus(kind)), - } - } -} - impl GitPatch { /// Returns the patch file name from the subject pub fn filename(&self, parent: impl AsRef) -> N34Result { diff --git a/src/cli/commands/patch/reopen.rs b/src/cli/commands/patch/reopen.rs index b0a03c5..40ac1f3 100644 --- a/src/cli/commands/patch/reopen.rs +++ b/src/cli/commands/patch/reopen.rs @@ -16,12 +16,11 @@ use clap::Args; -use super::PatchPrStatus; use crate::{ cli::{ CliOptions, traits::CommandRunner, - types::{EntityType, NaddrOrSet, NostrEvent}, + types::{EntityType, NaddrOrSet, NostrEvent, PatchPrStatus}, }, error::{N34Error, N34Result}, }; diff --git a/src/cli/commands/pr/apply.rs b/src/cli/commands/pr/apply.rs index 6dfe35f..78a5094 100644 --- a/src/cli/commands/pr/apply.rs +++ b/src/cli/commands/pr/apply.rs @@ -20,9 +20,8 @@ use nostr::hashes::sha1::Hash as Sha1Hash; use crate::{ cli::{ CliOptions, - patch::PatchPrStatus, traits::CommandRunner, - types::{EntityType, NaddrOrSet, NostrEvent}, + types::{EntityType, NaddrOrSet, NostrEvent, PatchPrStatus}, }, error::{N34Error, N34Result}, }; diff --git a/src/cli/commands/pr/close.rs b/src/cli/commands/pr/close.rs index 4dcfb4b..c425a15 100644 --- a/src/cli/commands/pr/close.rs +++ b/src/cli/commands/pr/close.rs @@ -19,9 +19,8 @@ use clap::Args; use crate::{ cli::{ CliOptions, - patch::PatchPrStatus, traits::CommandRunner, - types::{EntityType, NaddrOrSet, NostrEvent}, + types::{EntityType, NaddrOrSet, NostrEvent, PatchPrStatus}, }, error::{N34Error, N34Result}, }; diff --git a/src/cli/commands/pr/draft.rs b/src/cli/commands/pr/draft.rs index fc5a653..78950e2 100644 --- a/src/cli/commands/pr/draft.rs +++ b/src/cli/commands/pr/draft.rs @@ -19,9 +19,8 @@ use clap::Args; use crate::{ cli::{ CliOptions, - patch::PatchPrStatus, traits::CommandRunner, - types::{EntityType, NaddrOrSet, NostrEvent}, + types::{EntityType, NaddrOrSet, NostrEvent, PatchPrStatus}, }, error::{N34Error, N34Result}, }; diff --git a/src/cli/commands/pr/merge.rs b/src/cli/commands/pr/merge.rs index 8cab8db..58c271c 100644 --- a/src/cli/commands/pr/merge.rs +++ b/src/cli/commands/pr/merge.rs @@ -20,9 +20,8 @@ use nostr::hashes::sha1::Hash as Sha1Hash; use crate::{ cli::{ CliOptions, - patch::PatchPrStatus, traits::CommandRunner, - types::{EntityType, NaddrOrSet, NostrEvent}, + types::{EntityType, NaddrOrSet, NostrEvent, PatchPrStatus}, }, error::{N34Error, N34Result}, }; diff --git a/src/cli/commands/pr/reopen.rs b/src/cli/commands/pr/reopen.rs index e52d5d2..08b4a9c 100644 --- a/src/cli/commands/pr/reopen.rs +++ b/src/cli/commands/pr/reopen.rs @@ -19,9 +19,8 @@ use clap::Args; use crate::{ cli::{ CliOptions, - patch::PatchPrStatus, traits::CommandRunner, - types::{EntityType, NaddrOrSet, NostrEvent}, + types::{EntityType, NaddrOrSet, NostrEvent, PatchPrStatus}, }, error::{N34Error, N34Result}, }; diff --git a/src/cli/common_commands.rs b/src/cli/common_commands.rs index 9a4f069..3272335 100644 --- a/src/cli/common_commands.rs +++ b/src/cli/common_commands.rs @@ -28,11 +28,10 @@ use nostr::{ use super::{ issue::IssueStatus, - patch::PatchPrStatus, types::{NaddrOrSet, NostrEvent}, }; use crate::{ - cli::{CliOptions, patch::GitPatch}, + cli::{CliOptions, patch::GitPatch, types::PatchPrStatus}, error::{N34Error, N34Result}, nostr_utils::traits::{GitIssuePrMetadata, GitPatchUtils, ReposUtils}, }; diff --git a/src/cli/types.rs b/src/cli/types.rs index 320054b..21730c6 100644 --- a/src/cli/types.rs +++ b/src/cli/types.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use std::str::FromStr; +use std::{fmt, str::FromStr}; use nostr::{ event::{EventId, Kind}, @@ -259,6 +259,103 @@ impl FromStr for NostrEvent { } } +#[derive(Debug)] +pub enum PatchPrStatus { + /// The patch/pr is currently open + Open, + /// The patch/pr has been merged/applied + MergedApplied, + /// The patch/pr has been closed + Closed, + /// A patch/pr that has been drafted but not yet applied. + Draft, +} + +impl PatchPrStatus { + /// Returns all status kinds + #[inline] + pub const fn all_kinds() -> [Kind; 4] { + [ + Kind::GitStatusOpen, + Kind::GitStatusApplied, + Kind::GitStatusClosed, + Kind::GitStatusDraft, + ] + } + + /// Maps the patch/pr status to its corresponding Nostr kind. + #[inline] + pub fn kind(&self) -> Kind { + match self { + Self::Open => Kind::GitStatusOpen, + Self::MergedApplied => Kind::GitStatusApplied, + Self::Closed => Kind::GitStatusClosed, + Self::Draft => Kind::GitStatusDraft, + } + } + + /// Returns the string representation of the patch/pr status. + pub const fn as_str(&self) -> &'static str { + match self { + Self::Open => "Open", + Self::MergedApplied => "Merged/Applied", + Self::Closed => "Closed", + Self::Draft => "Draft", + } + } + + /// Check if the status is open. + #[inline] + pub fn is_open(&self) -> bool { + matches!(self, Self::Open) + } + + /// Check if the status is merged/applied. + #[inline] + pub fn is_merged_or_applied(&self) -> bool { + matches!(self, Self::MergedApplied) + } + + /// Check if the status is closed. + #[inline] + pub fn is_closed(&self) -> bool { + matches!(self, Self::Closed) + } + + /// Check if the status is draft + #[inline] + pub fn is_drafted(&self) -> bool { + matches!(self, Self::Draft) + } +} + +impl From<&PatchPrStatus> for Kind { + fn from(status: &PatchPrStatus) -> Self { + status.kind() + } +} + +impl fmt::Display for PatchPrStatus { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.as_str()) + } +} + + +impl TryFrom for PatchPrStatus { + type Error = N34Error; + + fn try_from(kind: Kind) -> Result { + match kind { + Kind::GitStatusOpen => Ok(Self::Open), + Kind::GitStatusApplied => Ok(Self::MergedApplied), + Kind::GitStatusClosed => Ok(Self::Closed), + Kind::GitStatusDraft => Ok(Self::Draft), + _ => Err(N34Error::InvalidPatchStatus(kind)), + } + } +} + fn parse_nip5_repo(nip5: &str, repo_id: &str) -> Result { let (username, domain) = nip5.split_once("@").unwrap_or(("_", nip5)); diff --git a/src/nostr_utils/mod.rs b/src/nostr_utils/mod.rs index 905b4c3..d15848c 100644 --- a/src/nostr_utils/mod.rs +++ b/src/nostr_utils/mod.rs @@ -39,7 +39,7 @@ use nostr_sdk::{Client, ClientOptions}; use traits::TokenUtils; use crate::{ - cli::{CliOptions, issue::IssueStatus, patch::PatchPrStatus}, + cli::{CliOptions, issue::IssueStatus, types::PatchPrStatus}, error::{N34Error, N34Result}, nostr_utils::traits::KindExt, };