• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

unrenamed / chatd / 9973933170

17 Jul 2024 12:10PM UTC coverage: 32.813% (+16.6%) from 16.215%
9973933170

push

github

unrenamed
test: input validator

820 of 2499 relevant lines covered (32.81%)

0.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/server/session_workflow/command_parse.rs
1
use async_trait::async_trait;
2
use chrono::Utc;
3
use std::io::Write;
4

5
use crate::auth::Auth;
6
use crate::chat::{message, ChatRoom, Command, CommandParseError};
7
use crate::terminal::{CloseHandle, Terminal};
8

9
use super::handler::{into_next, WorkflowHandler};
10
use super::WorkflowContext;
11

12
#[derive(Default)]
13
pub struct CommandParser<H>
14
where
15
    H: Clone + Write + CloseHandle + Send,
16
{
17
    next: Option<Box<dyn WorkflowHandler<H>>>,
18
}
19

20
impl<H> CommandParser<H>
21
where
22
    H: Clone + Write + CloseHandle + Send,
23
{
24
    pub fn new(next: impl WorkflowHandler<H> + 'static) -> Self {
×
25
        Self {
26
            next: into_next(next),
×
27
        }
28
    }
29
}
30

31
#[async_trait]
32
impl<H> WorkflowHandler<H> for CommandParser<H>
33
where
34
    H: Clone + Write + CloseHandle + Send,
35
{
36
    #[allow(unused_variables)]
37
    async fn handle(
38
        &mut self,
39
        context: &mut WorkflowContext,
40
        terminal: &mut Terminal<H>,
41
        room: &mut ChatRoom,
42
        auth: &mut Auth,
43
    ) -> anyhow::Result<()> {
44
        let user = context.user.clone();
×
45

46
        let command_str = if let Some(str) = &context.command_str {
×
47
            str
×
48
        } else {
49
            return Ok(());
×
50
        };
51

52
        let input_str = terminal.input.to_string();
×
53

54
        match command_str.parse::<Command>() {
×
55
            Err(err) if err == CommandParseError::NotRecognizedAsCommand => {
×
56
                terminal.clear_input()?;
×
57
                room.find_member_mut(&user.username)
×
58
                    .update_last_sent_time(Utc::now());
×
59
                let message = message::Public::new(user.clone().into(), input_str);
×
60
                room.send_message(message.into()).await?;
×
61
            }
62
            Err(err) => {
×
63
                terminal.input.push_to_history();
×
64
                terminal.clear_input()?;
×
65
                let message = message::Command::new(user.clone().into(), input_str);
×
66
                room.send_message(message.into()).await?;
×
67
                let message = message::Error::new(user.clone().into(), format!("{}", err));
×
68
                room.send_message(message.into()).await?;
×
69
            }
70
            Ok(command) => {
×
71
                terminal.input.push_to_history();
×
72
                terminal.clear_input()?;
×
73
                let message = message::Command::new(user.clone().into(), input_str);
×
74
                room.send_message(message.into()).await?;
×
75
                context.command = Some(command);
×
76
            }
77
        }
78
        Ok(())
×
79
    }
80

81
    fn next(&mut self) -> &mut Option<Box<dyn WorkflowHandler<H>>> {
×
82
        &mut self.next
×
83
    }
84
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc