use alloc::borrow::Cow;
use core::ops::Deref;
use crate::de::{DecodeSlot, FormatDecoder, NsonDeserialize};
use crate::error::{Error, Result};
use crate::schema::{NsonSchema, TypeSchema};
use crate::ser::{FormatEncoder, NsonSerialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Bytes<'a>(pub &'a [u8]);
impl<'a> Bytes<'a> {
pub fn as_bytes(&self) -> &'a [u8] {
self.0
}
}
impl<'a> From<&'a [u8]> for Bytes<'a> {
fn from(bytes: &'a [u8]) -> Self {
Bytes(bytes)
}
}
impl<'a> From<&'a str> for Bytes<'a> {
fn from(text: &'a str) -> Self {
Bytes(text.as_bytes())
}
}
impl<'a> Deref for Bytes<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.0
}
}
impl<'a> AsRef<[u8]> for Bytes<'a> {
fn as_ref(&self) -> &[u8] {
self.0
}
}
impl NsonSchema for Bytes<'_> {
const SCHEMA: TypeSchema = TypeSchema::Bytes;
}
impl NsonSerialize for Bytes<'_> {
fn nextencode<E: FormatEncoder>(&self, encoder: &mut E) -> Result<(), E::Error> {
encoder.write_bytes(self.0)
}
}
impl<'de, 'a> NsonDeserialize<'de> for Bytes<'a>
where
'de: 'a,
{
fn nextdecode_into<D: FormatDecoder<'de>>(
decoder: &mut D,
out: &mut DecodeSlot<Self>,
) -> Result<(), D::Error> {
match decoder.bytes()? {
Cow::Borrowed(b) => {
out.write(Bytes(b));
Ok(())
}
Cow::Owned(_) => Err(Error::invalid_type(
"a borrowed byte string (no escape sequences)",
"bytes",
)
.into()),
}
}
}