init
This commit is contained in:
commit
62788c1b26
23 changed files with 1532 additions and 0 deletions
2
harsh-common/.gitignore
vendored
Normal file
2
harsh-common/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
/Cargo.lock
|
10
harsh-common/Cargo.toml
Normal file
10
harsh-common/Cargo.toml
Normal 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"
|
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