Skip to content

Commit e4ba54f

Browse files
committed
refactor: Use an enum for input source names
1 parent 42c7e54 commit e4ba54f

File tree

6 files changed

+37
-14
lines changed

6 files changed

+37
-14
lines changed

src/api/flat.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55
use thiserror::Error;
66

77
use crate::{
8-
global::{Global, InputMessage, InputMessageData, InputSourceHandle},
8+
global::{Global, InputMessage, InputMessageData, InputSourceHandle, InputSourceName},
99
image::{RawImage, RawImageError},
1010
models::Color,
1111
utils::i32_to_duration,
@@ -103,7 +103,10 @@ pub async fn handle_request(
103103
*source = Some(
104104
global
105105
.register_input_source(
106-
format!("FlatBuffers({}): {}", peer_addr, register.origin()),
106+
InputSourceName::FlatBuffers {
107+
peer_addr,
108+
origin: register.origin().to_owned(),
109+
},
107110
Some(priority),
108111
)
109112
.await

src/global.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::collections::HashMap;
2+
use std::net::SocketAddr;
23
use std::sync::Arc;
34

5+
use parse_display::Display;
46
use tokio::sync::broadcast;
57
use tokio::sync::RwLock;
68

@@ -28,10 +30,25 @@ pub trait Message: Sized {
2830
#[derive(Clone)]
2931
pub struct Global(Arc<RwLock<GlobalData>>);
3032

33+
#[derive(Display, Debug)]
34+
pub enum InputSourceName {
35+
#[display("FlatBuffers({peer_addr}): {origin}")]
36+
FlatBuffers {
37+
peer_addr: SocketAddr,
38+
origin: String,
39+
},
40+
#[display("JSON({peer_addr})")]
41+
Json { peer_addr: SocketAddr },
42+
#[display("Protobuf({peer_addr})")]
43+
Protobuf { peer_addr: SocketAddr },
44+
#[display("PriorityMuxer")]
45+
PriorityMuxer,
46+
}
47+
3148
impl Global {
3249
pub async fn register_input_source(
3350
&self,
34-
name: String,
51+
name: InputSourceName,
3552
priority: Option<i32>,
3653
) -> Result<InputSourceHandle<InputMessage>, InputSourceError> {
3754
let priority = if let Some(priority) = priority {
@@ -53,7 +70,7 @@ impl Global {
5370

5471
pub async fn register_muxed_source(
5572
&self,
56-
name: String,
73+
name: InputSourceName,
5774
) -> Result<InputSourceHandle<MuxedMessage>, InputSourceError> {
5875
Ok(InputSourceHandle {
5976
input_source: self.0.write().await.register_muxed_source(name),
@@ -107,7 +124,7 @@ impl GlobalData {
107124

108125
fn register_input_source(
109126
&mut self,
110-
name: String,
127+
name: InputSourceName,
111128
priority: Option<i32>,
112129
) -> Arc<InputSource<InputMessage>> {
113130
let id = self.next_input_source_id;
@@ -133,7 +150,7 @@ impl GlobalData {
133150
}
134151
}
135152

136-
fn register_muxed_source(&mut self, name: String) -> Arc<InputSource<MuxedMessage>> {
153+
fn register_muxed_source(&mut self, name: InputSourceName) -> Arc<InputSource<MuxedMessage>> {
137154
let id = self.next_muxed_source_id;
138155
self.next_muxed_source_id += 1;
139156

src/global/input_source.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use parse_display::Display;
44
use thiserror::Error;
55
use tokio::sync::broadcast;
66

7-
use crate::global::{Global, Message};
7+
use super::{Global, InputSourceName, Message};
88

99
#[derive(Display)]
1010
#[display("`{name}` (id = {id}, priority = {priority:?})")]
1111
pub struct InputSource<T: Message> {
1212
pub(super) id: usize,
13-
pub(super) name: String,
13+
pub(super) name: InputSourceName,
1414
pub(super) priority: Option<i32>,
1515
pub(super) tx: broadcast::Sender<T>,
1616
}

src/muxer.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use tokio::select;
66
use tokio::sync::broadcast::Receiver;
77

88
use crate::{
9-
global::{Global, InputMessage, InputMessageData, InputSourceHandle, Message, MuxedMessage},
9+
global::{
10+
Global, InputMessage, InputMessageData, InputSourceHandle, InputSourceName, Message,
11+
MuxedMessage,
12+
},
1013
models::Color,
1114
};
1215

@@ -28,7 +31,7 @@ impl PriorityMuxer {
2831
let mut this = Self {
2932
receiver: global.subscribe_input().await,
3033
source_handle: global
31-
.register_muxed_source("PriorityMuxer".to_owned())
34+
.register_muxed_source(InputSourceName::PriorityMuxer)
3235
.await
3336
.unwrap(),
3437
inputs: Default::default(),

src/servers/json.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tokio_util::codec::Framed;
99

1010
use crate::{
1111
api::json::{self, JsonApiError},
12-
global::Global,
12+
global::{Global, InputSourceName},
1313
};
1414

1515
/// JSON protocol codec definition
@@ -37,7 +37,7 @@ pub async fn handle_client(
3737

3838
// unwrap: cannot fail because the priority is None
3939
let source = global
40-
.register_input_source(format!("JSON({})", peer_addr), None)
40+
.register_input_source(InputSourceName::Json { peer_addr }, None)
4141
.await
4242
.unwrap();
4343

src/servers/proto.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tokio::net::TcpStream;
99

1010
use crate::{
1111
api::proto::{self, message, ProtoApiError},
12-
global::{Global, InputMessage, InputSourceHandle},
12+
global::{Global, InputMessage, InputSourceHandle, InputSourceName},
1313
};
1414

1515
#[derive(Debug, Error)]
@@ -86,7 +86,7 @@ pub async fn handle_client(
8686

8787
// unwrap: cannot fail because the priority is None
8888
let source = global
89-
.register_input_source(format!("Protobuf({})", peer_addr), None)
89+
.register_input_source(InputSourceName::Protobuf { peer_addr }, None)
9090
.await
9191
.unwrap();
9292

0 commit comments

Comments
 (0)