Change use of deserialize_with for Option iterator

This commit is contained in:
Daniel García 2019-02-08 19:12:08 +01:00
parent 8b4a6f2a64
commit 820c8b0dce
No known key found for this signature in database
GPG Key ID: FC8A7D14C3CD543A

View File

@ -1,21 +1,16 @@
use rocket::request::Form; use rocket::request::Form;
use rocket::Route;
use rocket_contrib::json::Json; use rocket_contrib::json::Json;
use serde_json::Value; use serde_json::Value;
use crate::db::models::*;
use crate::db::DbConn;
use crate::CONFIG;
use crate::api::{ use crate::api::{
EmptyResult, JsonResult, JsonUpcase, JsonUpcaseVec, Notify, NumberOrString, PasswordData, UpdateType, EmptyResult, JsonResult, JsonUpcase, JsonUpcaseVec, Notify, NumberOrString, PasswordData, UpdateType,
}; };
use crate::auth::{decode_invite, AdminHeaders, Headers, OwnerHeaders}; use crate::auth::{decode_invite, AdminHeaders, Headers, OwnerHeaders};
use crate::db::models::*;
use crate::db::DbConn;
use crate::mail; use crate::mail;
use crate::CONFIG;
use serde::{Deserialize, Deserializer};
use rocket::Route;
pub fn routes() -> Vec<Route> { pub fn routes() -> Vec<Route> {
routes![ routes![
@ -447,14 +442,6 @@ fn get_org_users(org_id: String, _headers: AdminHeaders, conn: DbConn) -> JsonRe
}))) })))
} }
fn deserialize_collections<'de, D>(deserializer: D) -> Result<Vec<CollectionData>, D::Error>
where
D: Deserializer<'de>,
{
// Deserialize null to empty Vec
Deserialize::deserialize(deserializer).or_else(|_| Ok(vec![]))
}
#[derive(Deserialize)] #[derive(Deserialize)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
struct CollectionData { struct CollectionData {
@ -467,8 +454,7 @@ struct CollectionData {
struct InviteData { struct InviteData {
Emails: Vec<String>, Emails: Vec<String>,
Type: NumberOrString, Type: NumberOrString,
#[serde(deserialize_with = "deserialize_collections")] Collections: Option<Vec<CollectionData>>,
Collections: Vec<CollectionData>,
AccessAll: Option<bool>, AccessAll: Option<bool>,
} }
@ -524,7 +510,7 @@ fn send_invite(org_id: String, data: JsonUpcase<InviteData>, headers: AdminHeade
// If no accessAll, add the collections received // If no accessAll, add the collections received
if !access_all { if !access_all {
for col in &data.Collections { for col in data.Collections.iter().flatten() {
match Collection::find_by_uuid_and_org(&col.Id, &org_id, &conn) { match Collection::find_by_uuid_and_org(&col.Id, &org_id, &conn) {
None => err!("Collection not found in Organization"), None => err!("Collection not found in Organization"),
Some(collection) => { Some(collection) => {
@ -714,8 +700,7 @@ fn get_user(org_id: String, org_user_id: String, _headers: AdminHeaders, conn: D
#[allow(non_snake_case)] #[allow(non_snake_case)]
struct EditUserData { struct EditUserData {
Type: NumberOrString, Type: NumberOrString,
#[serde(deserialize_with = "deserialize_collections")] Collections: Option<Vec<CollectionData>>,
Collections: Vec<CollectionData>,
AccessAll: bool, AccessAll: bool,
} }
@ -780,7 +765,7 @@ fn edit_user(
// If no accessAll, add the collections received // If no accessAll, add the collections received
if !data.AccessAll { if !data.AccessAll {
for col in &data.Collections { for col in data.Collections.iter().flatten() {
match Collection::find_by_uuid_and_org(&col.Id, &org_id, &conn) { match Collection::find_by_uuid_and_org(&col.Id, &org_id, &conn) {
None => err!("Collection not found in Organization"), None => err!("Collection not found in Organization"),
Some(collection) => { Some(collection) => {