use std::convert::TryFrom;
use rasn::{ber, types::*, Decode, Decoder, Encode};
use rasn_ldap::Control;
use crate::error::Error;
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct SimplePagedResultsControl {
size: Integer,
cookie: OctetString,
has_entries: bool,
}
impl SimplePagedResultsControl {
pub const OID: &'static [u8] = crate::oid::SIMPLE_PAGED_RESULTS_CONTROL_OID;
pub fn new(size: u32) -> Self {
Self {
size: size.into(),
cookie: OctetString::default(),
has_entries: true,
}
}
pub fn with_size(self, size: u32) -> Self {
Self {
size: size.into(),
..self
}
}
pub fn cookie(&self) -> &OctetString {
&self.cookie
}
pub fn size(&self) -> &Integer {
&self.size
}
pub fn has_entries(&self) -> bool {
self.has_entries
}
}
#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
struct RealSearchControlValue {
size: Integer,
cookie: OctetString,
}
impl TryFrom<SimplePagedResultsControl> for Control {
type Error = Error;
fn try_from(control: SimplePagedResultsControl) -> Result<Self, Self::Error> {
let value = RealSearchControlValue {
size: control.size,
cookie: control.cookie,
};
Ok(Control::new(
SimplePagedResultsControl::OID.into(),
false,
Some(ber::encode(&value)?.into()),
))
}
}
impl TryFrom<Control> for SimplePagedResultsControl {
type Error = Error;
fn try_from(value: Control) -> Result<Self, Self::Error> {
let value = ber::decode::<RealSearchControlValue>(value.control_value.as_deref().unwrap_or(b""))?;
let has_entries = !value.cookie.is_empty();
Ok(SimplePagedResultsControl {
size: value.size,
cookie: value.cookie,
has_entries,
})
}
}