Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added constructors for non-exhaustive LDAP structs #49

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 167 additions & 2 deletions standards/ldap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ pub struct LdapMessage {
pub controls: Option<Controls>,
}

impl LdapMessage {
/// LdapMessage constructor
pub fn new(message_id: MessageId, protocol_op: ProtocolOp) -> Self {
LdapMessage {
message_id,
protocol_op,
controls: None,
}
}
}

/// The kind of operation in the [`LdapMessage`].
#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(choice)]
Expand Down Expand Up @@ -114,20 +125,44 @@ pub struct AttributeValueAssertion {
pub assertion_value: AssertionValue,
}

impl AttributeValueAssertion {
/// AttributeValueAssertion constructor
pub fn new(attribute_desc: AttributeDescription, assertion_value: AssertionValue) -> Self {
Self {
attribute_desc,
assertion_value,
}
}
}

#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[non_exhaustive]
pub struct PartialAttribute {
r#type: AttributeDescription,
pub r#type: AttributeDescription,
pub vals: SetOf<AttributeValue>,
}

impl PartialAttribute {
/// PartialAttribute constructor
pub fn new(r#type: AttributeDescription, vals: SetOf<AttributeValue>) -> Self {
Self { r#type, vals }
}
}

#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[non_exhaustive]
pub struct Attribute {
r#type: AttributeDescription,
pub r#type: AttributeDescription,
pub vals: SetOf<AttributeValue>,
}

impl Attribute {
/// Attribute constructor
pub fn new(r#type: AttributeDescription, vals: SetOf<AttributeValue>) -> Self {
Self { r#type, vals }
}
}

/// The envelope for the result of any operation.
#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[non_exhaustive]
Expand All @@ -140,6 +175,22 @@ pub struct LdapResult {
pub referral: Option<Referral>,
}

impl LdapResult {
/// LdapResult constructor
pub fn new(
result_code: ResultCode,
matched_dn: LdapDn,
diagnostic_message: LdapString,
) -> Self {
Self {
result_code,
matched_dn,
diagnostic_message,
referral: None,
}
}
}

#[derive(AsnType, Encode, Decode, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(enumerated)]
#[non_exhaustive]
Expand Down Expand Up @@ -194,6 +245,21 @@ pub struct Control {
pub control_value: Option<OctetString>,
}

impl Control {
/// Control constructor
pub fn new(
control_type: LdapOid,
criticality: bool,
control_value: Option<OctetString>,
) -> Self {
Self {
control_type,
criticality,
control_value,
}
}
}

/// Allow authentication information to be exchanged between the client
/// and server.
///
Expand Down Expand Up @@ -221,6 +287,17 @@ pub struct BindRequest {
pub authentication: AuthenticationChoice,
}

impl BindRequest {
/// BindRequest constructor
pub fn new(version: u8, name: LdapDn, authentication: AuthenticationChoice) -> Self {
Self {
version,
name,
authentication,
}
}
}

/// Information used in authentication.
///
/// This type is extensible. Servers that do not support a choice supplied
Expand All @@ -244,6 +321,16 @@ pub struct SaslCredentials {
pub credentials: Option<OctetString>,
}

impl SaslCredentials {
/// SaslCredentials constructor
pub fn new(mechanism: LdapString, credentials: Option<OctetString>) -> Self {
Self {
mechanism,
credentials,
}
}
}

/// An indication from the server of the status of the client's request
/// for authentication.
///
Expand All @@ -261,6 +348,25 @@ pub struct BindResponse {
pub server_sasl_creds: Option<OctetString>,
}

impl BindResponse {
/// BindResponse constructor
pub fn new(
result_code: ResultCode,
matched_dn: LdapDn,
diagnostic_message: LdapString,
referral: Option<Referral>,
server_sasl_creds: Option<OctetString>,
) -> Self {
Self {
result_code,
matched_dn,
diagnostic_message,
referral,
server_sasl_creds,
}
}
}

/// Request to terminate an LDAP session.
///
/// The Unbind operation is not the antithesis of the Bind operation as the name
Expand Down Expand Up @@ -316,6 +422,31 @@ pub struct SearchRequest {
pub attributes: AttributeSelection,
}

impl SearchRequest {
/// SearchRequest constructor
pub fn new(
base_object: LdapDn,
scope: SearchRequestScope,
deref_aliases: SearchRequestDerefAliases,
size_limit: u32,
time_limit: u32,
types_only: bool,
filter: Filter,
attributes: AttributeSelection,
) -> Self {
Self {
base_object,
scope,
deref_aliases,
size_limit,
time_limit,
types_only,
filter,
attributes,
}
}
}

/// The scope of the search to be performed.
#[derive(AsnType, Encode, Decode, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(enumerated)]
Expand Down Expand Up @@ -437,6 +568,13 @@ pub struct SubstringFilter {
pub substrings: SequenceOf<SubstringChoice>,
}

impl SubstringFilter {
/// SubstringFilter constructor
pub fn new(r#type: AttributeDescription, substrings: SequenceOf<SubstringChoice>) -> Self {
Self { r#type, substrings }
}
}

/// Which part of the substring to match against.
#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(choice)]
Expand Down Expand Up @@ -488,6 +626,23 @@ pub struct MatchingRuleAssertion {
pub dn_attributes: bool,
}

impl MatchingRuleAssertion {
/// MatchingRuleAssertion constructor
pub fn new(
matching_rule: Option<MatchingRuleId>,
r#type: Option<AttributeDescription>,
match_value: AssertionValue,
dn_attributes: bool,
) -> Self {
Self {
matching_rule,
r#type,
match_value,
dn_attributes,
}
}
}

/// An entry found during the search.
#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(tag(application, 4))]
Expand All @@ -499,6 +654,16 @@ pub struct SearchResultEntry {
pub attributes: PartialAttributeList,
}

impl SearchResultEntry {
/// SearchResultEntry constructor
pub fn new(object_name: LdapDn, attributes: PartialAttributeList) -> Self {
Self {
object_name,
attributes,
}
}
}

/// Reference of servers containing the data required to continue the search.
#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(tag(application, 19))]
Expand Down