refactor: split PinFilter::from_str out of larger fn
not sure what happened with the reformatting, with the simplification of
arms rustfmt decided to move this in an indentation level.
@@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::HashMap;
use std::convert::TryFrom;
+use std::str::FromStr;
use warp::{Filter, Rejection, Reply};
mod add;
@@ -47,7 +48,8 @@ impl<'a> TryFrom<&'a str> for ListRequest {
let mut timeout = None;
for (key, value) in parse {
- let target = match &*key {
+ let target =
+ match &*key {
"arg" => {
// FIXME: this should be IpfsPath
args.push(
@@ -60,18 +62,10 @@ impl<'a> TryFrom<&'a str> for ListRequest {
if filter.is_none() {
// not parsing this the whole way as there might be hope to have this
// function removed in the future.
- filter = Some(match &*value {
- "all" => PinFilter::All,
- "direct" => PinFilter::Direct,
- "recursive" => PinFilter::Recursive,
- "indirect" => PinFilter::Indirect,
- other => {
- return Err(ParseError::InvalidValue(
- "type".into(),
- other.to_owned().into(),
- ))
- }
- });
+ filter =
+ Some(value.parse::<PinFilter>().map_err(|e| {
+ ParseError::InvalidValue("type".into(), e.into())
+ })?);
continue;
} else {
return Err(DuplicateField(key));
@@ -79,11 +73,10 @@ impl<'a> TryFrom<&'a str> for ListRequest {
}
"timeout" => {
if timeout.is_none() {
- timeout = Some(
- value
- .parse()
- .map_err(|e| ParseError::InvalidDuration("timeout".into(), e))?,
- );
+ timeout =
+ Some(value.parse().map_err(|e| {
+ ParseError::InvalidDuration("timeout".into(), e)
+ })?);
continue;
} else {
return Err(DuplicateField(key));
@@ -120,8 +113,7 @@ impl<'a> TryFrom<&'a str> for ListRequest {
}
}
-#[derive(Debug, serde::Deserialize)]
-#[serde(rename_all = "lowercase")]
+#[derive(Debug)]
enum PinFilter {
Direct,
Indirect,
@@ -129,6 +121,22 @@ enum PinFilter {
All,
}
+impl FromStr for PinFilter {
+ // the not understood input
+ type Err = String;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ use PinFilter::*;
+ Ok(match s {
+ "all" => All,
+ "direct" => Direct,
+ "recursive" => Recursive,
+ "indirect" => Indirect,
+ other => return Err(other.to_owned()),
+ })
+ }
+}
+
impl Default for PinFilter {
fn default() -> Self {
PinFilter::All
