init
This commit is contained in:
commit
62788c1b26
23 changed files with 1532 additions and 0 deletions
42
harsh-common/src/client.rs
Normal file
42
harsh-common/src/client.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
#[derive(Debug)]
|
||||
pub enum ClientRequest {
|
||||
Ping(Ping),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Ping {
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
impl ClientRequest {
|
||||
pub fn new_ping(content: String) -> Self {
|
||||
Self::Ping(Ping { content })
|
||||
}
|
||||
|
||||
pub fn try_parse(line: &str) -> Option<Self> {
|
||||
let command: repr::Command = serde_json::from_str(line).ok()?;
|
||||
let mapped = match command {
|
||||
repr::Command::ping { content } => Self::Ping(Ping { content }),
|
||||
};
|
||||
Some(mapped)
|
||||
}
|
||||
|
||||
pub fn serialize(self) -> String {
|
||||
let mapped = match self {
|
||||
Self::Ping(Ping { content }) => repr::Command::ping { content },
|
||||
};
|
||||
serde_json::to_string(&mapped).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
mod repr {
|
||||
#![allow(non_camel_case_types)]
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum Command {
|
||||
ping { content: String },
|
||||
}
|
||||
}
|
14
harsh-common/src/lib.rs
Normal file
14
harsh-common/src/lib.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = 2 + 2;
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
}
|
||||
|
||||
pub use client::{ClientRequest, Ping};
|
||||
mod client;
|
||||
|
||||
pub use server::{Pong, ServerRequest};
|
||||
mod server;
|
40
harsh-common/src/server.rs
Normal file
40
harsh-common/src/server.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
pub enum ServerRequest {
|
||||
Pong(Pong),
|
||||
}
|
||||
|
||||
pub struct Pong {
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
impl ServerRequest {
|
||||
pub fn new_pong(content: String) -> Self {
|
||||
Self::Pong(Pong { content })
|
||||
}
|
||||
|
||||
pub fn try_parse(line: &str) -> Option<Self> {
|
||||
let command: repr::Command = serde_json::from_str(line).ok()?;
|
||||
let mapped = match command {
|
||||
repr::Command::pong { content } => Self::Pong(Pong { content }),
|
||||
};
|
||||
Some(mapped)
|
||||
}
|
||||
|
||||
pub fn serialize(self) -> String {
|
||||
let mapped = match self {
|
||||
Self::Pong(Pong { content }) => repr::Command::pong { content },
|
||||
};
|
||||
serde_json::to_string(&mapped).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
mod repr {
|
||||
#![allow(non_camel_case_types)]
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum Command {
|
||||
pong { content: String },
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue