Add event return type
This commit is contained in:
parent
6a0e2fd7d1
commit
2c0a8ab616
7 changed files with 166 additions and 15 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -139,7 +139,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "teamspeak-query-lib"
|
name = "teamspeak-query-lib"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"telnet",
|
"telnet",
|
||||||
|
|
|
@ -106,4 +106,5 @@ pub fn clientupdate(connection: &mut Telnet, parameters: ParameterList) -> Resul
|
||||||
|
|
||||||
pub fn sendtextmessage(connection: &mut Telnet, target: SendTextMessageTarget, msg: String) -> Result<Response, String> {
|
pub fn sendtextmessage(connection: &mut Telnet, target: SendTextMessageTarget, msg: String) -> Result<Response, String> {
|
||||||
let msg = String::from(Parameter::new("msg", &msg));
|
let msg = String::from(Parameter::new("msg", &msg));
|
||||||
send_command(connection, &format!("sendtextmessage {} {}", msg, String::from(target)), false) }
|
send_command(connection, &format!("sendtextmessage {} {}", msg, String::from(target)), false)
|
||||||
|
}
|
||||||
|
|
|
@ -49,6 +49,8 @@ fn main() {
|
||||||
wrappers::skip_welcome(&mut connection);
|
wrappers::skip_welcome(&mut connection);
|
||||||
wrappers::login(&mut connection);
|
wrappers::login(&mut connection);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// You can check for the existence of subcommands, and if found use their
|
// You can check for the existence of subcommands, and if found use their
|
||||||
// matches just as you would the top level cmd
|
// matches just as you would the top level cmd
|
||||||
match cli {
|
match cli {
|
||||||
|
|
|
@ -81,4 +81,3 @@ impl Channel {
|
||||||
list_sorted
|
list_sorted
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
83
src/models/event.rs
Normal file
83
src/models/event.rs
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Event {
|
||||||
|
ChannelList,
|
||||||
|
ChannelListFinished,
|
||||||
|
NotifyTalkStatusChange,
|
||||||
|
NotifyMessage,
|
||||||
|
NotifyMessageList,
|
||||||
|
NotifyComplainList,
|
||||||
|
NotifyBanList,
|
||||||
|
NotifyClientMoved,
|
||||||
|
NotifyClientLeftView,
|
||||||
|
NotifyClientEnterView,
|
||||||
|
NotifyClientPoke,
|
||||||
|
NotifyClientChatClosed,
|
||||||
|
NotifyClientChatComposing,
|
||||||
|
NotifyClientUpdated,
|
||||||
|
NotifyClientIds,
|
||||||
|
NotifyClientDBIDFromUid,
|
||||||
|
NotifyClientNameFromUid,
|
||||||
|
NotifyClientNameFromDBID,
|
||||||
|
NotifyClientUidFromClid,
|
||||||
|
NotifyConnectionInfo,
|
||||||
|
NotifyChannelCreated,
|
||||||
|
NotifyChannelEdited,
|
||||||
|
NotifyChannelDeleted,
|
||||||
|
NotifyChannelMoved,
|
||||||
|
NotifyServerEdited,
|
||||||
|
NotifyServerUpdated,
|
||||||
|
NotifyTextMessage,
|
||||||
|
NotifyCurrentServerConnectionChanged,
|
||||||
|
NotifyConnectStatusChange,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&str> for Event {
|
||||||
|
type Error = String;
|
||||||
|
|
||||||
|
//noinspection SpellCheckingInspection
|
||||||
|
fn try_from(event_type: &str) -> Result<Self, Self::Error> {
|
||||||
|
match event_type {
|
||||||
|
"channellist" => Ok(Event::ChannelList),
|
||||||
|
"channellistfinished" => Ok(Event::ChannelListFinished),
|
||||||
|
"notifytalkstatuschange" => Ok(Event::NotifyTalkStatusChange),
|
||||||
|
"notifymessage" => Ok(Event::NotifyMessage),
|
||||||
|
"notifymessagelist" => Ok(Event::NotifyMessageList),
|
||||||
|
"notifycomplainlist" => Ok(Event::NotifyComplainList),
|
||||||
|
"notifybanlist" => Ok(Event::NotifyBanList),
|
||||||
|
"notifyclientmoved" => Ok(Event::NotifyClientMoved),
|
||||||
|
"notifyclientleftview" => Ok(Event::NotifyClientLeftView),
|
||||||
|
"notifycliententerview" => Ok(Event::NotifyClientEnterView),
|
||||||
|
"notifyclientpoke" => Ok(Event::NotifyClientPoke),
|
||||||
|
"notifyclientchatclosed" => Ok(Event::NotifyClientChatClosed),
|
||||||
|
"notifyclientchatcomposing" => Ok(Event::NotifyClientChatComposing),
|
||||||
|
"notifyclientupdated" => Ok(Event::NotifyClientUpdated),
|
||||||
|
"notifyclientids" => Ok(Event::NotifyClientIds),
|
||||||
|
"notifyclientdbidfromuid" => Ok(Event::NotifyClientDBIDFromUid),
|
||||||
|
"notifyclientnamefromuid" => Ok(Event::NotifyClientNameFromUid),
|
||||||
|
"notifyclientnamefromdbid" => Ok(Event::NotifyClientNameFromDBID),
|
||||||
|
"notifyclientuidfromclid" => Ok(Event::NotifyClientUidFromClid),
|
||||||
|
"notifyconnectioninfo" => Ok(Event::NotifyConnectionInfo),
|
||||||
|
"notifychannelcreated" => Ok(Event::NotifyChannelCreated),
|
||||||
|
"notifychanneledited" => Ok(Event::NotifyChannelEdited),
|
||||||
|
"notifychanneldeleted" => Ok(Event::NotifyChannelDeleted),
|
||||||
|
"notifychannelmoved" => Ok(Event::NotifyChannelMoved),
|
||||||
|
"notifyserveredited" => Ok(Event::NotifyServerEdited),
|
||||||
|
"notifyserverupdated" => Ok(Event::NotifyServerUpdated),
|
||||||
|
"notifytextmessage" => Ok(Event::NotifyTextMessage),
|
||||||
|
"notifycurrentserverconnectionchanged" => {
|
||||||
|
Ok(Event::NotifyCurrentServerConnectionChanged)
|
||||||
|
}
|
||||||
|
"notifyconnectstatuschange" => Ok(Event::NotifyConnectStatusChange),
|
||||||
|
_ => Err(String::from("Unknown event type.")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Display for Event {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{:?}", self)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
mod channel;
|
mod channel;
|
||||||
mod client;
|
mod client;
|
||||||
|
mod event;
|
||||||
|
|
||||||
pub use channel::Channel;
|
pub use channel::Channel;
|
||||||
pub use client::Client;
|
pub use client::Client;
|
||||||
|
pub use event::Event;
|
|
@ -1,4 +1,5 @@
|
||||||
use std::fmt::{Debug, Display, Formatter};
|
use std::fmt::{Debug, Display, Formatter};
|
||||||
|
use crate::models::Event;
|
||||||
|
|
||||||
use crate::parameter::*;
|
use crate::parameter::*;
|
||||||
|
|
||||||
|
@ -6,6 +7,14 @@ pub enum Response {
|
||||||
Ok,
|
Ok,
|
||||||
Data(ParameterList),
|
Data(ParameterList),
|
||||||
DataList(Vec<ParameterList>),
|
DataList(Vec<ParameterList>),
|
||||||
|
Event(Event, ParameterList),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum ResponseType {
|
||||||
|
Error,
|
||||||
|
Event(Event),
|
||||||
|
Data,
|
||||||
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ResponseError {
|
pub struct ResponseError {
|
||||||
|
@ -13,24 +22,67 @@ pub struct ResponseError {
|
||||||
pub msg: String,
|
pub msg: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn split_type_and_params(response_str: &str) -> Result<(ResponseType, &str), String> {
|
||||||
|
if let Some(first_space) = response_str.find(' ') {
|
||||||
|
if let Some(first_equal) = response_str.find('=') {
|
||||||
|
if first_equal > first_space {
|
||||||
|
// The first word is not a parameter. Error or Event?
|
||||||
|
let (type_str, params) = response_str.split_once(' ').unwrap(); // We found a space, so this is safe.
|
||||||
|
let response_type = get_response_type(type_str)?;
|
||||||
|
return Ok((response_type, params));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok((ResponseType::Data, response_str))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_response_type(type_str: &str) -> Result<ResponseType, String> {
|
||||||
|
if type_str == "error" {
|
||||||
|
return Ok(ResponseType::Error);
|
||||||
|
}
|
||||||
|
if type_str.starts_with("notify") || type_str == "channellist" || type_str == "channellistfinished" {
|
||||||
|
let event_type = Event::try_from(type_str)?;
|
||||||
|
return Ok(ResponseType::Event(event_type));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(ResponseType::Unknown);
|
||||||
|
}
|
||||||
|
|
||||||
impl TryFrom<String> for Response {
|
impl TryFrom<String> for Response {
|
||||||
type Error = ResponseError;
|
type Error = ResponseError;
|
||||||
|
|
||||||
fn try_from(response_str: String) -> Result<Self, ResponseError> {
|
fn try_from(response_str: String) -> Result<Self, ResponseError> {
|
||||||
let mut response_str = response_str.trim_end_matches("\n\r");
|
let response_str = response_str.trim_end_matches("\n\r");
|
||||||
|
|
||||||
|
let (rsp_type, rsp_params) = split_type_and_params(&response_str)
|
||||||
|
.map_err(|err| ResponseError {
|
||||||
|
id: -1,
|
||||||
|
msg: err,
|
||||||
|
})?;
|
||||||
|
|
||||||
if response_str.starts_with("error ") {
|
|
||||||
response_str = response_str.trim_start_matches("error ");
|
|
||||||
let response_params = parameter_parse(response_str);
|
|
||||||
Err(ResponseError::create_error(&response_params))
|
|
||||||
} else {
|
|
||||||
let mut parameter_lists: Vec<ParameterList> = Vec::new();
|
let mut parameter_lists: Vec<ParameterList> = Vec::new();
|
||||||
for response_entry in response_str.split('|') {
|
for response_entry in rsp_params.split('|') {
|
||||||
let response_params = parameter_parse(response_entry);
|
let response_params = parameter_parse(response_entry);
|
||||||
parameter_lists.push(response_params);
|
parameter_lists.push(response_params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return match rsp_type {
|
||||||
|
ResponseType::Error => {
|
||||||
|
Err(ResponseError::create_error(¶meter_lists[0]))
|
||||||
|
}
|
||||||
|
ResponseType::Event(event_type) => {
|
||||||
|
Ok(Response::Event(event_type, parameter_lists[0].clone()))
|
||||||
|
}
|
||||||
|
ResponseType::Data => {
|
||||||
Ok(Response::DataList(parameter_lists))
|
Ok(Response::DataList(parameter_lists))
|
||||||
}
|
}
|
||||||
|
ResponseType::Unknown => {
|
||||||
|
Err(ResponseError {
|
||||||
|
id: -1,
|
||||||
|
msg: "Unknown response type.".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,6 +104,11 @@ impl Debug for Response {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Response::Event(event, params) => {
|
||||||
|
write!(f, "Event: {:?}", event)?;
|
||||||
|
write!(f, "{:?};", params)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,6 +135,13 @@ impl Display for Response {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Response::Event(event, params) => {
|
||||||
|
write!(f, "Event: {}", event)?;
|
||||||
|
for param in params {
|
||||||
|
write!(f, "{};", param)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue