api: support HashSet<String> as api type

This is just an array of strings in json, or a
semicolon-separated string on the command line.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-08-01 11:20:10 +02:00
parent ef13b38632
commit 75e90ebb25
2 changed files with 17 additions and 1 deletions

View File

@ -1,5 +1,7 @@
//! This contains traits used to implement methods to be added to the `Router`.
use std::collections::HashSet;
use failure::{bail, Error};
use http::Response;
use serde_json::{json, Value};
@ -313,6 +315,7 @@ unconstrained_api_type! {String, &str}
unconstrained_api_type! {bool}
unconstrained_api_type! {isize, usize, i64, u64, i32, u32, i16, u16, i8, u8, f64, f32}
unconstrained_api_type! {Vec<String>}
unconstrained_api_type! {HashSet<String>}
// Raw return types are also okay:
impl<Body> ApiType for Response<Body> {

View File

@ -1,6 +1,6 @@
//! Provides Command Line Interface to API methods
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::str::FromStr;
use bytes::Bytes;
@ -382,3 +382,16 @@ impl ParseCli for String {
))
}
}
impl ParseCli for HashSet<String> {
fn parse_cli(name: &str, value: Option<&str>) -> Result<Value, Error> {
Ok(serde_json::Value::Array(value
.ok_or_else(|| format_err!("missing value for parameter '{}'", name))?
.split(';')
.fold(Vec::new(), |mut list, entry| {
list.push(serde_json::Value::String(entry.trim().to_string()));
list
})
))
}
}