diff --git a/Cargo.lock b/Cargo.lock index f61f973..8749863 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,12 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "anyhow" version = "1.0.97" @@ -161,6 +167,14 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "dictionary" +version = "0.3.1" +dependencies = [ + "fastedge", + "proxy-wasm", +] + [[package]] name = "digest" version = "0.10.7" @@ -271,6 +285,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -414,7 +434,18 @@ version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" dependencies = [ - "foldhash", + "foldhash 0.1.5", +] + +[[package]] +name = "hashbrown" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5419bdc4f6a9207fbeba6d11b604d481addf78ecd10c11ad51e76c2f6482748d" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash 0.2.0", ] [[package]] @@ -613,7 +644,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3954d50fe15b02142bf25d3b8bdadb634ec3948f103d04ffe3031bc8fe9d7058" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.15.2", "serde", ] @@ -798,6 +829,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "proxy-wasm" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8d35d9e2bc5104e2e954b149aa1d5f9fa3bb27f73b45b2706020fed101db685" +dependencies = [ + "hashbrown 0.16.0", + "log", +] + [[package]] name = "pulldown-cmark" version = "0.11.3" @@ -1184,7 +1225,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c9d90bb93e764f6beabf1d02028c70a2156a6583e63ac4218dd07ef733368b0" dependencies = [ "bitflags 2.9.0", - "hashbrown", + "hashbrown 0.15.2", "indexmap", "semver", ] diff --git a/src/utils.rs b/src/helper.rs similarity index 100% rename from src/utils.rs rename to src/helper.rs diff --git a/src/lib.rs b/src/lib.rs index 326b408..13aca73 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,7 +34,7 @@ pub use http_client::send_request; pub use crate::exports::gcore::fastedge::http_handler; use crate::gcore::fastedge::http::{Error as HttpError, Method, Request, Response}; -mod utils; +mod helper; /// Implementation of Outbound HTTP component mod http_client; @@ -81,6 +81,12 @@ pub mod key_value { pub use crate::gcore::fastedge::key_value::Error; } +/// FastEdge-specific utility functions for diagnostics and statistics. +pub mod utils { + #[doc(inline)] + pub use crate::gcore::fastedge::utils::set_user_diag; +} + /// Error type returned by [`send_request`] #[derive(thiserror::Error, Debug)] pub enum Error { diff --git a/src/proxywasm/key_value.rs b/src/proxywasm/key_value.rs index 37383cb..ee3a544 100644 --- a/src/proxywasm/key_value.rs +++ b/src/proxywasm/key_value.rs @@ -48,7 +48,9 @@ //! } //! ``` //! -use crate::utils; + +use std::fmt::Display; +use crate::helper; use std::ptr::null_mut; /// The set of errors which may be raised by functions in this interface @@ -67,6 +69,16 @@ pub struct Store { handle: u32, } +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::NoSuchStore => write!(f, "no such store"), + Error::AccessDenied => write!(f, "access denied"), + Error::Other(msg) => write!(f, "other error: {}", msg), + } + } +} + impl Store { /// Open the default store. pub fn new() -> Result { @@ -144,7 +156,7 @@ impl Store { if !return_data.is_null() { let data = Vec::from_raw_parts(return_data, return_size, return_size); - let data: Vec<(Vec, f64)> = utils::deserialize_list(&data) + let data: Vec<(Vec, f64)> = helper::deserialize_list(&data) .into_iter() .map(|v| { let mut value = v.to_vec(); @@ -193,7 +205,7 @@ impl Store { if !return_data.is_null() { let data = Vec::from_raw_parts(return_data, return_size, return_size); - let data: Vec = utils::deserialize_list(&data) + let data: Vec = helper::deserialize_list(&data) .into_iter() .map(|v| String::from_utf8_lossy(v).to_string()) .collect(); @@ -228,7 +240,7 @@ impl Store { if !return_data.is_null() { let data = Vec::from_raw_parts(return_data, return_size, return_size); - let data: Vec<(Vec, f64)> = utils::deserialize_list(&data) + let data: Vec<(Vec, f64)> = helper::deserialize_list(&data) .into_iter() .map(|v| { let mut value = v.to_vec(); diff --git a/src/proxywasm/mod.rs b/src/proxywasm/mod.rs index df958ad..ed6fb01 100644 --- a/src/proxywasm/mod.rs +++ b/src/proxywasm/mod.rs @@ -1,6 +1,7 @@ pub mod key_value; pub mod secret; pub mod dictionary; +pub mod utils; extern "C" { fn proxy_secret_get( @@ -71,4 +72,9 @@ extern "C" { item_size: usize, return_handle: *mut u32, ) -> u32; + + fn stats_set_user_diag( + value_data: *const u8, + value_size: usize, + ) -> u32; } diff --git a/src/proxywasm/utils.rs b/src/proxywasm/utils.rs new file mode 100644 index 0000000..0dc7c54 --- /dev/null +++ b/src/proxywasm/utils.rs @@ -0,0 +1,12 @@ +//! This module provides an interface for FastEdge specific handlers, such as setting user diagnostics. +//! + +/// Save statistics user diagnostic message. +pub fn set_user_diag(value: &str) { + unsafe { + let status = super::stats_set_user_diag(value.as_ptr(), value.len()); + if status != 0 { + panic!("unexpected status: {}", status) + } + } +} diff --git a/wit b/wit index 561aa99..9a18af2 160000 --- a/wit +++ b/wit @@ -1 +1 @@ -Subproject commit 561aa99135425fb2a7a01feb989614fcbd083a50 +Subproject commit 9a18af2195424b94f4473d48e0f5e9c000c161e4