mirror of
https://github.com/pcvolkmer/arsnova-client.git
synced 2025-04-19 19:16:51 +00:00
refactor: use enum instead of raw u8 value
This commit is contained in:
parent
5a12f79f93
commit
d1fbe944a4
@ -34,7 +34,7 @@ use ratatui::Terminal;
|
|||||||
use tokio::select;
|
use tokio::select;
|
||||||
use tokio::sync::mpsc::{channel, Receiver};
|
use tokio::sync::mpsc::{channel, Receiver};
|
||||||
|
|
||||||
use arsnova_client::{Client, Feedback, FeedbackHandler};
|
use arsnova_client::{Client, Feedback, FeedbackHandler, FeedbackValue};
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(author, version, about = "Terminal-based ARSnova live feedback client", long_about = None)]
|
#[command(author, version, about = "Terminal-based ARSnova live feedback client", long_about = None)]
|
||||||
@ -64,7 +64,7 @@ async fn main() -> Result<(), ()> {
|
|||||||
|
|
||||||
let (tx, rx) = channel::<Feedback>(10);
|
let (tx, rx) = channel::<Feedback>(10);
|
||||||
|
|
||||||
let (fb_tx, fb_rx) = channel::<u8>(10);
|
let (fb_tx, fb_rx) = channel::<FeedbackValue>(10);
|
||||||
|
|
||||||
let _ = tx
|
let _ = tx
|
||||||
.clone()
|
.clone()
|
||||||
@ -94,16 +94,16 @@ async fn main() -> Result<(), ()> {
|
|||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Esc => break,
|
KeyCode::Esc => break,
|
||||||
KeyCode::Char('a') | KeyCode::Char('1') => {
|
KeyCode::Char('a') | KeyCode::Char('1') => {
|
||||||
let _ = fb_tx.send(0).await;
|
let _ = fb_tx.send(FeedbackValue::VeryGood).await;
|
||||||
}
|
}
|
||||||
KeyCode::Char('b') | KeyCode::Char('2') => {
|
KeyCode::Char('b') | KeyCode::Char('2') => {
|
||||||
let _ = fb_tx.send(1).await;
|
let _ = fb_tx.send(FeedbackValue::Good).await;
|
||||||
}
|
}
|
||||||
KeyCode::Char('c') | KeyCode::Char('3') => {
|
KeyCode::Char('c') | KeyCode::Char('3') => {
|
||||||
let _ = fb_tx.send(2).await;
|
let _ = fb_tx.send(FeedbackValue::Bad).await;
|
||||||
}
|
}
|
||||||
KeyCode::Char('d') | KeyCode::Char('4') => {
|
KeyCode::Char('d') | KeyCode::Char('4') => {
|
||||||
let _ = fb_tx.send(3).await;
|
let _ = fb_tx.send(FeedbackValue::VeryBad).await;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
|
@ -174,6 +174,30 @@ pub enum FeedbackHandler {
|
|||||||
Sender(Sender<Feedback>),
|
Sender(Sender<Feedback>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A possible feedback value
|
||||||
|
pub enum FeedbackValue {
|
||||||
|
VeryGood,
|
||||||
|
A,
|
||||||
|
Good,
|
||||||
|
B,
|
||||||
|
Bad,
|
||||||
|
C,
|
||||||
|
VeryBad,
|
||||||
|
D,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FeedbackValue {
|
||||||
|
/// Returns internal u8 representation
|
||||||
|
fn into_u8(self) -> u8 {
|
||||||
|
match self {
|
||||||
|
FeedbackValue::VeryGood | FeedbackValue::A => 0,
|
||||||
|
FeedbackValue::Good | FeedbackValue::B => 1,
|
||||||
|
FeedbackValue::Bad | FeedbackValue::C => 2,
|
||||||
|
FeedbackValue::VeryBad | FeedbackValue::D => 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub enum ClientError {
|
pub enum ClientError {
|
||||||
ConnectionError,
|
ConnectionError,
|
||||||
@ -368,7 +392,7 @@ impl Client<LoggedIn> {
|
|||||||
pub async fn register_feedback_receiver(
|
pub async fn register_feedback_receiver(
|
||||||
&self,
|
&self,
|
||||||
short_id: &str,
|
short_id: &str,
|
||||||
mut receiver: Receiver<u8>,
|
mut receiver: Receiver<FeedbackValue>,
|
||||||
) -> Result<(), ClientError> {
|
) -> Result<(), ClientError> {
|
||||||
let room_info = self.get_room_info(short_id).await?;
|
let room_info = self.get_room_info(short_id).await?;
|
||||||
|
|
||||||
@ -401,7 +425,7 @@ impl Client<LoggedIn> {
|
|||||||
"payload": {
|
"payload": {
|
||||||
"roomId": room_info.id,
|
"roomId": room_info.id,
|
||||||
"userId": user_id,
|
"userId": user_id,
|
||||||
"value": value
|
"value": value.into_u8()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.to_string();
|
.to_string();
|
||||||
|
@ -19,4 +19,4 @@
|
|||||||
|
|
||||||
pub mod client;
|
pub mod client;
|
||||||
|
|
||||||
pub use client::{Client, Feedback, FeedbackHandler, RoomInfo};
|
pub use client::{Client, Feedback, FeedbackHandler, FeedbackValue, RoomInfo};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user