aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-09-11 15:12:30 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-09-11 15:12:30 -0700
commit14e14de8be12256e623ca4a147b62469c0fc16ab (patch)
tree9a19c2c6ddcbeb9f17c4a9048a2907cf581abdfc
parentb9b79a7b7385ba18654d51bc2ca1b5d07f191d0e (diff)
downloadfatcat-14e14de8be12256e623ca4a147b62469c0fc16ab.tar.gz
fatcat-14e14de8be12256e623ca4a147b62469c0fc16ab.zip
add ExpandFlags type
-rw-r--r--rust/src/api_helpers.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/rust/src/api_helpers.rs b/rust/src/api_helpers.rs
index 96fc898e..b1d2de94 100644
--- a/rust/src/api_helpers.rs
+++ b/rust/src/api_helpers.rs
@@ -21,6 +21,78 @@ pub struct EditContext {
pub autoaccept: bool,
}
+#[derive(Clone,Copy,PartialEq)]
+pub struct ExpandFlags {
+ pub files: bool,
+ pub container: bool,
+ pub releases: bool,
+ pub creators: bool,
+}
+
+impl ExpandFlags {
+
+ pub fn from_string(param: &str) -> ExpandFlags {
+ let list: Vec<&str> = param.split_terminator(",").collect();
+ ExpandFlags::from_strings(&list)
+ }
+ pub fn from_strings(list: &[&str]) -> ExpandFlags {
+ if list.contains(&"none") {
+ ExpandFlags::none()
+ } else if list.contains(&"all") {
+ ExpandFlags::all()
+ } else {
+ ExpandFlags {
+ files: list.contains(&"files"),
+ container: list.contains(&"container"),
+ releases: list.contains(&"releases"),
+ creators: list.contains(&"creators"),
+ }
+ }
+ }
+ pub fn all() -> ExpandFlags {
+ ExpandFlags {
+ files: true,
+ container: true,
+ releases: true,
+ creators: true,
+ }
+ }
+ pub fn none() -> ExpandFlags {
+ ExpandFlags {
+ files: false,
+ container: false,
+ releases: false,
+ creators: false,
+ }
+ }
+}
+
+#[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!["files", "container", "other_thing", "releases", "creators"]);
+ assert!(all == ExpandFlags {
+ files: true,
+ container: true,
+ releases: true,
+ 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!(all == ExpandFlags {
+ files: true,
+ container: true,
+ releases: true,
+ creators: true
+ });
+ assert!(all == ExpandFlags::all());
+}
+
pub fn make_edit_context(
conn: &DbConn,
editgroup_id: Option<FatCatId>,