zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use crate::core::{auth0::UserDetails, websocat::echo::input::SubscribeData};

use super::{
    connection_new::Connection,
    input::{Action, AuthorizationData, HistoryMessage, Input, PostLocationData, TalkMessage},
    output::{Level, OutputMessage, Ty},
    ConnId,
};

impl Connection {
    pub fn process_message(&self, conn: ConnId, user_details: Option<&UserDetails>, msg: &str) {
        if msg.is_empty() {
            return;
        }

        match serde_json::from_str::<Input>(msg) {
            Ok(input) => self.process_input(conn, user_details, input),
            Err(e) => {
                if msg.starts_with('{') && msg.ends_with('}') {
                    let out = OutputMessage {
                        id: None,
                        level: Level::Err,
                        ty: Ty::Failed,
                        msg: Some(e.to_string()),
                        data: None,
                    };

                    if let Some(out) = &out.message() {
                        self.fire_message(conn, out)
                    }
                } else {
                    self.fire_message(conn, msg);
                }
            }
        }
    }

    pub fn process_input(&self, conn: ConnId, user_details: Option<&UserDetails>, input: Input) {
        let data = input.data;

        match input.action {
            Action::Autorization => match AuthorizationData::parse(&data) {
                Ok(_data) => {
                    self.fire_autorization_message(input.id, conn, _data.token);
                }
                Err(e) => {
                    self.err(conn, input.id.to_owned(), e.to_string());
                }
            },
            Action::Subscribe | Action::Unsubscribe => match SubscribeData::parse(&data) {
                Ok(data) => match input.action {
                    Action::Subscribe => self.fire_subscribe(conn, input.id, data.channel),
                    Action::Unsubscribe => self.fire_unsubscribe(conn, input.id, data.channel),
                    _ => {}
                },
                Err(e) => {
                    self.err(conn, input.id.to_owned(), e.to_string());
                }
            },
            Action::Talk => {
                if let Some(user) = user_details {
                    match TalkMessage::parse(&data) {
                        Ok(data) => {
                            self.fire_talk_message(conn, user, input.id, data);
                        }
                        Err(e) => {
                            self.err(conn, input.id.to_owned(), e.to_string());
                        }
                    }
                } else {
                    self.err(
                        conn,
                        input.id.to_owned(),
                        "Please private auth token!".to_string(),
                    );
                }
            }
            Action::History => {
                if user_details.is_some() {
                    match HistoryMessage::parse(&data) {
                        Ok(data) => {
                            self.fire_history_message(input.id, conn, data.to);
                        }
                        Err(e) => {
                            self.err(conn, input.id.to_owned(), e.to_string());
                        }
                    }
                } else {
                    self.err(
                        conn,
                        input.id.to_owned(),
                        "Please private auth token!".to_string(),
                    );
                }
            }
            Action::HistoryTalk => {
                if user_details.is_some() {
                    self.fire_history_talk(input.id, conn);
                } else {
                    self.err(
                        conn,
                        input.id.to_owned(),
                        "Please private auth token!".to_string(),
                    );
                }
            }
            Action::PostLocation => match PostLocationData::parse(&data) {
                Ok(data) => {
                    self.fire_location_message(conn, input.id, data);
                }
                Err(e) => {
                    self.err(conn, input.id.to_owned(), e.to_string());
                }
            },
        }
    }

    fn err(&self, conn: ConnId, id: Option<String>, err: String) {
        let out = OutputMessage {
            id,
            level: Level::Err,
            ty: Ty::Failed,
            msg: Some(err),
            data: None,
        };

        if let Some(out) = &out.message() {
            self.fire_message(conn, out);
        }
    }
}