upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/kind.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/kind.rs')
-rw-r--r--src/kind.rs137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/kind.rs b/src/kind.rs
new file mode 100644
index 0000000..90771b5
--- /dev/null
+++ b/src/kind.rs
@@ -0,0 +1,137 @@
1// Copyright (c) 2022-2023 Yuki Kishimoto
2// Distributed under the MIT software license
3
4//! Kind
5
6use std::fmt;
7use std::num::ParseIntError;
8use std::str::FromStr;
9
10use serde::de::{Deserialize, Deserializer, Error, Visitor};
11use serde::ser::{Serialize, Serializer};
12
13/// Event [`Kind`]
14#[derive(Debug, Copy, Clone, Eq, Ord, PartialOrd)]
15pub enum Kind {
16 /// Initialize Group
17 InitializeGroup,
18 /// Update Group
19 UpdateGroup,
20 /// Initialize Repository
21 InitializeRepo,
22 /// Update Repository
23 UpdateRepo,
24 /// Initialize Branch
25 InitializeBranch,
26 /// Update Branch
27 UpdateBranch,
28 /// Patch
29 Patch,
30 /// Pull Request
31 PullRequest,
32 /// Merge
33 Merge,
34 /// Custom
35 Custom(u64),
36}
37
38impl Kind {
39 /// Get [`Kind`] as `u32`
40 pub fn as_u32(&self) -> u32 {
41 self.as_u64() as u32
42 }
43
44 /// Get [`Kind`] as `u64`
45 pub fn as_u64(&self) -> u64 {
46 (*self).into()
47 }
48
49 /// Convert to nostr::event::Kind::Custom()
50 pub fn into_sdk_custom_kind(&self) -> nostr::event::Kind {
51 nostr::event::Kind::Custom((*self).into())
52 }
53}
54
55impl From<u64> for Kind {
56 fn from(u: u64) -> Self {
57 match u {
58 40000 => Self::InitializeGroup,
59 40001 => Self::UpdateGroup,
60 40010 => Self::InitializeRepo,
61 40011 => Self::UpdateRepo,
62 40020 => Self::InitializeBranch,
63 40021 => Self::UpdateBranch,
64 410 => Self::Patch,
65 1 => Self::PullRequest,
66 421 => Self::Merge,
67 x => Self::Custom(x),
68
69 }
70 }
71}
72
73impl From<Kind> for u64 {
74 fn from(e: Kind) -> u64 {
75 match e {
76 Kind::InitializeGroup => 40000,
77 Kind::UpdateGroup => 40001,
78 Kind::InitializeRepo => 40010,
79 Kind::UpdateRepo => 40011,
80 Kind::InitializeBranch => 40020,
81 Kind::UpdateBranch => 40021,
82 Kind::Patch => 410,
83 Kind::PullRequest => 1,
84 Kind::Merge => 421,
85 Kind::Custom(u) => u,
86 }
87 }
88}
89
90impl FromStr for Kind {
91 type Err = ParseIntError;
92 fn from_str(kind: &str) -> Result<Self, Self::Err> {
93 let kind: u64 = kind.parse()?;
94 Ok(Self::from(kind))
95 }
96}
97
98impl PartialEq<Kind> for Kind {
99 fn eq(&self, other: &Kind) -> bool {
100 self.as_u64() == other.as_u64()
101 }
102}
103
104impl Serialize for Kind {
105 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
106 where
107 S: Serializer,
108 {
109 serializer.serialize_u64(From::from(*self))
110 }
111}
112
113impl<'de> Deserialize<'de> for Kind {
114 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
115 where
116 D: Deserializer<'de>,
117 {
118 deserializer.deserialize_u64(KindVisitor)
119 }
120}
121
122struct KindVisitor;
123
124impl Visitor<'_> for KindVisitor {
125 type Value = Kind;
126
127 fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
128 write!(f, "an unsigned number")
129 }
130
131 fn visit_u64<E>(self, v: u64) -> Result<Kind, E>
132 where
133 E: Error,
134 {
135 Ok(From::<u64>::from(v))
136 }
137}