use super::codec::Encode;
use bytes::{BufMut, BytesMut};
use std::borrow::BorrowMut;
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct XmlSchema {
db_name: String,
owner: String,
collection: String,
}
impl XmlSchema {
pub(crate) fn new(
db_name: impl ToString,
owner: impl ToString,
collection: impl ToString,
) -> Self {
Self {
db_name: db_name.to_string(),
owner: owner.to_string(),
collection: collection.to_string(),
}
}
pub fn db_name(&self) -> &str {
&self.db_name
}
pub fn owner(&self) -> &str {
&self.owner
}
pub fn collection(&self) -> &str {
&self.collection
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct XmlData {
data: String,
schema: Option<Arc<XmlSchema>>,
}
impl XmlData {
pub fn new(data: impl ToString) -> Self {
Self {
data: data.to_string(),
schema: None,
}
}
pub(crate) fn set_schema(&mut self, schema: Arc<XmlSchema>) {
self.schema = Some(schema);
}
#[allow(clippy::option_as_ref_deref)]
pub fn schema(&self) -> Option<&XmlSchema> {
self.schema.as_ref().map(|s| &**s)
}
pub fn into_string(self) -> String {
self.data
}
}
impl ToString for XmlData {
fn to_string(&self) -> String {
self.data.to_string()
}
}
impl AsRef<str> for XmlData {
fn as_ref(&self) -> &str {
self.data.as_ref()
}
}
impl Encode<BytesMut> for XmlData {
fn encode(self, dst: &mut BytesMut) -> crate::Result<()> {
dst.put_u64_le(0xfffffffffffffffe_u64);
let mut length = 0u32;
let len_pos = dst.len();
dst.put_u32_le(length);
for chr in self.data.encode_utf16() {
length += 1;
dst.put_u16_le(chr);
}
dst.put_u32_le(0);
let dst: &mut [u8] = dst.borrow_mut();
let mut dst = &mut dst[len_pos..];
dst.put_u32_le(length * 2);
Ok(())
}
}