Skip to content

Commit 6c52ef1

Browse files
chore: Add Port type
1 parent 8cc03df commit 6c52ef1

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

rust/operator-binary/src/controller/build/role_group_builder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ use crate::{
6262
},
6363
role_group_utils::ResourceNames,
6464
types::{
65+
common::Port,
6566
kubernetes::{
6667
PersistentVolumeClaimName, SecretClassName, ServiceAccountName, ServiceName,
6768
VolumeName,
@@ -72,9 +73,9 @@ use crate::{
7273
};
7374

7475
pub const HTTP_PORT_NAME: &str = "http";
75-
pub const HTTP_PORT: u16 = 9200;
76+
pub const HTTP_PORT: Port = Port(9200);
7677
pub const TRANSPORT_PORT_NAME: &str = "transport";
77-
pub const TRANSPORT_PORT: u16 = 9300;
78+
pub const TRANSPORT_PORT: Port = Port(9300);
7879

7980
constant!(CONFIG_VOLUME_NAME: VolumeName = "config");
8081

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pub mod common;
12
pub mod kubernetes;
23
pub mod operator;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//! Common types that do not belong (yet) to a more specific module
2+
use snafu::{ResultExt, Snafu};
3+
use strum::{EnumDiscriminants, IntoStaticStr};
4+
5+
#[derive(Snafu, Debug, EnumDiscriminants)]
6+
#[strum_discriminants(derive(IntoStaticStr))]
7+
pub enum Error {
8+
#[snafu(display("failed to convert to port number"))]
9+
ConvertToPortNumber { source: std::num::TryFromIntError },
10+
}
11+
12+
/// A port number
13+
#[derive(Clone, Debug, Eq, PartialEq)]
14+
pub struct Port(pub u16);
15+
16+
impl std::fmt::Display for Port {
17+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18+
self.0.fmt(f)
19+
}
20+
}
21+
22+
impl From<u16> for Port {
23+
fn from(value: u16) -> Self {
24+
Port(value)
25+
}
26+
}
27+
28+
impl From<Port> for i32 {
29+
fn from(value: Port) -> Self {
30+
value.0 as i32
31+
}
32+
}
33+
34+
impl TryFrom<i32> for Port {
35+
type Error = Error;
36+
37+
fn try_from(value: i32) -> Result<Self, Self::Error> {
38+
Ok(Port(
39+
u16::try_from(value).context(ConvertToPortNumberSnafu)?,
40+
))
41+
}
42+
}
43+
44+
#[cfg(test)]
45+
mod tests {
46+
47+
use super::{ErrorDiscriminants, Port};
48+
49+
#[test]
50+
fn test_port_fmt() {
51+
assert_eq!("0".to_owned(), Port(0).to_string());
52+
assert_eq!("65535".to_owned(), Port(65535).to_string());
53+
}
54+
55+
#[test]
56+
fn test_port_try_from_i32() {
57+
assert_eq!(Some(Port(0)), Port::try_from(0).ok());
58+
assert_eq!(Some(Port(65535)), Port::try_from(65535).ok());
59+
assert_eq!(
60+
Err(ErrorDiscriminants::ConvertToPortNumber),
61+
Port::try_from(-1).map_err(ErrorDiscriminants::from)
62+
);
63+
assert_eq!(
64+
Err(ErrorDiscriminants::ConvertToPortNumber),
65+
Port::try_from(65536).map_err(ErrorDiscriminants::from)
66+
);
67+
}
68+
}

0 commit comments

Comments
 (0)