msldap-ext 0.1.0-dev

Encoder/decoder for MS-ADTS LDAP extension controls (Paged Results, ExtendedDN, SDFlags, ShowDeleted, DirSync, VLV). Pure BER — no transport.
Documentation
//! Virtual List View (VLV) request — 2.16.840.1.113730.3.4.9 (draft-ietf-ldapext-ldapv3-vlv-09).
//!
//! ```asn1
//! VirtualListViewRequest ::= SEQUENCE {
//!     beforeCount    INTEGER (0..maxInt),
//!     afterCount     INTEGER (0..maxInt),
//!     target CHOICE {
//!         byOffset        [0] SEQUENCE {
//!                              offset          INTEGER,
//!                              contentCount    INTEGER },
//!         greaterThanOrEqual [1] AssertionValue },
//!     contextID     OCTET STRING OPTIONAL
//! }
//! ```
//!
//! We currently implement the `byOffset` variant only — the mode servers
//! actually use for AD paging. `greaterThanOrEqual` returns
//! `Error::Unimplemented` from `encode_gte` (see stubs section in README).

use crate::ber;

pub const OID: &str = "2.16.840.1.113730.3.4.9";
pub const OID_RESPONSE: &str = "2.16.840.1.113730.3.4.10";

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VirtualListView {
    pub before: u32,
    pub after: u32,
    pub offset: u32,
    pub content_count: u32,
    pub context_id: Option<Vec<u8>>,
}

impl VirtualListView {
    pub fn by_offset(before: u32, after: u32, offset: u32, content_count: u32) -> Self {
        Self {
            before,
            after,
            offset,
            content_count,
            context_id: None,
        }
    }

    pub fn encode(&self) -> Vec<u8> {
        // Inner SEQUENCE for the byOffset target, then wrap with context tag [0].
        let mut target_body = Vec::with_capacity(12);
        ber::write_u32(&mut target_body, self.offset);
        ber::write_u32(&mut target_body, self.content_count);

        let mut body = Vec::with_capacity(24);
        ber::write_u32(&mut body, self.before);
        ber::write_u32(&mut body, self.after);
        // Context-specific constructed [0] — tag 0xA0.
        body.push(0xA0);
        ber::write_len(&mut body, target_body.len());
        body.extend_from_slice(&target_body);

        if let Some(ctx) = &self.context_id {
            ber::write_octet_string(&mut body, ctx);
        }
        ber::wrap_sequence(&body)
    }
}