use std::fmt;
use bytes::Bytes;
use serde::de::DeserializeOwned;
use serde_json::Value;
use super::FunctionError;
use crate::placed::{self, Placed};
use crate::{Context, Limits};
pub struct FunctionRequest {
method: String,
args: Value,
bytes: Placed<Bytes>,
context: Context,
limits: Limits,
}
#[cfg(any(
feature = "boa",
feature = "quickjs",
feature = "v8",
feature = "fuzzing",
test
))]
impl FunctionRequest {
pub(crate) fn new(
method: String,
args: Value,
bytes: Placed<Bytes>,
context: Option<Context>,
limits: Limits,
) -> Self {
Self {
method,
args,
bytes,
context: context.unwrap_or_default(),
limits,
}
}
}
impl FunctionRequest {
pub fn method(&self) -> &str {
&self.method
}
pub fn context(&self) -> &Context {
&self.context
}
pub fn limits(&self) -> &Limits {
&self.limits
}
pub fn args<T: DeserializeOwned>(&self) -> Result<T, FunctionError> {
placed::deserialize(&self.args, &self.bytes)
.map_err(|error| FunctionError::arguments(error.to_string()))
}
pub fn raw_args(&self) -> &Value {
&self.args
}
}
impl fmt::Debug for FunctionRequest {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("FunctionRequest")
.field("method", &self.method)
.finish_non_exhaustive()
}
}