This commit is contained in:
JOLIMAITRE Matthieu 2022-08-22 01:59:56 +02:00
commit 62788c1b26
23 changed files with 1532 additions and 0 deletions

2
harsh-common/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
/Cargo.lock

10
harsh-common/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "harsh_common"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0.144", features = ["derive"] }
serde_json = "1.0.83"

View 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
View 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;

View 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 },
}
}