use alloc::string::String;
use core::ops::{Deref, DerefMut};
use serde::ser::{Serialize, Serializer};
use super::{Json, TOOL_OUTPUT_QUARANTINE_TAG, ToolOutput};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Default,
serde::Deserialize,
schemars::JsonSchema,
)]
#[serde(transparent)]
pub struct Sanitized<T>(pub T);
impl<T> Sanitized<T> {
#[must_use]
pub const fn new(value: T) -> Self {
Self(value)
}
#[must_use]
pub fn into_inner(self) -> T {
self.0
}
#[must_use]
pub const fn as_inner(&self) -> &T {
&self.0
}
}
impl<T> Deref for Sanitized<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for Sanitized<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> From<T> for Sanitized<T> {
fn from(value: T) -> Self {
Self(value)
}
}
impl From<Sanitized<Self>> for ToolOutput {
fn from(value: Sanitized<Self>) -> Self {
value.0.sanitized()
}
}
impl From<Sanitized<String>> for ToolOutput {
fn from(value: Sanitized<String>) -> Self {
Self::new(value.0).sanitized()
}
}
impl From<Sanitized<&str>> for ToolOutput {
fn from(value: Sanitized<&str>) -> Self {
Self::new(value.0).sanitized()
}
}
impl<T: Serialize> From<Sanitized<Json<T>>> for ToolOutput {
fn from(value: Sanitized<Json<T>>) -> Self {
Self::from(Json(Sanitized(value.0.0))).sanitized()
}
}
impl<T: Serialize> Serialize for Sanitized<T> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
md_tmpl_core::serialize_with_sanitization(
&self.0,
serializer,
md_tmpl_core::StringSanitizeMode::SanitizeUntrusted {
tag_spec: TOOL_OUTPUT_QUARANTINE_TAG,
},
)
}
}
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Default,
serde::Deserialize,
schemars::JsonSchema,
)]
#[serde(transparent)]
pub struct Untrusted<T>(pub T);
pub type Quarantined<T> = Untrusted<T>;
impl<T> Untrusted<T> {
#[must_use]
pub const fn new(value: T) -> Self {
Self(value)
}
#[must_use]
pub fn into_inner(self) -> T {
self.0
}
#[must_use]
pub const fn as_inner(&self) -> &T {
&self.0
}
}
impl<T> Deref for Untrusted<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for Untrusted<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> From<T> for Untrusted<T> {
fn from(value: T) -> Self {
Self(value)
}
}
impl From<Untrusted<Self>> for ToolOutput {
fn from(value: Untrusted<Self>) -> Self {
value.0.quarantined()
}
}
impl From<Untrusted<String>> for ToolOutput {
fn from(value: Untrusted<String>) -> Self {
Self::new(value.0).quarantined()
}
}
impl From<Untrusted<&str>> for ToolOutput {
fn from(value: Untrusted<&str>) -> Self {
Self::new(value.0).quarantined()
}
}
impl<T: Serialize> From<Untrusted<Json<T>>> for ToolOutput {
fn from(value: Untrusted<Json<T>>) -> Self {
Self::from(value.0).quarantined()
}
}
impl<T: Serialize> Serialize for Untrusted<T> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
md_tmpl_core::serialize_with_sanitization(
&self.0,
serializer,
md_tmpl_core::StringSanitizeMode::Quarantine {
tag_spec: TOOL_OUTPUT_QUARANTINE_TAG,
},
)
}
}