summaryrefslogtreecommitdiffstats
path: root/rust/src
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-09-11 20:32:01 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-09-11 20:32:33 -0700
commitd45deae242f6a256b52874d5a6995e3a7e0bdab4 (patch)
treefd20da515628e3bb2924b5cc1e630a60dd9cba32 /rust/src
parent99306a4dab4919c1c9fed338560f8c3e01a6382f (diff)
downloadfatcat-d45deae242f6a256b52874d5a6995e3a7e0bdab4.tar.gz
fatcat-d45deae242f6a256b52874d5a6995e3a7e0bdab4.zip
use idiomatic from_str for ExpandFlags
Diffstat (limited to 'rust/src')
-rw-r--r--rust/src/api_helpers.rs30
-rw-r--r--rust/src/api_wrappers.rs2
2 files changed, 18 insertions, 14 deletions
diff --git a/rust/src/api_helpers.rs b/rust/src/api_helpers.rs
index f260d25f..1df42622 100644
--- a/rust/src/api_helpers.rs
+++ b/rust/src/api_helpers.rs
@@ -29,12 +29,16 @@ pub struct ExpandFlags {
pub creators: bool,
}
-impl ExpandFlags {
- pub fn from_string(param: &str) -> ExpandFlags {
+impl FromStr for ExpandFlags {
+ type Err = Error;
+ fn from_str(param: &str) -> Result<ExpandFlags> {
let list: Vec<&str> = param.split_terminator(",").collect();
- ExpandFlags::from_strings(&list)
+ Ok(ExpandFlags::from_str_list(&list))
}
- pub fn from_strings(list: &[&str]) -> ExpandFlags {
+}
+
+impl ExpandFlags {
+ pub fn from_str_list(list: &[&str]) -> ExpandFlags {
if list.contains(&"none") {
ExpandFlags::none()
} else if list.contains(&"all") {
@@ -68,10 +72,10 @@ impl ExpandFlags {
#[test]
fn test_expand_flags() {
- assert!(ExpandFlags::from_strings(&vec![]).files == false);
- assert!(ExpandFlags::from_strings(&vec!["files"]).files == true);
- assert!(ExpandFlags::from_strings(&vec!["file"]).files == false);
- let all = ExpandFlags::from_strings(&vec![
+ assert!(ExpandFlags::from_str_list(&vec![]).files == false);
+ assert!(ExpandFlags::from_str_list(&vec!["files"]).files == true);
+ assert!(ExpandFlags::from_str_list(&vec!["file"]).files == false);
+ let all = ExpandFlags::from_str_list(&vec![
"files",
"container",
"other_thing",
@@ -86,11 +90,11 @@ fn test_expand_flags() {
creators: true
}
);
- assert!(ExpandFlags::from_string("").files == false);
- assert!(ExpandFlags::from_string("files").files == true);
- assert!(ExpandFlags::from_string("something,,files").files == true);
- assert!(ExpandFlags::from_string("file").files == false);
- let all = ExpandFlags::from_string("files,container,other_thing,releases,creators");
+ assert!(ExpandFlags::from_str("").files == false);
+ assert!(ExpandFlags::from_str("files").files == true);
+ assert!(ExpandFlags::from_str("something,,files").files == true);
+ assert!(ExpandFlags::from_str("file").files == false);
+ let all = ExpandFlags::from_str("files,container,other_thing,releases,creators");
assert!(
all == ExpandFlags {
files: true,
diff --git a/rust/src/api_wrappers.rs b/rust/src/api_wrappers.rs
index 1f61cc9b..85739ea6 100644
--- a/rust/src/api_wrappers.rs
+++ b/rust/src/api_wrappers.rs
@@ -39,7 +39,7 @@ macro_rules! wrap_entity_handlers {
match expand {
None => $model::db_get(&conn, entity_id),
Some(param) => {
- let expand_flags = ExpandFlags::from_string(&param);
+ let expand_flags = ExpandFlags::from_str(&param)?;
let mut entity = $model::db_get(&conn, entity_id)?;
entity.db_expand(&conn, expand_flags)?;
Ok(entity)