use ::safer_ffi::prelude::*;
use ::serde::ser::Serialize;
use crate::{error::DittoError, utils::zstr::ZString};
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Query {
pub(crate) inner_string: ZString,
}
impl TryFrom<&str> for Query {
type Error = DittoError;
fn try_from(query: &str) -> Result<Query, DittoError> {
Self::try_from(query.to_string())
}
}
impl TryFrom<String> for Query {
type Error = DittoError;
fn try_from(query: String) -> Result<Query, Self::Error> {
Ok(Self::new(query))
}
}
impl Query {
fn new(query_str: String) -> Self {
Self {
inner_string: char_p::new(query_str).into(),
}
}
pub(crate) fn prepare_ffi(&self) -> char_p::Ref<'_> {
(&*self.inner_string).into()
}
}
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct QueryArguments {
cbor: Vec<u8>,
}
impl<T: Serialize> From<T> for QueryArguments {
fn from(query_args: T) -> QueryArguments {
let cbor = serde_cbor::to_vec(&query_args).unwrap();
Self { cbor }
}
}
impl QueryArguments {
pub(crate) fn cbor(&self) -> &[u8] {
&self.cbor
}
}