From 0affe3e351c6c8638ec9a8752d62b416b833af70 Mon Sep 17 00:00:00 2001 From: buj Date: Sun, 25 Aug 2024 18:05:40 +0700 Subject: [PATCH 1/3] Replaced `serde` with `tinyjson` --- Cargo.lock | 55 ++++++++--------------------------------------------- Cargo.toml | 7 +++---- src/hook.rs | 36 +++++++++++++++++++++++++++-------- 3 files changed, 39 insertions(+), 59 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 80809de..7895fa4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -156,12 +156,11 @@ dependencies = [ [[package]] name = "discord-backup-util" -version = "0.2.0" +version = "0.2.1" dependencies = [ "minreq", "rand", - "serde", - "serde_json", + "tinyjson", "ureq", "zip", ] @@ -283,12 +282,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "itoa" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" - [[package]] name = "libc" version = "0.2.158" @@ -530,44 +523,6 @@ dependencies = [ "untrusted", ] -[[package]] -name = "ryu" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" - -[[package]] -name = "serde" -version = "1.0.208" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cff085d2cb684faa248efb494c39b68e522822ac0de72ccf08109abde717cfb2" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.208" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.125" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83c8e735a073ccf5be70aa8066aa984eaf2fa000db6c8d0100ae605b366d31ed" -dependencies = [ - "itoa", - "memchr", - "ryu", - "serde", -] - [[package]] name = "sha1" version = "0.10.6" @@ -634,6 +589,12 @@ dependencies = [ "syn", ] +[[package]] +name = "tinyjson" +version = "2.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ab95735ea2c8fd51154d01e39cf13912a78071c2d89abc49a7ef102a7dd725a" + [[package]] name = "tinyvec" version = "1.8.0" diff --git a/Cargo.toml b/Cargo.toml index cb0a398..6268534 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "discord-backup-util" -version = "0.2.0" +version = "0.2.1" authors = ["buj"] license = "AGPL3-or-later" description = "A tiny tool to backup stuff to Discord" @@ -12,14 +12,13 @@ categories = ["command-line-utilities"] edition = "2021" [features] -default = ["ureq"] +default = ["minreq"] minreq = ["dep:minreq"] ureq = ["dep:ureq"] [dependencies] minreq = { version = "2.12.0", features = ["https-bundled-probe"], optional = true } rand = "0.8.5" -serde = { version = "1.0.208", features = ["derive"] } -serde_json = "1.0.125" +tinyjson = "2.5.1" ureq = { version = "2.10.1", optional = true } zip = { version = "2.2.0", features = ["aes", "aes-crypto", "deflate", "deflate-zlib", "deflate64"], default-features = false } diff --git a/src/hook.rs b/src/hook.rs index 3e10016..7472266 100644 --- a/src/hook.rs +++ b/src/hook.rs @@ -1,7 +1,7 @@ use std::{num::NonZeroU64, ops::Add, time::Duration}; use rand::Rng; -use serde::Deserialize; +use tinyjson::JsonValue; use crate::log::Logger; @@ -75,7 +75,6 @@ impl Message { } } -#[derive(Deserialize)] struct ApiMessage { id: String, } @@ -174,19 +173,40 @@ impl Webhook { } } { Ok(x) => { - let parsed: ApiMessage = match serde_json::from_reader({ + let parsed: ApiMessage = match { #[cfg(feature = "ureq")] { - x.into_reader() + x.into_string() } #[cfg(feature = "minreq")] { - std::io::Cursor::new(x.as_bytes()) + x.as_str() } - }) { - Ok(x) => x, + } { + Ok(x) => match x.parse::() { + Ok(x) => match x { + JsonValue::Object(x) => match x.get("id") { + Some(JsonValue::String(x)) => ApiMessage { id: x.to_owned() }, + x => { + logger.error(&format!("Received invalid json, retrying in 5 minutes\n\nExpected string, found {x:?}")); + std::thread::sleep(Duration::from_secs(300)); + continue; + } + } + x => { + logger.error(&format!("Received invalid json, retrying in 5 minutes\n\nExpected object, found {x:?}")); + std::thread::sleep(Duration::from_secs(300)); + continue; + } + } + Err(why) => { + logger.error(&format!("Failed to parse json, retrying in 5 minutes...\n\n{why}")); + std::thread::sleep(Duration::from_secs(300)); + continue; + } + }, Err(why) => { - println!("Failed to parse message, retrying in 5 minutes...\n\n{why}"); + logger.error(&format!("Failed to parse message, retrying in 5 minutes...\n\n{why}")); std::thread::sleep(Duration::from_secs(300)); continue; } From 66426d71120fba1e0be4aed74eb3a2b8109d68d1 Mon Sep 17 00:00:00 2001 From: buj Date: Sun, 25 Aug 2024 19:19:41 +0700 Subject: [PATCH 2/3] Windows support (possibly functions) --- .cargo/config.toml | 6 ++++++ .github/workflows/check.yml | 5 ++++- .github/workflows/publish.yml | 17 ++++++++++++++++- .gitignore | 1 + Cargo.toml | 2 +- src/main.rs | 2 ++ src/temp.rs | 23 ++++++++++++++++++++++- 7 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..00f7a58 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,6 @@ +[target.x86_64-pc-windows-msvc] +linker = "lld-link" + +[target.x86_64-unknown-linux-gnu] +linker = "clang" +rustflags = ["-C", "link-arg=-fuse-ld=/usr/bin/mold"] \ No newline at end of file diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 24e88fe..f2df549 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -11,10 +11,13 @@ env: jobs: build: - runs-on: ubuntu-latest steps: + - uses: awalsh128/cache-apt-pkgs-action@v1 + with: + packages: mold + version: 1.0 - uses: actions/checkout@v4 - name: Build run: cargo build --verbose diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 966b254..716d6e3 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -19,8 +19,9 @@ jobs: - uses: ningenMe/setup-rustup@v1.1.0 - uses: awalsh128/cache-apt-pkgs-action@v1 with: - packages: musl-tools + packages: musl-tools mold gcc-mingw-w64 version: 1.0 + - run: cargo install cargo-xwin --locked - name: Create release dir run: rm -fr target/github-release && mkdir -p target/github-release @@ -52,6 +53,20 @@ jobs: run: cargo build --verbose --release --target i686-unknown-linux-gnu - name: Copy i686-unknown-linux-gnu artifact run: cp target/i686-unknown-linux-gnu/release/discord-backup-util target/github-release/discord-backup-util.i686-unknown-linux-gnu + + - name: Install x86_64-pc-windows-gnu toolchain + run: rustup target add x86_64-pc-windows-gnu + - name: Build for x86_64-pc-windows-gnu + run: cargo build --verbose --release --target x86_64-pc-windows-gnu + - name: Copy x86_64-pc-windows-gnu artifact + run: cp target/x86_64-pc-windows-gnu/release/discord-backup-util.exe target/github-release/discord-backup-util.x86_64-pc-windows-gnu.exe + + - name: Install x86_64-pc-windows-msvc toolchain + run: rustup target add x86_64-pc-windows-msvc + - name: Build for x86_64-pc-windows-msvc + run: cargo xwin build --verbose --release --target x86_64-pc-windows-msvc + - name: Copy x86_64-pc-windows-msvc artifact + run: cp target/x86_64-pc-windows-msvc/release/discord-backup-util.exe target/github-release/discord-backup-util.x86_64-pc-windows-msvc.exe - uses: colathro/crate-version@1.0.0 id: crate-version diff --git a/.gitignore b/.gitignore index c851cb0..79a5a95 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /target +/.xwin # local config for testing /.backup_config diff --git a/Cargo.toml b/Cargo.toml index 6268534..0564fb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ categories = ["command-line-utilities"] edition = "2021" [features] -default = ["minreq"] +default = ["ureq"] minreq = ["dep:minreq"] ureq = ["dep:ureq"] diff --git a/src/main.rs b/src/main.rs index 769cbba..d6221d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ use upload::upload; #[cfg(not(any(feature = "ureq", feature = "minreq")))] compile_error!("Either 'ureq' or 'minreq' feature must be enabled"); +#[cfg(all(feature = "ureq", feature = "minreq"))] +compile_error!("Cannot enable both 'ureq' and 'minreq' features"); mod config; mod hook; diff --git a/src/temp.rs b/src/temp.rs index 647b1bd..ac19ea0 100644 --- a/src/temp.rs +++ b/src/temp.rs @@ -2,12 +2,33 @@ use std::{path::PathBuf, str::FromStr}; use rand::Rng; +#[cfg(windows)] pub fn temp_path() -> PathBuf { + let Ok(root) = std::env::var("TEMP") else { + panic!("'TEMP' is not set"); + }; + + let name: String = rand::thread_rng() + .sample_iter(rand::distributions::Alphanumeric) + .map(|x| x as char) + .take(32) + .collect(); + + PathBuf::from_str(&format!("{root}\\discord-backup-util.{name}")).unwrap() +} + +#[cfg(unix)] +pub fn temp_path() -> PathBuf { + let temp = match std::env::var("TMPDIR") { + Ok(x) => x, + Err(_) => "/var/tmp".to_string(), + }; + let name: String = rand::thread_rng() .sample_iter(rand::distributions::Alphanumeric) .map(|x| x as char) .take(32) .collect(); - PathBuf::from_str(&format!("/tmp/discord-backup-util.{name}")).unwrap() + PathBuf::from_str(&format!("{temp}/discord-backup-util.{name}")).unwrap() } From d13622ad911a7cffbe38566fae0749f396184128 Mon Sep 17 00:00:00 2001 From: buj Date: Sun, 25 Aug 2024 19:23:18 +0700 Subject: [PATCH 3/3] Removed unused .gitignore item --- .gitignore | 1 - src/hook.rs | 12 ++++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 79a5a95..c851cb0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ /target -/.xwin # local config for testing /.backup_config diff --git a/src/hook.rs b/src/hook.rs index 7472266..ca27627 100644 --- a/src/hook.rs +++ b/src/hook.rs @@ -192,21 +192,25 @@ impl Webhook { std::thread::sleep(Duration::from_secs(300)); continue; } - } + }, x => { logger.error(&format!("Received invalid json, retrying in 5 minutes\n\nExpected object, found {x:?}")); std::thread::sleep(Duration::from_secs(300)); continue; } - } + }, Err(why) => { - logger.error(&format!("Failed to parse json, retrying in 5 minutes...\n\n{why}")); + logger.error(&format!( + "Failed to parse json, retrying in 5 minutes...\n\n{why}" + )); std::thread::sleep(Duration::from_secs(300)); continue; } }, Err(why) => { - logger.error(&format!("Failed to parse message, retrying in 5 minutes...\n\n{why}")); + logger.error(&format!( + "Failed to parse message, retrying in 5 minutes...\n\n{why}" + )); std::thread::sleep(Duration::from_secs(300)); continue; }