1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
//! This crate defines Guzzle and its custom derive. //! //! Example: //! ``` //! use guzzle::Guzzle; //! //! #[derive(Default, Guzzle)] //! struct Location { //! #[guzzle(keys = ["lng"])] //! lng: String, //! #[guzzle(keys = ["lat"])] //! lat: String, //! } //! //! let test_data = vec![ //! ("lng", "51.5074° N".to_string()), //! ("lat", "0.1278° W".to_string()), //! ("some-other-key", "some-other-key".to_string()), //! ]; //! //! let mut location = Location::default(); //! //! let remaining_data: Vec<(&str, String)> = test_data //! .into_iter() //! .filter_map(|v| location.guzzle(v)) //! .collect(); //! //! assert_eq!(location.lng, "51.5074° N".to_string()); //! assert_eq!(location.lat, "0.1278° W".to_string()); //! //! assert_eq!(remaining_data, [("some-other-key", "some-other-key".to_string())]); //! ``` //! However, the names of your keys may not match your struct names. To map those values, use the //! guzzle attribute macro: //! ``` //! use guzzle::Guzzle; //! //! #[derive(Default, Guzzle)] //! struct Location { //! #[guzzle(keys = ["longitude", "lng"])] //! lng: String, //! #[guzzle(keys = ["latitude", "lat"])] //! lat: String, //! } //! //! let test_data = vec![ //! ("longitude", "51.5074° N".to_string()), //! ("lat", "0.1278° W".to_string()), //! ("some-other-key", "some-other-key".to_string()), //! ]; //! //! let mut location = Location::default(); //! //! let remaining_data: Vec<(&str, String)> = test_data //! .into_iter() //! .filter_map(|v| location.guzzle(v)) //! .collect(); //! //! assert_eq!(location.lng, "51.5074° N".to_string()); //! assert_eq!(location.lat, "0.1278° W".to_string()); //! //! assert_eq!(remaining_data, [("some-other-key", "some-other-key".to_string())]); //! ``` pub use guzzle_derive::*; pub trait Guzzle { fn guzzle<T>(&mut self, current: (T, String)) -> Option<(T, String)> where T: AsRef<str>; } #[cfg(test)] mod tests { mod guzzle_trait { use crate::Guzzle; #[derive(Default)] struct Tester { pub one: String, pub two: String, } impl Guzzle for Tester { fn guzzle<T>(&mut self, (key, value): (T, String)) -> Option<(T, String)> where T: AsRef<str>, { match key.as_ref() { "one" => self.one = value, "two" => self.two = value, _ => return Some((key, value)), } None } } #[test] fn guzzle_with_vec_string_string() { let test_data = vec![ ("one".to_string(), "1".to_string()), ("two".to_string(), "2".to_string()), ("three".to_string(), "3".to_string()), ]; let mut tester = Tester::default(); let remaining_data: Vec<(String, String)> = test_data .into_iter() .filter_map(|v| tester.guzzle(v)) .collect(); assert_eq!(tester.one, "1".to_string()); assert_eq!(tester.two, "2".to_string()); assert_eq!(remaining_data.len(), 1); assert_eq!( remaining_data.into_iter().next(), Some(("three".to_string(), "3".to_string())) ); } #[test] fn guzzle_with_vec_str_string() { let test_data = vec![ ("one", "1".to_string()), ("two", "2".to_string()), ("three", "3".to_string()), ]; let mut tester = Tester::default(); let remaining_data: Vec<(&str, String)> = test_data .into_iter() .filter_map(|v| tester.guzzle(v)) .collect(); assert_eq!(tester.one, "1".to_string()); assert_eq!(tester.two, "2".to_string()); assert_eq!(remaining_data.len(), 1); assert_eq!( remaining_data.into_iter().next(), Some(("three", "3".to_string())) ); } #[test] fn guzzle_with_hash_str_string() { use std::collections::HashMap; let test_data: HashMap<String, String> = vec![ ("one".to_string(), "1".to_string()), ("two".to_string(), "2".to_string()), ("three".to_string(), "3".to_string()), ] .into_iter() .collect(); let mut tester = Tester::default(); let remaining_data: Vec<(String, String)> = test_data .into_iter() .filter_map(|v| tester.guzzle(v)) .collect(); assert_eq!(tester.one, "1".to_string()); assert_eq!(tester.two, "2".to_string()); assert_eq!(remaining_data.len(), 1); assert_eq!( remaining_data.into_iter().next(), Some(("three".to_string(), "3".to_string())) ); } } //mod guzzle_meta_data_derive { // use crate::Guzzle; // // #[derive(Default, Guzzle)] // struct AttributeDemo { // /// This field is not annotated, therefore its field is `basic` and its keys contain // /// one string which is the same as the name `basic`. // basic: String, // /// This field may be filled from multiple keys // #[guzzle(keys = ["one", "two"])] // listed_keys: String, // /// This field is not a string, you must provider a parser that will transform it into // /// the correct type // #[guzzle(parser = "my_parser")] // other_types: u64, // /// This field isn't a string and has multiple keys // #[guzzle(parser = my_parser, keys = ["three", "four"])] // other_types_with_listed_keys: u64, // } // // #[test] // fn everything() { // use std::collections::HashMap; // // fn my_parser(s: String) -> u64 { // s.parse().unwrap_or(0) // } // // let test_data: HashMap<String, String> = vec![ // ("basic".to_string(), "basic info".to_string()), // ("one".to_string(), "1".to_string()), // ("two".to_string(), "2".to_string()), // ("other_types".to_string(), "20".to_string()), // ("three".to_string(), "3".to_string()), // ("four".to_string(), "4".to_string()), // ] // .into_iter() // .collect(); // // let attribute_demo = AttributeDemo::default(); // // let remaining_data: Vec<(String, String)> = attribute_demo // .into_iter() // .filter_map(|v| tester.guzzle(v)) // .collect(); // // assert_eq!(attribute_demo.basic, "basic info".to_string()); // assert_eq!(attribute_demo.listed_keys, "2".to_string()); // assert_eq!(attribute_demo.other_types, 20); // assert_eq!(attribute_demo.other_types_with_listed_keys, 4); // } //} }