use std::fmt;
use bytes::Bytes;
use serde_json::Value;
use crate::host::{GuestEnd, pair};
use crate::placed::{self, Placed};
use crate::{ChannelEnd, Limits, Result};
pub struct FunctionResponse {
value: Value,
bytes: Placed<Bytes>,
channels: Placed<GuestEnd>,
}
impl FunctionResponse {
pub fn new(value: Value) -> Self {
Self {
value,
bytes: Vec::new(),
channels: Vec::new(),
}
}
pub fn bytes_at(&mut self, pointer: &str, bytes: impl Into<Bytes>) -> Result<&mut Self> {
let path = self.place(pointer)?;
self.bytes.push((path, bytes.into()));
Ok(self)
}
pub fn channel_at(&mut self, pointer: &str, limits: &Limits) -> Result<ChannelEnd> {
let path = self.place(pointer)?;
let (end, guest) = pair(limits);
self.channels.push((path, guest));
Ok(end)
}
fn place(&mut self, pointer: &str) -> Result<Vec<String>> {
let path = placed::parse(pointer)?;
let taken = self
.bytes
.iter()
.map(|(path, _)| path.as_slice())
.chain(self.channels.iter().map(|(path, _)| path.as_slice()));
if placed::overlaps(taken, &path) {
return Err(placed::invalid(
"the pointer overlaps another byte array or channel",
));
}
if !placed::clear(&mut self.value, &path) {
return Err(placed::invalid("the pointer names no value"));
}
Ok(path)
}
}
#[cfg(any(
feature = "boa",
feature = "quickjs",
feature = "v8",
feature = "fuzzing",
test
))]
impl FunctionResponse {
pub(crate) fn into_parts(self) -> (Value, Placed<Bytes>, Placed<GuestEnd>) {
(self.value, self.bytes, self.channels)
}
}
impl From<Value> for FunctionResponse {
fn from(value: Value) -> Self {
Self::new(value)
}
}
impl From<Bytes> for FunctionResponse {
fn from(bytes: Bytes) -> Self {
Self {
value: Value::Null,
bytes: vec![(Vec::new(), bytes)],
channels: Vec::new(),
}
}
}
impl From<Vec<u8>> for FunctionResponse {
fn from(bytes: Vec<u8>) -> Self {
Bytes::from(bytes).into()
}
}
impl fmt::Debug for FunctionResponse {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("FunctionResponse")
.field("value", &self.value)
.field(
"bytes",
&self.bytes.iter().map(|(path, _)| path).collect::<Vec<_>>(),
)
.field(
"channels",
&self
.channels
.iter()
.map(|(path, _)| path)
.collect::<Vec<_>>(),
)
.finish()
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
use crate::Error;
#[test]
fn resources_take_free_positions() {
let limits = Limits::default();
let mut response = FunctionResponse::new(json!({ "a": { "b": 1 }, "c": [1, 2] }));
response.bytes_at("/a", Bytes::from_static(b"x")).unwrap();
let _end = response.channel_at("/c/1", &limits).unwrap();
for pointer in ["", "/a", "/a/b", "/c", "/c/1", "/missing", "c"] {
assert!(
matches!(
response.bytes_at(pointer, Bytes::new()),
Err(Error::InvalidRequest(_))
),
"{pointer}"
);
}
let (value, bytes, channels) = response.into_parts();
assert_eq!(value, json!({ "a": null, "c": [1, null] }));
assert_eq!(bytes[0].0, vec!["a"]);
assert_eq!(channels[0].0, vec!["c", "1"]);
}
#[test]
fn bytes_can_be_the_whole_result() {
let (value, bytes, _) = FunctionResponse::from(b"raw".to_vec()).into_parts();
assert_eq!(value, Value::Null);
assert_eq!(bytes, vec![(Vec::new(), Bytes::from_static(b"raw"))]);
}
}