fn parse_command(cmd: &'a str, body: &'a str, tags: Option<Tags<'a>>) -> Result<Message<'a>> {
        Ok(match cmd {
            "PING" => Message::Ping,
            "PONG" => Message::Pong,
            "CAP" => {
                let off = body
                    .find(" :")
                    .ok_or_else(|| Error::new(ErrorKind::Other, "Malformed CAP command."))?;

                match &body[..off] {
                    "REQ" => Message::CapReq {
                        req: &body[(off + 2)..],
                    },
                    "* ACK" => Message::CapAck {
                        req: &body[(off + 2)..],
                    },
                    _ => return Err(Error::new(ErrorKind::Other, "Malformed CAP command.")),
                }
            }
            "PASS" => Message::Pass { pass: body },
            "NICK" => Message::Nick { nick: body },
            "JOIN" => Message::Join { chan: &body[1..] },
            "PART" => Message::Part { chan: &body[1..] },
            "PRIVMSG" => {
                let off = body
                    .find(" :")
                    .ok_or_else(|| Error::new(ErrorKind::Other, "Malformed PRIVMSG command."))?;

                Message::Privmsg {
                    tags,
                    chan: &body[1..off],
                    msg: &body[(off + 2)..],
                }
            }
            "CLEARCHAT" => {
                if let Some(off) = body.find(" :") {
                    Message::Clearchat {
                        tags,
                        chan: &body[1..off],
                        usr: Some(&body[(off + 2)..]),
                    }
                } else {
                    Message::Clearchat {
                        tags,
                        chan: &body[1..],
                        usr: None,
                    }
                }
            }
            "CLEARMSG" => {
                let off = body
                    .find(" :")
                    .ok_or_else(|| Error::new(ErrorKind::Other, "Malformed CLEARMSG command."))?;

                Message::Clearmsg {
                    tags,
                    chan: &body[1..off],
                    msg: &body[(off + 2)..],
                }
            }
            "HOSTTARGET" => {
                if let Some(off) = body.find(" :-") {
                    if body.len() > off + 5 {
                        if let Ok(view) = body[(off + 4)..].parse::<u32>() {
                            Message::HosttargetEnd {
                                host: &body[1..off],
                                view: Some(view),
                            }
                        } else {
                            return Err(Error::new(
                                ErrorKind::Other,
                                "Malformed HOSTTARGET command.",
                            ));
                        }
                    } else {
                        Message::HosttargetEnd {
                            host: &body[1..off],
                            view: None,
                        }
                    }
                } else {
                    let off = body.find(" :").ok_or_else(|| {
                        Error::new(ErrorKind::Other, "Malformed HOSTTARGET command.")
                    })?;

                    if body.len() < off + 3 {
                        return Err(Error::new(
                            ErrorKind::Other,
                            "Malformed HOSTTARGET command.",
                        ));
                    }

                    let host = &body[1..off];
                    let body = &body[(off + 2)..];

                    if let Some(off) = body.find(' ') {
                        if body.len() > off + 2 {
                            if let Ok(view) = body[(off + 1)..].parse::<u32>() {
                                return Ok(Message::HosttargetStart {
                                    host,
                                    chan: &body[..off],
                                    view: Some(view),
                                });
                            } else {
                                return Err(Error::new(
                                    ErrorKind::Other,
                                    "Malformed HOSTTARGET command.",
                                ));
                            }
                        } else {
                            Message::HosttargetStart {
                                host,
                                chan: body,
                                view: None,
                            }
                        }
                    } else {
                        Message::HosttargetStart {
                            host,
                            chan: body,
                            view: None,
                        }
                    }
                }
            }
            "NOTICE" => {
                let off = body
                    .find(" :")
                    .ok_or_else(|| Error::new(ErrorKind::Other, "Malformed NOTICE command."))?;

                Message::Notice {
                    tags,
                    chan: &body[1..off],
                    msg: &body[(off + 2)..],
                }
            }
            "RECONNECT" => Message::Reconnect,
            "ROOMSTATE" => Message::Roomstate {
                tags,
                chan: &body[1..],
            },
            "USERNOTICE" => {
                let off = body
                    .find(" :")
                    .ok_or_else(|| Error::new(ErrorKind::Other, "Malformed USERNOTICE command."))?;

                Message::Usernotice {
                    tags,
                    chan: &body[1..off],
                    msg: &body[(off + 2)..],
                }
            }
            "USERSTATE" => Message::Userstate {
                tags,
                chan: &body[1..],
            },
            "GLOBALUSERSTATE" => Message::GlobalUserstate { tags },
            _ => {
                return Err(Error::new(
                    ErrorKind::Other,
                    "Parsing message command failed.",
                ))
            }
        })
    }
/// Helper function for parsing message body base on the command.
Helper function for parsing message body base on the command.
Helper function for parsing message body base on the command .
fn parse_command(cmd: &'a str, body: &'a str, tags: Option<Tags<'a>>) -> Result<Message<'a>> {
        Ok(match cmd {
            "PING" => Message::Ping,
            "PONG" => Message::Pong,
            "CAP" => {
                let off = body
                    .find(" :")
                    .ok_or_else(|| Error::new(ErrorKind::Other, "Malformed CAP command."))?;
                match &body[..off] {
                    "REQ" => Message::CapReq {
                        req: &body[(off + 2)..],
                    },
                    "* ACK" => Message::CapAck {
                        req: &body[(off + 2)..],
                    },
                    _ => return Err(Error::new(ErrorKind::Other, "Malformed CAP command.")),
                }
            }
            "PASS" => Message::Pass { pass: body },
            "NICK" => Message::Nick { nick: body },
            "JOIN" => Message::Join { chan: &body[1..] },
            "PART" => Message::Part { chan: &body[1..] },
            "PRIVMSG" => {
                let off = body
                    .find(" :")
                    .ok_or_else(|| Error::new(ErrorKind::Other, "Malformed PRIVMSG command."))?;
                Message::Privmsg {
                    tags,
                    chan: &body[1..off],
                    msg: &body[(off + 2)..],
                }
            }
            "CLEARCHAT" => {
                if let Some(off) = body.find(" :") {
                    Message::Clearchat {
                        tags,
                        chan: &body[1..off],
                        usr: Some(&body[(off + 2)..]),
                    }
                } else {
                    Message::Clearchat {
                        tags,
                        chan: &body[1..],
                        usr: None,
                    }
                }
            }
            "CLEARMSG" => {
                let off = body
                    .find(" :")
                    .ok_or_else(|| Error::new(ErrorKind::Other, "Malformed CLEARMSG command."))?;
                Message::Clearmsg {
                    tags,
                    chan: &body[1..off],
                    msg: &body[(off + 2)..],
                }
            }
            "HOSTTARGET" => {
                if let Some(off) = body.find(" :-") {
                    if body.len() > off + 5 {
                        if let Ok(view) = body[(off + 4)..].parse::<u32>() {
                            Message::HosttargetEnd {
                                host: &body[1..off],
                                view: Some(view),
                            }
                        } else {
                            return Err(Error::new(
                                ErrorKind::Other,
                                "Malformed HOSTTARGET command.",
                            ));
                        }
                    } else {
                        Message::HosttargetEnd {
                            host: &body[1..off],
                            view: None,
                        }
                    }
                } else {
                    let off = body.find(" :").ok_or_else(|| {
                        Error::new(ErrorKind::Other, "Malformed HOSTTARGET command.")
                    })?;
                    if body.len() < off + 3 {
                        return Err(Error::new(
                            ErrorKind::Other,
                            "Malformed HOSTTARGET command.",
                        ));
                    }
                    let host = &body[1..off];
                    let body = &body[(off + 2)..];
                    if let Some(off) = body.find(' ') {
                        if body.len() > off + 2 {
                            if let Ok(view) = body[(off + 1)..].parse::<u32>() {
                                return Ok(Message::HosttargetStart {
                                    host,
                                    chan: &body[..off],
                                    view: Some(view),
                                });
                            } else {
                                return Err(Error::new(
                                    ErrorKind::Other,
                                    "Malformed HOSTTARGET command.",
                                ));
                            }
                        } else {
                            Message::HosttargetStart {
                                host,
                                chan: body,
                                view: None,
                            }
                        }
                    } else {
                        Message::HosttargetStart {
                            host,
                            chan: body,
                            view: None,
                        }
                    }
                }
            }
            "NOTICE" => {
                let off = body
                    .find(" :")
                    .ok_or_else(|| Error::new(ErrorKind::Other, "Malformed NOTICE command."))?;
                Message::Notice {
                    tags,
                    chan: &body[1..off],
                    msg: &body[(off + 2)..],
                }
            }
            "RECONNECT" => Message::Reconnect,
            "ROOMSTATE" => Message::Roomstate {
                tags,
                chan: &body[1..],
            },
            "USERNOTICE" => {
                let off = body
                    .find(" :")
                    .ok_or_else(|| Error::new(ErrorKind::Other, "Malformed USERNOTICE command."))?;
                Message::Usernotice {
                    tags,
                    chan: &body[1..off],
                    msg: &body[(off + 2)..],
                }
            }
            "USERSTATE" => Message::Userstate {
                tags,
                chan: &body[1..],
            },
            "GLOBALUSERSTATE" => Message::GlobalUserstate { tags },
            _ => {
                return Err(Error::new(
                    ErrorKind::Other,
                    "Parsing message command failed.",
                ))
            }
        })
    }
fn parse_command ( cmd : & ' a str , body : & ' a str , tags : Option < Tags < ' a > > ) -> Result < Message < ' a > > { Ok ( match cmd { " " => Message :: Ping , " " => Message :: Pong , " " => { let off = body . find ( " " ) . ok_or_else ( | | Error :: new ( ErrorKind :: Other , " " ) ) ? ; match & body [ .. off ] { " " => Message :: CapReq { req : & body [ ( off + 2 ) .. ] , } , " " => Message :: CapAck { req : & body [ ( off + 2 ) .. ] , } , _ => return Err ( Error :: new ( ErrorKind :: Other , " " ) ) , } } " " => Message :: Pass { pass : body } , " " => Message :: Nick { nick : body } , " " => Message :: Join { chan : & body [ 1 .. ] } , " " => Message :: Part { chan : & body [ 1 .. ] } , " " => { let off = body . find ( " " ) . ok_or_else ( | | Error :: new ( ErrorKind :: Other , " " ) ) ? ; Message :: Privmsg { tags , chan : & body [ 1 .. off ] , msg : & body [ ( off + 2 ) .. ] , } } " " => { if let Some ( off ) = body . find ( " " ) { Message :: Clearchat { tags , chan : & body [ 1 .. off ] , usr : Some ( & body [ ( off + 2 ) .. ] ) , } } else { Message :: Clearchat { tags , chan : & body [ 1 .. ] , usr : None , } } } " " => { let off = body . find ( " " ) . ok_or_else ( | | Error :: new ( ErrorKind :: Other , " " ) ) ? ; Message :: Clearmsg { tags , chan : & body [ 1 .. off ] , msg : & body [ ( off + 2 ) .. ] , } } " " => { if let Some ( off ) = body . find ( " " ) { if body . len ( ) > off + 5 { if let Ok ( view ) = body [ ( off + 4 ) .. ] . parse :: < u32 > ( ) { Message :: HosttargetEnd { host : & body [ 1 .. off ] , view : Some ( view ) , } } else { return Err ( Error :: new ( ErrorKind :: Other , " " , ) ) ; } } else { Message :: HosttargetEnd { host : & body [ 1 .. off ] , view : None , } } } else { let off = body . find ( " " ) . ok_or_else ( | | { Error :: new ( ErrorKind :: Other , " " ) } ) ? ; if body . len ( ) < off + 3 { return Err ( Error :: new ( ErrorKind :: Other , " " , ) ) ; } let host = & body [ 1 .. off ] ; let body = & body [ ( off + 2 ) .. ] ; if let Some ( off ) = body . find ( ' ' ) { if body . len ( ) > off + 2 { if let Ok ( view ) = body [ ( off + 1 ) .. ] . parse :: < u32 > ( ) { return Ok ( Message :: HosttargetStart { host , chan : & body [ .. off ] , view : Some ( view ) , } ) ; } else { return Err ( Error :: new ( ErrorKind :: Other , " " , ) ) ; } } else { Message :: HosttargetStart { host , chan : body , view : None , } } } else { Message :: HosttargetStart { host , chan : body , view : None , } } } } " " => { let off = body . find ( " " ) . ok_or_else ( | | Error :: new ( ErrorKind :: Other , " " ) ) ? ; Message :: Notice { tags , chan : & body [ 1 .. off ] , msg : & body [ ( off + 2 ) .. ] , } } " " => Message :: Reconnect , " " => Message :: Roomstate { tags , chan : & body [ 1 .. ] , } , " " => { let off = body . find ( " " ) . ok_or_else ( | | Error :: new ( ErrorKind :: Other , " " ) ) ? ; Message :: Usernotice { tags , chan : & body [ 1 .. off ] , msg : & body [ ( off + 2 ) .. ] , } } " " => Message :: Userstate { tags , chan : & body [ 1 .. ] , } , " " => Message :: GlobalUserstate { tags } , _ => { return Err ( Error :: new ( ErrorKind :: Other , " " , ) ) } } ) }
Helper function for parsing message body base on the command.
Helper function for parsing message body base on the command .
cmd
&'a str
body
&'a str
tags
Option<Tags<'a>>
cmd
&'a str
body
&'a str
tags
Option<Tags<'a>>