using shared lib for shared structs

This commit is contained in:
2026-08-15 18:27:37 +02:00
parent 2e4e6ebdb0
commit f6f1b764a4
5 changed files with 17 additions and 41 deletions
Generated
+3
View File
@@ -682,6 +682,9 @@ dependencies = [
[[package]] [[package]]
name = "shared" name = "shared"
version = "0.1.0" version = "0.1.0"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
+2 -16
View File
@@ -3,22 +3,10 @@ use ringbuf::{
HeapRb, HeapRb,
traits::{Consumer, Observer, Producer, Split}, traits::{Consumer, Observer, Producer, Split},
}; };
use serde::{Deserialize, Serialize}; use shared::{ConectionResponse, ResponseStatus};
use std::sync::Arc; use std::sync::Arc;
use tokio::net::{TcpStream, UdpSocket}; use tokio::net::{TcpStream, UdpSocket};
#[derive(Serialize, Deserialize, PartialEq)]
enum ResponseStatus {
Ok,
Err,
}
#[derive(Serialize, Deserialize)]
struct ConectionResponse {
status: ResponseStatus,
client_id: u32,
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let server_address = "127.0.0.1:8080"; let server_address = "127.0.0.1:8080";
@@ -34,15 +22,13 @@ async fn main() {
match tcp_stream.try_read(&mut tcp_buf) { match tcp_stream.try_read(&mut tcp_buf) {
Ok(n) => { Ok(n) => {
// let connection_response = str::from_utf8(&tcp_buf[0..n]).expect("failed to create str");
let connection_response: ConectionResponse = let connection_response: ConectionResponse =
serde_json::from_slice(&tcp_buf[0..n]).unwrap(); serde_json::from_slice(&tcp_buf[0..n]).unwrap();
session_client_id = connection_response.client_id; session_client_id = connection_response.client_id;
println!( println!(
"tcp response status: {}, clientId: {}", "tcp response status Ok: {}, clientId: {}",
connection_response.status == ResponseStatus::Ok, connection_response.status == ResponseStatus::Ok,
connection_response.client_id connection_response.client_id
); );
+1 -14
View File
@@ -1,7 +1,6 @@
use serde::{Deserialize, Serialize}; use shared::{ConectionResponse, ResponseStatus};
use std::sync::Arc; use std::sync::Arc;
use std::{collections::HashMap, net::SocketAddr}; use std::{collections::HashMap, net::SocketAddr};
use tokio::io::AsyncWriteExt; use tokio::io::AsyncWriteExt;
use tokio::{ use tokio::{
net::{TcpListener, UdpSocket}, net::{TcpListener, UdpSocket},
@@ -15,18 +14,6 @@ struct Client {
type State = Arc<RwLock<HashMap<u32, Client>>>; type State = Arc<RwLock<HashMap<u32, Client>>>;
#[derive(Serialize, Deserialize)]
enum ResponseStatus {
Ok,
Err,
}
#[derive(Serialize, Deserialize)]
struct ConectionResponse {
status: ResponseStatus,
client_id: u32,
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let listen_address = "0.0.0.0:8080"; let listen_address = "0.0.0.0:8080";
+1
View File
@@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
serde = { version = "1.0.229", features = ["derive"] }
+10 -11
View File
@@ -1,14 +1,13 @@
pub fn add(left: u64, right: u64) -> u64 { use serde::{Deserialize, Serialize};
left + right
#[derive(Serialize, Deserialize, PartialEq)]
pub enum ResponseStatus {
Ok,
Err,
} }
#[cfg(test)] #[derive(Serialize, Deserialize)]
mod tests { pub struct ConectionResponse {
use super::*; pub status: ResponseStatus,
pub client_id: u32,
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
} }