From 6d26330bd85ff5a1e86cd9154896e88dd386792a Mon Sep 17 00:00:00 2001 From: Cappy Ishihara Date: Sun, 2 Jun 2024 03:37:57 +0700 Subject: [PATCH] use tracing instead, revert this string converter function --- Cargo.lock | 13 +++++++++++++ Cargo.toml | 1 + src/utils.rs | 20 +++++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 70108d9..e03e83e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1185,6 +1185,7 @@ dependencies = [ "tokio", "toml", "tower-http", + "tracing", "tracing-subscriber", "uuid", ] @@ -1587,9 +1588,21 @@ checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ "log", "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tracing-core" version = "0.1.32" diff --git a/Cargo.toml b/Cargo.toml index 5647169..3716d88 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ axum = { version = "0.7.5", features = ["ws", "macros", "http2"] } tower-http = { version = "0.5.2", features = ["trace"] } tokio = { version = "1.37.0", features = ["full"] } tracing-subscriber = { version = "0.3.18", features = ["env-filter", "chrono"] } +tracing = "0.1.40" # TODO: Sort it! # TODO: Replace Vec and &[u8] by Bytes diff --git a/src/utils.rs b/src/utils.rs index cd57da4..2e00ea3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -16,9 +16,27 @@ pub fn rand() -> [u8; 50] { nums } //? What is this guy doing +#[tracing::instrument] pub fn bytes_into_string(code: &[u8]) -> String { + // This *might* be the correct way to do it. + // code.iter().map(|byte| format!("{:02x}", byte)).collect::() // ????? Why do you need this? Why not just String::from_utf8()?? - String::from_utf8_lossy(code).to_string() + // So we need to turn each byte into a string with a 2-digit hexadecimal representation apparently... + + // hex::encode_to_slice(input, output) + + let res = code.iter().fold(String::new(), |mut acc, byte| { + acc.push_str(&format!("{:02x}", byte)); + acc + }); // This is the same as the above, but with a fold instead of a map + + + // Can we do this with hex::encode instead? + + + res + + // String::from_utf8_lossy(code).to_string() // Tried this, causes corrupted string } // Конец кор функций