// Copyright 2019 YechaoLi // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #[cfg(test)] #[macro_use] extern crate prune_derive; use std::borrow::Cow; #[derive(Debug, Prune, PartialEq)] pub struct TestStruct<'a> { #[prune(trim)] pub trim: String, #[prune(trim_start)] pub trim_start: String, #[prune(trim_end)] pub trim_end: String, #[prune(no_whitespace)] pub no_whitespace: String, #[prune(trim)] pub optional_trim_none: Option, #[prune(trim)] pub optional_trim_some: Option, #[prune(trim)] pub quard_optional_trim_none: Option>>>, #[prune(trim)] pub quard_optional_trim_some: Option>>>, #[prune(trim)] pub cow_trim: Cow<'a, str>, #[prune(trim_start)] pub cow_trim_start: Cow<'a, str>, #[prune(trim_end)] pub cow_trim_end: Cow<'a, str>, #[prune(no_whitespace)] pub cow_no_whitespace: Cow<'a, str>, #[prune(trim)] pub vec_trim: Vec, #[prune(trim_start)] pub vec_trim_start: Vec, #[prune(trim_end)] pub vec_trim_end: Vec, #[prune(no_whitespace)] pub vec_no_whitespace: Vec, #[prune] pub nested: TestNested, #[prune] pub optional_nested_none: Option, #[prune] pub optional_nested_some: Option, pub other: &'a str, } #[derive(Debug, Prune, PartialEq)] pub struct TestNested { #[prune(trim)] pub trim: String, } #[test] fn test_prune() { use prune::Prune; let t = TestStruct { trim: " trim ".to_owned(), trim_start: " trim start ".to_owned(), trim_end: " trim end ".to_owned(), no_whitespace: " no whitespace ".to_owned(), optional_trim_none: Some(" \n ".to_owned()), optional_trim_some: Some(" optional\n ".to_owned()), quard_optional_trim_none: Some(Some(Some(Some(" \n ".to_owned())))), quard_optional_trim_some: Some(Some(Some(Some(" optional ".to_owned())))), cow_trim: Cow::Borrowed(" cow trim "), cow_trim_start: Cow::Borrowed(" cow trim "), cow_trim_end: Cow::Borrowed(" cow trim "), cow_no_whitespace: Cow::Borrowed(" cow trim "), vec_trim: vec![" vec trim ".to_owned(), " vec trim ".to_owned()], vec_trim_start: vec![" vec trim ".to_owned(), " vec trim ".to_owned()], vec_trim_end: vec![" vec trim ".to_owned(), " vec trim ".to_owned()], vec_no_whitespace: vec![" vec trim ".to_owned(), " vec trim ".to_owned()], nested: TestNested { trim: " trim ".to_owned(), }, optional_nested_none: None, optional_nested_some: Some(TestNested { trim: " trim ".to_owned(), }), other: "", }; let t = t.prune(); assert_eq!( t, TestStruct { trim: "trim".to_owned(), trim_start: "trim start ".to_owned(), trim_end: " trim end".to_owned(), no_whitespace: "nowhitespace".to_owned(), optional_trim_none: None, optional_trim_some: Some("optional".to_owned()), quard_optional_trim_none: None, quard_optional_trim_some: Some(Some(Some(Some("optional".to_owned())))), cow_trim: Cow::Borrowed("cow trim"), cow_trim_start: Cow::Borrowed("cow trim "), cow_trim_end: Cow::Borrowed(" cow trim"), cow_no_whitespace: Cow::Borrowed("cowtrim"), vec_trim: vec!["vec trim".to_owned(), "vec trim".to_owned()], vec_trim_start: vec!["vec trim ".to_owned(), "vec trim ".to_owned()], vec_trim_end: vec![" vec trim".to_owned(), " vec trim".to_owned()], vec_no_whitespace: vec!["vectrim".to_owned(), "vectrim".to_owned()], nested: TestNested { trim: "trim".to_owned(), }, optional_nested_none: None, optional_nested_some: Some(TestNested { trim: "trim".to_owned(), }), other: "", } ); }