clean up code and make to_parameters use AsRef<str>

Co-authored-by: confused-ace-noises <confused-ace-noises@users.noreply.github.com>
This commit is contained in:
askiiart 2024-12-05 13:11:52 -06:00
parent 62822233bf
commit fcef148285
Signed by untrusted user who does not match committer: askiiart
GPG key ID: EA85979611654C30

View file

@ -4,6 +4,7 @@ use rocket::http::Status;
use rocket::response::status; use rocket::response::status;
use rocket::{get, response::content::RawXml}; use rocket::{get, response::content::RawXml};
use rocket::{FromForm, State}; use rocket::{FromForm, State};
use std::borrow::Borrow;
use std::str; use std::str;
use xml::writer::{EmitterConfig, XmlEvent}; use xml::writer::{EmitterConfig, XmlEvent};
@ -32,38 +33,38 @@ struct SearchForm {
impl SearchForm { impl SearchForm {
/// Converts it to a SearchParameters object /// Converts it to a SearchParameters object
fn to_parameters(&self, conf: Config, search_type: &str) -> SearchParameters { fn to_parameters(
let mut categories: Option<Vec<u32>> = None; &self,
if !self.cat.is_none() { conf: impl Borrow<Config>,
// unholy amalgation of code to make the comma-separated list of strings into a vector of integers search_type: impl AsRef<str>,
categories = Some( ) -> SearchParameters {
self.cat let search_type: &str = search_type.as_ref();
.as_ref() let conf: Config = conf.borrow().clone();
.unwrap()
.split(",")
.filter_map(|s| s.parse().ok())
.collect(),
);
}
let mut extended_attribute_names: Option<Vec<String>> = None; let split = |string: String| -> Option<Vec<u32>> {
if !self.attrs.is_none() { Some(
extended_attribute_names = Some( string
self.attrs
.as_ref()
.unwrap()
.split(",") .split(",")
.map(|s| s.to_string()) .filter_map(|s| s.parse::<u32>().ok())
.collect(), .collect::<Vec<u32>>(),
); )
} };
let mut extended_attrs: Option<bool> = None; let categories = self.cat.clone().and_then(split);
// also, let's just make it a closure at this point
let extended_attribute_names: Option<Vec<String>> = self
.attrs
.clone()
.and_then(|l| Some(l.split(",").map(|s| s.to_string()).collect()));
// let mut extended_attrs: Option<bool> = self.extended.and_then(|k| if k == 1 { Some(true) } else { Some(false) }); // was this what you were trying to do?
let mut extended_attrs = None;
if !self.extended.is_none() && self.extended.ok_or(false).unwrap() == 1 { if !self.extended.is_none() && self.extended.ok_or(false).unwrap() == 1 {
extended_attrs = Some(true); extended_attrs = Some(true);
} }
let mut limit: u32 = self.limit.ok_or("").unwrap_or(conf.caps.limits.max); let mut limit: u32 = self.limit.unwrap_or(conf.caps.limits.default);
if limit > conf.caps.limits.max { if limit > conf.caps.limits.max {
limit = conf.caps.limits.max; limit = conf.caps.limits.max;
} }