mirror of
https://github.com/lldap/lldap.git
synced 2026-07-25 17:58:44 +00:00
server: Simplify RequestFilter's TryInto
This commit is contained in:
committed by
nitnelave
parent
af8277dbbd
commit
1fddd87470
@@ -41,70 +41,51 @@ pub struct RequestFilter {
|
|||||||
impl TryInto<DomainRequestFilter> for RequestFilter {
|
impl TryInto<DomainRequestFilter> for RequestFilter {
|
||||||
type Error = String;
|
type Error = String;
|
||||||
fn try_into(self) -> Result<DomainRequestFilter, Self::Error> {
|
fn try_into(self) -> Result<DomainRequestFilter, Self::Error> {
|
||||||
let mut field_count = 0;
|
match (
|
||||||
if self.any.is_some() {
|
self.eq,
|
||||||
field_count += 1;
|
self.any,
|
||||||
}
|
self.all,
|
||||||
if self.all.is_some() {
|
self.not,
|
||||||
field_count += 1;
|
self.member_of,
|
||||||
}
|
self.member_of_id,
|
||||||
if self.not.is_some() {
|
) {
|
||||||
field_count += 1;
|
(Some(eq), None, None, None, None, None) => {
|
||||||
}
|
match map_user_field(&eq.field.to_ascii_lowercase()) {
|
||||||
if self.eq.is_some() {
|
UserFieldType::NoMatch => Err(format!("Unknown request filter: {}", &eq.field)),
|
||||||
field_count += 1;
|
UserFieldType::PrimaryField(UserColumn::UserId) => {
|
||||||
}
|
Ok(DomainRequestFilter::UserId(UserId::new(&eq.value)))
|
||||||
if self.member_of.is_some() {
|
}
|
||||||
field_count += 1;
|
UserFieldType::PrimaryField(column) => {
|
||||||
}
|
Ok(DomainRequestFilter::Equality(column, eq.value))
|
||||||
if self.member_of_id.is_some() {
|
}
|
||||||
field_count += 1;
|
UserFieldType::Attribute(column) => Ok(DomainRequestFilter::AttributeEquality(
|
||||||
}
|
column.to_owned(),
|
||||||
if field_count == 0 {
|
eq.value,
|
||||||
return Err("No field specified in request filter".to_string());
|
)),
|
||||||
}
|
|
||||||
if field_count > 1 {
|
|
||||||
return Err("Multiple fields specified in request filter".to_string());
|
|
||||||
}
|
|
||||||
if let Some(e) = self.eq {
|
|
||||||
return match map_user_field(&e.field.to_ascii_lowercase()) {
|
|
||||||
UserFieldType::NoMatch => Err(format!("Unknown request filter: {}", &e.field)),
|
|
||||||
UserFieldType::PrimaryField(UserColumn::UserId) => {
|
|
||||||
Ok(DomainRequestFilter::UserId(UserId::new(&e.value)))
|
|
||||||
}
|
}
|
||||||
UserFieldType::PrimaryField(column) => {
|
}
|
||||||
Ok(DomainRequestFilter::Equality(column, e.value))
|
(None, Some(any), None, None, None, None) => Ok(DomainRequestFilter::Or(
|
||||||
}
|
any.into_iter()
|
||||||
UserFieldType::Attribute(column) => Ok(DomainRequestFilter::AttributeEquality(
|
|
||||||
column.to_owned(),
|
|
||||||
e.value,
|
|
||||||
)),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if let Some(c) = self.any {
|
|
||||||
return Ok(DomainRequestFilter::Or(
|
|
||||||
c.into_iter()
|
|
||||||
.map(TryInto::try_into)
|
.map(TryInto::try_into)
|
||||||
.collect::<Result<Vec<_>, String>>()?,
|
.collect::<Result<Vec<_>, String>>()?,
|
||||||
));
|
)),
|
||||||
}
|
(None, None, Some(all), None, None, None) => Ok(DomainRequestFilter::And(
|
||||||
if let Some(c) = self.all {
|
all.into_iter()
|
||||||
return Ok(DomainRequestFilter::And(
|
|
||||||
c.into_iter()
|
|
||||||
.map(TryInto::try_into)
|
.map(TryInto::try_into)
|
||||||
.collect::<Result<Vec<_>, String>>()?,
|
.collect::<Result<Vec<_>, String>>()?,
|
||||||
));
|
)),
|
||||||
|
(None, None, None, Some(not), None, None) => {
|
||||||
|
Ok(DomainRequestFilter::Not(Box::new((*not).try_into()?)))
|
||||||
|
}
|
||||||
|
(None, None, None, None, Some(group), None) => Ok(DomainRequestFilter::MemberOf(group)),
|
||||||
|
(None, None, None, None, None, Some(group_id)) => {
|
||||||
|
Ok(DomainRequestFilter::MemberOfId(GroupId(group_id)))
|
||||||
|
}
|
||||||
|
(None, None, None, None, None, None) => {
|
||||||
|
Err("No field specified in request filter".to_string())
|
||||||
|
}
|
||||||
|
_ => Err("Multiple fields specified in request filter".to_string()),
|
||||||
}
|
}
|
||||||
if let Some(c) = self.not {
|
|
||||||
return Ok(DomainRequestFilter::Not(Box::new((*c).try_into()?)));
|
|
||||||
}
|
|
||||||
if let Some(group) = self.member_of {
|
|
||||||
return Ok(DomainRequestFilter::MemberOf(group));
|
|
||||||
}
|
|
||||||
if let Some(group_id) = self.member_of_id {
|
|
||||||
return Ok(DomainRequestFilter::MemberOfId(GroupId(group_id)));
|
|
||||||
}
|
|
||||||
unreachable!();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user