use std::sync::Arc;
use crate::error::Error;
use crate::index::IndexedDocument;
use crate::typed_doc::{JsonDoc, TypedDoc, TypedDocRef};
use crate::view::JsonView;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SharedDocument {
data: Arc<[u8]>,
}
impl SharedDocument {
#[inline]
pub fn from_slice(slice: &[u8]) -> Self {
Self {
data: Arc::from(slice),
}
}
#[inline]
pub fn from_vec(vec: Vec<u8>) -> Self {
Self {
data: Arc::from(vec),
}
}
#[inline]
pub fn as_bytes(&self) -> &[u8] {
&self.data
}
#[inline]
pub fn len(&self) -> usize {
self.data.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
#[inline]
pub fn arc(&self) -> Arc<[u8]> {
Arc::clone(&self.data)
}
#[inline]
pub fn into_arc(self) -> Arc<[u8]> {
self.data
}
#[inline]
pub fn to_vec(&self) -> Vec<u8> {
self.data.to_vec()
}
#[inline]
pub fn read<T: JsonView>(&self) -> Result<T, Error> {
T::read_from(self.as_bytes())
}
#[inline]
pub fn read_indexed<T: JsonView>(&self) -> Result<T, Error> {
T::read_from_indexed(self.as_bytes())
}
#[inline]
pub fn indexed(&self, array_paths: &[&str]) -> Result<IndexedDocument<'_>, Error> {
IndexedDocument::build(self.as_bytes(), array_paths)
}
#[inline]
pub fn indexed_full(
&self,
array_paths: &[&str],
object_paths: &[&str],
) -> Result<IndexedDocument<'_>, Error> {
IndexedDocument::build_full(self.as_bytes(), array_paths, object_paths)
}
#[inline]
pub fn typed_ref(&self) -> TypedDocRef<'_> {
TypedDocRef::from_slice(self.as_bytes())
}
#[inline]
pub fn to_typed_doc(&self) -> TypedDoc {
TypedDoc::from_slice(self.as_bytes())
}
}
impl JsonDoc for SharedDocument {
#[inline]
fn as_json_bytes(&self) -> &[u8] {
self.as_bytes()
}
}
impl AsRef<[u8]> for SharedDocument {
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl From<Vec<u8>> for SharedDocument {
#[inline]
fn from(vec: Vec<u8>) -> Self {
Self::from_vec(vec)
}
}
impl From<&[u8]> for SharedDocument {
#[inline]
fn from(slice: &[u8]) -> Self {
Self::from_slice(slice)
}
}
impl From<Arc<[u8]>> for SharedDocument {
#[inline]
fn from(data: Arc<[u8]>) -> Self {
Self { data }
}
}