use untrusted_value_derive_internals::{SanitizeValue, SanitizeWith};
#[repr(transparent)]
pub struct UntrustedValue<Insecure> {
value: Insecure,
}
impl<Insecure> UntrustedValue<Insecure> {
pub fn use_untrusted_value(self) -> Insecure {
self.value
}
pub fn wrap(value: Insecure) -> Self {
UntrustedValue { value }
}
}
impl<Insecure, Trusted> SanitizeWith<Insecure, Trusted> for UntrustedValue<Insecure> {
fn sanitize_with<Sanitizer, Error>(self, sanitizer: Sanitizer) -> Result<Trusted, Error>
where
Sanitizer: FnOnce(Insecure) -> Result<Trusted, Error>,
{
sanitizer(self.value)
}
}
impl<Insecure> From<Insecure> for UntrustedValue<Insecure> {
fn from(value: Insecure) -> Self {
UntrustedValue::wrap(value)
}
}
#[allow(clippy::expl_impl_clone_on_copy)]
impl<Insecure: Clone> Clone for UntrustedValue<Insecure> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
}
}
}
impl<Insecure: Copy> Copy for UntrustedValue<Insecure> {}
impl<Sanitized, E, Insecure: SanitizeValue<Sanitized, Error = E>> SanitizeValue<Sanitized>
for UntrustedValue<Insecure>
{
type Error = E;
fn sanitize_value(self) -> Result<Sanitized, Self::Error> {
self.value.sanitize_value()
}
}