implemented channel queries

This commit is contained in:
mb 2022-08-22 11:25:49 +03:00
parent 62788c1b26
commit 28a3812812
10 changed files with 208 additions and 67 deletions

View file

@ -1,29 +1,77 @@
#[derive(Debug)]
pub enum ClientRequest {
Ping(Ping),
}
#[derive(Debug)]
pub struct Ping {
pub content: String,
}
#[derive(Debug)]
pub struct ChannelList {}
#[derive(Debug)]
pub struct ChannelCreate {
pub name: String,
}
#[derive(Debug)]
pub struct ChannelDelete {
pub channel_id: u64,
}
#[derive(Debug)]
pub struct ChannelGetName {
pub channel_id: u64,
}
#[derive(Debug)]
pub enum ClientRequest {
Ping(Ping),
ChannelList(ChannelList),
ChannelCreate(ChannelCreate),
ChannelDelete(ChannelDelete),
ChannelGetName(ChannelGetName),
}
impl ClientRequest {
pub fn new_ping(content: String) -> Self {
Self::Ping(Ping { content })
}
pub fn new_channel_list() -> Self {
Self::ChannelList(ChannelList {})
}
pub fn new_channel_create(name: String) -> Self {
Self::ChannelCreate(ChannelCreate { name })
}
pub fn new_channel_delete(channel_id: u64) -> Self {
Self::ChannelDelete(ChannelDelete { channel_id })
}
pub fn new_channel_get_name(channel_id: u64) -> Self {
Self::ChannelGetName(ChannelGetName { channel_id })
}
pub fn try_parse(line: &str) -> Option<Self> {
use repr::Command::*;
let command: repr::Command = serde_json::from_str(line).ok()?;
let mapped = match command {
repr::Command::ping { content } => Self::Ping(Ping { content }),
ping { content } => Self::Ping(Ping { content }),
channel_list {} => Self::ChannelList(ChannelList {}),
channel_create { name } => Self::ChannelCreate(ChannelCreate { name }),
channel_delete { channel_id } => Self::ChannelDelete(ChannelDelete { channel_id }),
channel_get_name { channel_id } => Self::ChannelGetName(ChannelGetName { channel_id }),
};
Some(mapped)
}
pub fn serialize(self) -> String {
use repr::Command::*;
let mapped = match self {
Self::Ping(Ping { content }) => repr::Command::ping { content },
Self::Ping(Ping { content }) => ping { content },
Self::ChannelList(ChannelList {}) => repr::Command::channel_list {},
Self::ChannelCreate(ChannelCreate { name }) => channel_create { name },
Self::ChannelDelete(ChannelDelete { channel_id }) => channel_delete { channel_id },
Self::ChannelGetName(ChannelGetName { channel_id }) => channel_get_name { channel_id },
};
serde_json::to_string(&mapped).unwrap()
}
@ -38,5 +86,9 @@ mod repr {
#[serde(tag = "type")]
pub enum Command {
ping { content: String },
channel_list {},
channel_create { name: String },
channel_delete { channel_id: u64 },
channel_get_name { channel_id: u64 },
}
}

View file

@ -7,8 +7,8 @@ mod tests {
}
}
pub use client::{ClientRequest, Ping};
mod client;
pub use client::ClientRequest;
pub mod client;
pub use server::{Pong, ServerRequest};
mod server;
pub use server::ServerRequest;
pub mod server;

View file

@ -1,27 +1,51 @@
pub enum ServerRequest {
Pong(Pong),
}
pub struct Pong {
pub content: String,
}
pub struct ChannelList {
pub channels: Vec<u64>,
}
pub struct ChannelGetName {
pub id: u64,
pub name: Option<String>,
}
pub enum ServerRequest {
Pong(Pong),
ChannelList(ChannelList),
ChannelGetName(ChannelGetName),
}
impl ServerRequest {
pub fn new_pong(content: String) -> Self {
Self::Pong(Pong { content })
}
pub fn new_channel_list(channels: Vec<u64>) -> Self {
Self::ChannelList(ChannelList { channels })
}
pub fn new_channel_get_name(id: u64, name: Option<String>) -> Self {
Self::ChannelGetName(ChannelGetName { name, id })
}
pub fn try_parse(line: &str) -> Option<Self> {
use repr::Command::*;
let command: repr::Command = serde_json::from_str(line).ok()?;
let mapped = match command {
repr::Command::pong { content } => Self::Pong(Pong { content }),
pong { content } => Self::Pong(Pong { content }),
channel_list { channels } => Self::ChannelList(ChannelList { channels }),
channel_get_name { id, name } => Self::ChannelGetName(ChannelGetName { id, name }),
};
Some(mapped)
}
pub fn serialize(self) -> String {
use repr::Command::*;
let mapped = match self {
Self::Pong(Pong { content }) => repr::Command::pong { content },
Self::Pong(Pong { content }) => pong { content },
Self::ChannelList(ChannelList { channels }) => channel_list { channels },
Self::ChannelGetName(ChannelGetName { id, name }) => channel_get_name { id, name },
};
serde_json::to_string(&mapped).unwrap()
}
@ -36,5 +60,7 @@ mod repr {
#[serde(tag = "type")]
pub enum Command {
pong { content: String },
channel_list { channels: Vec<u64> },
channel_get_name { id: u64, name: Option<String> },
}
}