From 4a78061dba680c26d96d9cfd3a7eecee6a83d2bb Mon Sep 17 00:00:00 2001 From: MicroPanda123 Date: Mon, 18 Nov 2024 00:09:54 +0100 Subject: [PATCH 1/2] added zasa :3 --- server/Cargo.lock | 9 +++++-- server/Cargo.toml | 3 +-- server/src/main.rs | 58 +++++++++++++++++++++++++++++++++------------- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/server/Cargo.lock b/server/Cargo.lock index e1bd910..ede6a0a 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -804,8 +804,7 @@ dependencies = [ "ntex", "ntex-files", "rand", - "serde", - "serde_json", + "zasa", ] [[package]] @@ -1893,6 +1892,12 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +[[package]] +name = "zasa" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bd6fe2c1435fa0bd5ab924aba1f8f18f70e87735c142ede178a4fb14f9e85ed" + [[package]] name = "zerocopy" version = "0.7.35" diff --git a/server/Cargo.toml b/server/Cargo.toml index af29611..039b047 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -7,6 +7,5 @@ edition = "2021" env_logger = "0.11.3" ntex = { version = "2.0.0", features = [ "tokio", "rustls" ] } ntex-files = "2.0.0" -serde = "1.0.210" -serde_json = "1.0.128" rand = "0.8.5" +zasa = "0.3.0" diff --git a/server/src/main.rs b/server/src/main.rs index 5c76db3..fa5b877 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,22 +1,48 @@ use std::fs::{self}; -use ntex::{http::{header, Response}, web::{self, middleware}}; -use ntex_files as nfs; use ::rand::{thread_rng, Rng}; -use serde::{Deserialize, Serialize}; +use ntex::{ + http::{header, Response}, + web::{self, middleware}, +}; +use ntex_files as nfs; +use std::collections::HashMap; +use zasa::{ + parser::Parser, + value::{normalize, Normalize, Value, ValueError}, +}; -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone)] struct WebringMember { name: String, site: String, } +impl Normalize for WebringMember { + fn normalize(value: Value) -> Result { + match value { + Value::Object(a) => { + let member = WebringMember { + name: a["name"].to_string(), + site: a["site"].to_string(), + }; + Ok(member) + } + _ => Err(ValueError::ValueNotArray), + } + } +} + #[web::get("/next/{name}")] async fn next( members: web::types::State>, name: web::types::Path, ) -> impl web::Responder { - if let Some((i, _)) = members.iter().enumerate().find(|(_, member)| member.name == *name) { + if let Some((i, _)) = members + .iter() + .enumerate() + .find(|(_, member)| member.name == *name) + { let next_index = (i + 1) % members.len(); let next_site = &members[next_index].site; @@ -37,7 +63,11 @@ async fn prev( members: web::types::State>, name: web::types::Path, ) -> impl web::Responder { - if let Some((i, _)) = members.iter().enumerate().find(|(_, member)| member.name == *name) { + if let Some((i, _)) = members + .iter() + .enumerate() + .find(|(_, member)| member.name == *name) + { let prev_index = if i == 0 { members.len() - 1 } else { i - 1 }; let prev_site = &members[prev_index].site; @@ -54,9 +84,7 @@ async fn prev( } #[web::get("/rand")] -async fn rand( - members: web::types::State>, -) -> impl web::Responder { +async fn rand(members: web::types::State>) -> impl web::Responder { let rand_index = thread_rng().gen_range(0..members.len()); let rand_site = &members[rand_index].site; @@ -70,8 +98,7 @@ async fn rand( async fn main() -> std::io::Result<()> { env_logger::init(); - let nix_webring_dir = std::env::var("NIX_WEBRING_DIR") - .expect("NIX_WEBRING_DIR not found"); + let nix_webring_dir = std::env::var("NIX_WEBRING_DIR").expect("NIX_WEBRING_DIR not found"); let nix_webring_port = std::env::var("NIX_WEBRING_PORT") .expect("NIX_WEBRING_PORT not found") @@ -80,10 +107,9 @@ async fn main() -> std::io::Result<()> { web::server(move || { let path = format!("{nix_webring_dir}/webring.json"); - let json = fs::read_to_string(&path) - .expect(&format!("couldn't open {path}")); + let json = fs::read_to_string(&path).expect(&format!("couldn't open {path}")); - let members: Vec = serde_json::from_str(&json) + let members: Vec = normalize(Parser::new(json.chars()).parse().unwrap()) .expect(&format!("failed deserializing webring members: {json}")); web::App::new() @@ -97,8 +123,8 @@ async fn main() -> std::io::Result<()> { .service( nfs::Files::new("/", nix_webring_dir.clone()) .index_file("index.html") - .redirect_to_slash_directory() - ) + .redirect_to_slash_directory(), + ), ) }) .bind(("127.0.0.1", nix_webring_port))? -- 2.47.0 From 71ccf335d061c9c752d9a3b9d820b2274917f5ad Mon Sep 17 00:00:00 2001 From: MicroPanda123 Date: Mon, 18 Nov 2024 22:25:36 +0100 Subject: [PATCH 2/2] updated zasa --- server/Cargo.lock | 18 +++++++++++++++++- server/Cargo.toml | 2 +- server/src/main.rs | 23 ++--------------------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/server/Cargo.lock b/server/Cargo.lock index ede6a0a..8683f50 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -1894,9 +1894,25 @@ checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" [[package]] name = "zasa" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e406a4941ce0c5351a09bb1d30e93744f430f80ffebcb1687f1550cff4553de4" +dependencies = [ + "zasa_core", + "zasa_derive", +] + +[[package]] +name = "zasa_core" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bd6fe2c1435fa0bd5ab924aba1f8f18f70e87735c142ede178a4fb14f9e85ed" +checksum = "828c3c824073e930389d33e932d376504bc88acf89be3c05bfa9bb406a09e52f" + +[[package]] +name = "zasa_derive" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f3f2e32a4c0a55c14568fee40a9ca566193d0c78d62282cb451e38f9defc5ec" [[package]] name = "zerocopy" diff --git a/server/Cargo.toml b/server/Cargo.toml index 039b047..bfc4a39 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -8,4 +8,4 @@ env_logger = "0.11.3" ntex = { version = "2.0.0", features = [ "tokio", "rustls" ] } ntex-files = "2.0.0" rand = "0.8.5" -zasa = "0.3.0" +zasa = { version = "0.4.0", features = [ "derive" ] } diff --git a/server/src/main.rs b/server/src/main.rs index fa5b877..c324c10 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -6,33 +6,14 @@ use ntex::{ web::{self, middleware}, }; use ntex_files as nfs; -use std::collections::HashMap; -use zasa::{ - parser::Parser, - value::{normalize, Normalize, Value, ValueError}, -}; +use zasa::{parser::Parser, value::normalize, Normalize}; -#[derive(Clone)] +#[derive(Clone, Normalize)] struct WebringMember { name: String, site: String, } -impl Normalize for WebringMember { - fn normalize(value: Value) -> Result { - match value { - Value::Object(a) => { - let member = WebringMember { - name: a["name"].to_string(), - site: a["site"].to_string(), - }; - Ok(member) - } - _ => Err(ValueError::ValueNotArray), - } - } -} - #[web::get("/next/{name}")] async fn next( members: web::types::State>, -- 2.47.0