From fcef148285310d2ce1a9c2b9950763c38575529e Mon Sep 17 00:00:00 2001 From: askiiart Date: Thu, 5 Dec 2024 13:11:52 -0600 Subject: [PATCH] clean up code and make to_parameters use AsRef Co-authored-by: confused-ace-noises --- src/api.rs | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/src/api.rs b/src/api.rs index 0d667a7..94d675a 100644 --- a/src/api.rs +++ b/src/api.rs @@ -4,6 +4,7 @@ use rocket::http::Status; use rocket::response::status; use rocket::{get, response::content::RawXml}; use rocket::{FromForm, State}; +use std::borrow::Borrow; use std::str; use xml::writer::{EmitterConfig, XmlEvent}; @@ -32,38 +33,38 @@ struct SearchForm { impl SearchForm { /// Converts it to a SearchParameters object - fn to_parameters(&self, conf: Config, search_type: &str) -> SearchParameters { - let mut categories: Option> = None; - if !self.cat.is_none() { - // unholy amalgation of code to make the comma-separated list of strings into a vector of integers - categories = Some( - self.cat - .as_ref() - .unwrap() - .split(",") - .filter_map(|s| s.parse().ok()) - .collect(), - ); - } + fn to_parameters( + &self, + conf: impl Borrow, + search_type: impl AsRef, + ) -> SearchParameters { + let search_type: &str = search_type.as_ref(); + let conf: Config = conf.borrow().clone(); - let mut extended_attribute_names: Option> = None; - if !self.attrs.is_none() { - extended_attribute_names = Some( - self.attrs - .as_ref() - .unwrap() + let split = |string: String| -> Option> { + Some( + string .split(",") - .map(|s| s.to_string()) - .collect(), - ); - } + .filter_map(|s| s.parse::().ok()) + .collect::>(), + ) + }; - let mut extended_attrs: Option = 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> = self + .attrs + .clone() + .and_then(|l| Some(l.split(",").map(|s| s.to_string()).collect())); + + // let mut extended_attrs: Option = 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 { 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 { limit = conf.caps.limits.max; }