use std::collections::BTreeMap;
use crate::vrl::expressions::{FromVrlValue, ToVrlValue, VrlValue};
use bytes::Bytes;
use http::HeaderValue;
use crate::executor::headers::{
request::RequestExpressionContext, response::ResponseExpressionContext,
};
pub fn vrl_value_to_header_value(value: VrlValue) -> Option<HeaderValue> {
HeaderValue::from_vrl_value(value).ok()
}
impl From<&RequestExpressionContext<'_>> for VrlValue {
fn from(ctx: &RequestExpressionContext) -> Self {
let subgraph_value = {
let value = Self::Bytes(Bytes::from(ctx.subgraph_name.to_owned()));
Self::Object(BTreeMap::from([("name".into(), value)]))
};
let request_value: Self = ctx.client_request.into();
Self::Object(BTreeMap::from([
("subgraph".into(), subgraph_value),
("request".into(), request_value),
]))
}
}
impl From<&ResponseExpressionContext<'_>> for VrlValue {
fn from(ctx: &ResponseExpressionContext) -> Self {
let subgraph_value = Self::Object(BTreeMap::from([(
"name".into(),
Self::Bytes(Bytes::from(ctx.subgraph_name.to_owned())),
)]));
let response_value = Self::Object(BTreeMap::from([(
"headers".into(),
ctx.subgraph_headers.to_vrl_value(),
)]));
let request_value: Self = ctx.client_request.into();
Self::Object(BTreeMap::from([
("subgraph".into(), subgraph_value),
("response".into(), response_value),
("request".into(), request_value),
]))
}
}