From 2c71e8dbda83fe2d5cec8c0e0c5bdef2dcd80c5e Mon Sep 17 00:00:00 2001 From: actioninja Date: Sun, 14 May 2023 04:03:11 -0700 Subject: [PATCH] ffi macro v2 initial conversions --- Cargo.lock | 87 +++++++++++++++++++++++++++++++++++++++++++---------- Cargo.toml | 1 + src/json.rs | 16 +++++----- src/log.rs | 46 ++++++++++++++-------------- src/time.rs | 52 +++++++++++++++----------------- src/url.rs | 15 +++++---- 6 files changed, 135 insertions(+), 82 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9fee1ee0..51cf37c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -60,7 +60,7 @@ checksum = "96cf8829f67d2eab0b2dfa42c5d0ef737e0724e4a82b01b3e292456202b19716" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.98", ] [[package]] @@ -150,6 +150,25 @@ version = "3.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37ccbd214614c6783386c1af30caf03192f17891059cecc394b4fb119e363de3" +[[package]] +name = "byond_fn" +version = "0.4.0" +dependencies = [ + "byond_fn_impl", + "serde", + "serde_json", +] + +[[package]] +name = "byond_fn_impl" +version = "0.4.0" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.15", +] + [[package]] name = "bytemuck" version = "1.10.0" @@ -460,7 +479,7 @@ checksum = "532b4c15dccee12c7044f1fcad956e98410860b22231e44a3b827464797ca7bf" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.98", ] [[package]] @@ -590,7 +609,7 @@ checksum = "3dbc4f084ec5a3f031d24ccedeb87ab2c3189a2f33b8d070889073837d5ea09e" dependencies = [ "frunk_proc_macro_helpers", "quote", - "syn", + "syn 1.0.98", ] [[package]] @@ -602,7 +621,7 @@ dependencies = [ "frunk_core", "proc-macro2", "quote", - "syn", + "syn 1.0.98", ] [[package]] @@ -626,7 +645,7 @@ dependencies = [ "frunk_proc_macro_helpers", "proc-macro-hack", "quote", - "syn", + "syn 1.0.98", ] [[package]] @@ -921,7 +940,7 @@ dependencies = [ "autocfg", "derive_utils", "quote", - "syn", + "syn 1.0.98", ] [[package]] @@ -1444,7 +1463,7 @@ checksum = "b501e44f11665960c7e7fcf062c7d96a14ade4aa98116c004b2e37b5be7d736c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.98", ] [[package]] @@ -1555,7 +1574,7 @@ checksum = "710faf75e1b33345361201d36d04e98ac1ed8909151a017ed384700836104c74" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.98", ] [[package]] @@ -1594,6 +1613,30 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.98", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro-hack" version = "0.5.19" @@ -1602,18 +1645,18 @@ checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" [[package]] name = "proc-macro2" -version = "1.0.40" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.20" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" +checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" dependencies = [ "proc-macro2", ] @@ -1856,6 +1899,7 @@ version = "1.2.0" dependencies = [ "aho-corasick", "base64", + "byond_fn", "chrono", "const-random", "dashmap", @@ -2005,7 +2049,7 @@ checksum = "6f2122636b9fe3b81f1cb25099fcf2d3f542cdb1d45940d56c713158884a05da" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.98", ] [[package]] @@ -2158,6 +2202,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn" +version = "2.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "tap" version = "1.0.1" @@ -2195,7 +2250,7 @@ checksum = "0396bc89e626244658bef819e22d0cc459e795a5ebe878e6ec336d1674a8d79a" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.98", ] [[package]] @@ -2458,7 +2513,7 @@ dependencies = [ "log", "proc-macro2", "quote", - "syn", + "syn 1.0.98", "wasm-bindgen-shared", ] @@ -2492,7 +2547,7 @@ checksum = "7d94ac45fcf608c1f45ef53e748d35660f168490c10b23704c7779ab8f5c3048" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.98", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/Cargo.toml b/Cargo.toml index 024568e2..ea33b964 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ debug = true [dependencies] thiserror = "1.0" +byond_fn = "0.5" flume = { version = "0.10", optional = true } chrono = { version = "0.4", optional = true } base64 = { version = "0.13", optional = true } diff --git a/src/json.rs b/src/json.rs index db75d967..850b2a9a 100644 --- a/src/json.rs +++ b/src/json.rs @@ -1,16 +1,16 @@ +use byond_fn::byond_fn; use serde_json::Value; use std::cmp; const VALID_JSON_MAX_RECURSION_DEPTH: usize = 8; -byond_fn!(fn json_is_valid(text) { - let value = match serde_json::from_str::(text) { - Ok(value) => value, - Err(_) => return Some("false".to_owned()) - }; - - Some(get_recursion_level(&value).is_ok().to_string()) -}); +#[byond_fn] +fn json_is_valid(text: &str) -> bool { + match serde_json::from_str::(text) { + Ok(value) => get_recursion_level(&value).is_ok(), + Err(_) => false, + } +} /// Gets the recursion level of the given value /// If it is above VALID_JSON_MAX_RECURSION_DEPTH, returns Err(()) diff --git a/src/log.rs b/src/log.rs index de45fc0b..e13da0fa 100644 --- a/src/log.rs +++ b/src/log.rs @@ -10,49 +10,49 @@ use std::{ path::Path, }; +use byond_fn::byond_fn; + thread_local! { static FILE_MAP: RefCell> = RefCell::new(HashMap::new()); } -byond_fn!(fn log_write(path, data, ...rest) { - FILE_MAP.with(|cell| -> Result<()> { +#[byond_fn] +fn log_write(path: &Path, data: &str, with_format: Option) -> Result<()> { + FILE_MAP.with(|cell| { // open file let mut map = cell.borrow_mut(); - let path = Path::new(path as &str); let file = match map.entry(path.into()) { Entry::Occupied(elem) => elem.into_mut(), Entry::Vacant(elem) => elem.insert(open(path)?), }; - if rest.first().map(|x| &**x) == Some("false") { - // Write the data to the file with no accoutrements. - write!(file, "{}", data)?; - } else { + if with_format.unwrap_or(true) { // write first line, timestamped let mut iter = data.split('\n'); if let Some(line) = iter.next() { - writeln!(file, "[{}] {}", Utc::now().format("%F %T%.3f"), line)?; + let time = Utc::now().format("%F %T%.3f"); + writeln!(file, "[{time}] {line}")?; } - // write remaining lines + // write the rest of the lines for line in iter { - writeln!(file, " - {}", line)?; + writeln!(file, "{line}")?; } + } else { + // Write the data to the file with no accoutrement's. + write!(file, "{data}")?; } - Ok(()) - }).err() -}); - -byond_fn!( - fn log_close_all() { - FILE_MAP.with(|cell| { - let mut map = cell.borrow_mut(); - map.clear(); - }); - Some("") - } -); + }) +} + +#[byond_fn] +fn log_close_all() { + FILE_MAP.with(|cell| { + let mut map = cell.borrow_mut(); + map.clear(); + }); +} fn open(path: &Path) -> Result { if let Some(parent) = path.parent() { diff --git a/src/time.rs b/src/time.rs index f27b9ddb..b3a8ad2d 100644 --- a/src/time.rs +++ b/src/time.rs @@ -1,49 +1,47 @@ +use std::time::{Duration, SystemTime, SystemTimeError}; use std::{ cell::RefCell, collections::hash_map::{Entry, HashMap}, time::Instant, }; +use byond_fn::byond_fn; + thread_local!( static INSTANTS: RefCell> = RefCell::new(HashMap::new()) ); -byond_fn!(fn time_microseconds(instant_id) { +fn time(instant_id: String) -> Duration { INSTANTS.with(|instants| { let mut map = instants.borrow_mut(); let instant = match map.entry(instant_id.into()) { Entry::Occupied(elem) => elem.into_mut(), Entry::Vacant(elem) => elem.insert(Instant::now()), }; - Some(instant.elapsed().as_micros().to_string()) + instant.elapsed() }) -}); +} -byond_fn!(fn time_milliseconds(instant_id) { - INSTANTS.with(|instants| { - let mut map = instants.borrow_mut(); - let instant = match map.entry(instant_id.into()) { - Entry::Occupied(elem) => elem.into_mut(), - Entry::Vacant(elem) => elem.insert(Instant::now()), - }; - Some(instant.elapsed().as_millis().to_string()) - }) -}); +#[byond_fn] +fn time_microseconds(instant_id: String) -> String { + time(instant_id).as_micros().to_string() +} + +#[byond_fn] +fn time_milliseconds(instant_id: String) -> String { + time(instant_id).as_millis().to_string() +} -byond_fn!(fn time_reset(instant_id) { +#[byond_fn] +fn time_reset(instant_id: String) { INSTANTS.with(|instants| { let mut map = instants.borrow_mut(); map.insert(instant_id.into(), Instant::now()); - Some("") }) -}); +} -byond_fn!( - fn unix_timestamp() { - Some( - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_secs_f64() - .to_string(), - ) - } -); +#[byond_fn] +fn unix_timestamp() -> Result { + SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|x| x.as_secs_f64()) + .map(|x| x.to_string()) +} diff --git a/src/url.rs b/src/url.rs index 177cbd40..0dc8c2bb 100644 --- a/src/url.rs +++ b/src/url.rs @@ -1,25 +1,24 @@ use crate::error::Result; +use byond_fn::byond_fn; use std::borrow::Cow; use url_dep::form_urlencoded::byte_serialize; -byond_fn!(fn url_encode(data) { - Some(encode(data)) -}); - byond_fn!(fn url_decode(data) { decode(data).ok() }); -fn encode(string: &str) -> String { +#[byond_fn] +fn url_encode(string: &str) -> String { byte_serialize(string.as_bytes()).collect() } -fn decode(string: &str) -> Result { +#[byond_fn] +fn url_decode(string: &str) -> String { let replaced = replace_plus(string.as_bytes()); // into_owned() is not strictly necessary here, but saves some refactoring work. - Ok(percent_encoding::percent_decode(&replaced) + percent_encoding::percent_decode(&replaced) .decode_utf8_lossy() - .into_owned()) + .into_owned() } // From `url` crate.