use crate::std::{borrow::Cow, string::String, vec::Vec};
use rama_core::bytes::{Bytes, BytesMut};
mod sealed {
use crate::std::borrow::Cow;
use rama_core::bytes::BytesMut;
pub trait Sealed {
fn as_uri_component_bytes(&self) -> Cow<'_, [u8]>;
fn into_uri_component_bytes_mut(self) -> BytesMut;
fn is_already_uri_component(&self) -> bool {
false
}
}
}
pub trait IntoUriComponent: sealed::Sealed {}
impl sealed::Sealed for BytesMut {
fn as_uri_component_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self)
}
fn into_uri_component_bytes_mut(self) -> BytesMut {
self
}
}
impl IntoUriComponent for BytesMut {}
impl sealed::Sealed for Bytes {
fn as_uri_component_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self)
}
fn into_uri_component_bytes_mut(self) -> BytesMut {
BytesMut::from(self)
}
}
impl IntoUriComponent for Bytes {}
impl sealed::Sealed for String {
fn as_uri_component_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self.as_bytes())
}
fn into_uri_component_bytes_mut(self) -> BytesMut {
BytesMut::from(Bytes::from(self.into_bytes()))
}
}
impl IntoUriComponent for String {}
impl sealed::Sealed for Vec<u8> {
fn as_uri_component_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self)
}
fn into_uri_component_bytes_mut(self) -> BytesMut {
BytesMut::from(Bytes::from(self))
}
}
impl IntoUriComponent for Vec<u8> {}
impl sealed::Sealed for &str {
fn as_uri_component_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self.as_bytes())
}
fn into_uri_component_bytes_mut(self) -> BytesMut {
BytesMut::from(self.as_bytes())
}
}
impl IntoUriComponent for &str {}
impl sealed::Sealed for &[u8] {
fn as_uri_component_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self)
}
fn into_uri_component_bytes_mut(self) -> BytesMut {
BytesMut::from(self)
}
}
impl IntoUriComponent for &[u8] {}
macro_rules! impl_integer {
($($ty:ty),+ $(,)?) => {
$(
impl sealed::Sealed for $ty {
fn as_uri_component_bytes(&self) -> Cow<'_, [u8]> {
let mut buf = itoa::Buffer::new();
Cow::Owned(buf.format(*self).as_bytes().to_vec())
}
fn into_uri_component_bytes_mut(self) -> BytesMut {
let mut buf = itoa::Buffer::new();
BytesMut::from(buf.format(self).as_bytes())
}
}
impl IntoUriComponent for $ty {}
)+
};
}
impl_integer!(
i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize
);
impl sealed::Sealed for super::path::PathRef<'_> {
fn as_uri_component_bytes(&self) -> Cow<'_, [u8]> {
match self.as_encoded_str() {
Cow::Borrowed(s) => Cow::Borrowed(s.as_bytes()),
Cow::Owned(s) => Cow::Owned(s.into_bytes()),
}
}
fn into_uri_component_bytes_mut(self) -> BytesMut {
let mut bytes = BytesMut::with_capacity(self.bytes.len());
self.write_encoded_to(&mut bytes);
bytes
}
fn is_already_uri_component(&self) -> bool {
true
}
}
impl IntoUriComponent for super::path::PathRef<'_> {}
impl sealed::Sealed for super::path::PathSegment<'_> {
fn as_uri_component_bytes(&self) -> Cow<'_, [u8]> {
match self.as_encoded_str() {
Cow::Borrowed(s) => Cow::Borrowed(s.as_bytes()),
Cow::Owned(s) => Cow::Owned(s.into_bytes()),
}
}
fn into_uri_component_bytes_mut(self) -> BytesMut {
let mut bytes = BytesMut::with_capacity(self.encoded_capacity_hint());
self.write_encoded_to(&mut bytes);
bytes
}
fn is_already_uri_component(&self) -> bool {
true
}
}
impl IntoUriComponent for super::path::PathSegment<'_> {}
impl sealed::Sealed for bool {
fn as_uri_component_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(if *self { b"true" } else { b"false" })
}
fn into_uri_component_bytes_mut(self) -> BytesMut {
BytesMut::from(if self { &b"true"[..] } else { &b"false"[..] })
}
}
impl IntoUriComponent for bool {}