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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
/* Copyright 2016 Joshua Gentry * * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your * option. This file may not be copied, modified, or distributed * except according to those terms. */ use std; use toml::Value; use data::Container; use enums::ExtractResult; use item_def::ItemDef; use item_value::ItemValue; use toml_def::TomlDef; use value::UsizeValue; //************************************************************************************************* /// This item is used to read an 64 bit unsigned number from the TOML file. pub struct ItemUsize { //--------------------------------------------------------------------------------------------- /// The name of the item. name : String, //--------------------------------------------------------------------------------------------- /// The min value for the value. min : Option<i64>, //--------------------------------------------------------------------------------------------- /// The max value for the value. max : Option<i64>, //--------------------------------------------------------------------------------------------- /// Flag indicating if the item is optional. optional : bool, //--------------------------------------------------------------------------------------------- /// The default value for the value. default : Option<usize> } impl ItemUsize { //********************************************************************************************* /// Constructs a new instance of the ItemUsize object. pub fn with_name<T:AsRef<str>>(name : T) -> ItemUsize { ItemUsize { name : String::from(name.as_ref()), min : None, max : None, optional : false, default : None } } //********************************************************************************************* /// Adds the item to a group and returns an option that will receive the item's value when /// the file is loaded. Basically this allows a program to receive a value laoded from /// the file without getting the TomlData object that was created. /// /// # Examples /// /// ``` /// use valid_toml::{TomlDef, ItemStr, ItemUsize}; /// /// # fn main() { /// let file = r#" /// password = "secret" /// count = 15 /// "#; /// /// let mut def = TomlDef::new(); /// /// let password = ItemStr::with_name("password").min(3).max(8).add_to(&mut def); /// let count = ItemUsize::with_name("count").optional().add_to(&mut def); /// /// let file = def.parse_toml(file).unwrap(); /// /// assert_eq!(password.get(), "secret"); /// assert_eq!(count.get(), 15); /// # } /// ``` pub fn add_to( self, group : &mut TomlDef ) -> UsizeValue { let notify = UsizeValue::new(); group.add_notify(self.name.clone(), Box::new(notify.clone())); group.ref_add(self); notify } //********************************************************************************************* /// Defines the minimum value (inclusive) of the value read from the file. /// /// # Panics /// /// This method will panic if max is set and is less than min. The method will also panic if /// the value is greater than i64::MAX, which is the largest value that can be read from the /// file. pub fn min( mut self, min : usize ) -> Self { if min as u64 > std::i64::MAX as u64 { panic!("Largest value that can be read from the file is i64::MAX."); } if let Some(max) = self.max { if max < min as i64 { panic!("Minimum value [{}] is greater than the maximum value [{}]", min, max); } } self.min = Some(min as i64); self } //********************************************************************************************* /// Defines the maximum value (inclusive) of the value read from the file. /// /// # Panics /// /// This method will panic if min is set and is greater than max. The method will also panic if /// the value is greater than i64::MAX, which is the largest value that can be read from the /// file. pub fn max( mut self, max : usize ) -> Self { if max as u64 > std::i64::MAX as u64 { panic!("Largest value that can be read from the file is i64::MAX."); } if let Some(min) = self.min { if min > max as i64 { panic!("Maximum value [{}] is less than the minimum value [{}]", max, min); } } self.max = Some(max as i64); self } //********************************************************************************************* /// Marks the item as optional without providing a default value. pub fn optional( mut self, ) -> Self { self.optional = true; self } //********************************************************************************************* /// Defines the default value for the item. This method makes the item optional. The default /// value will only be used if the parent group exists. /// /// # Panics /// /// This method will panic if default is greater than i64::MAX as that is the largest value /// that can be read from a TOML file. pub fn default( mut self, default : usize ) -> Self { if default as u64 > std::i64::MAX as u64 { panic!("Largest value that can be read from the file is i64::MAX."); } self.default = Some(default); self } } impl ItemDef for ItemUsize { //********************************************************************************************* /// Returns the name of the item. fn name(&self) -> &str { &self.name } //********************************************************************************************* /// Validates and extracts the value. fn extract( &self, value : &Value ) -> ExtractResult { //----------------------------------------------------------------------------------------- // Extract the string. if let Some(value) = value.as_integer() { //--------------------------------------------------------------------------------- // If there is a min value, make sure we're over or equal to it. if let Some(min) = self.min { if value < min { return ExtractResult::underrun(self.min.clone(), self.max.clone()) } } else if value < std::usize::MIN as i64 { return ExtractResult::underrun(Some(0), self.max.clone()) } //--------------------------------------------------------------------------------- // If there is a max value, make sure we're under or equal to it. if let Some(max) = self.max { if value > max { return ExtractResult::overrun(self.min.clone(), self.max.clone()) } } //--------------------------------------------------------------------------------- // The value looks good. ExtractResult::Item(ItemValue::Usize(value as usize)) } //----------------------------------------------------------------------------------------- // The value is not a string. else { ExtractResult::incorrect_type("number") } } //********************************************************************************************* /// Returns true if the item is optional. fn is_optional(&self) -> bool { self.optional } //********************************************************************************************* /// Returns the default value for the item if it exists, otherwise return Err. fn default(&self) -> Option<ItemValue> { self.default.as_ref().map(|x|ItemValue::Usize(*x)) } } #[cfg(test)] mod tests { use std; use toml::Value; use item_def::ItemDef; use item_value::ItemValue; use enums::{ExtractResult, ValidationError}; macro_rules! test { ($item:expr, $val:expr) => ($item.extract(&Value::Integer($val))) } //********************************************************************************************* /// Test setting the values don't panic. #[test] fn set() { super::ItemUsize::with_name("a").min(12); super::ItemUsize::with_name("b").max(67); super::ItemUsize::with_name("c").default(123); } //********************************************************************************************* /// Test that the min value is honored. #[test] fn min() { let test = super::ItemUsize::with_name("b").min(53); assert_underrun!(test!(test, 17), 53); assert_usize!(test!(test, 53), 53); assert_usize!(test!(test, 67), 67); } //********************************************************************************************* /// Test that the max value is honored. #[test] fn max() { let test = super::ItemUsize::with_name("b").max(24); assert_usize!(test!(test, 3), 3); assert_usize!(test!(test, 24), 24); assert_overflow!(test!(test, 67), 24); } //********************************************************************************************* /// Test that the min and max values are honored. #[test] fn min_max() { let test = super::ItemUsize::with_name("b").min(11).max(17); assert_underrun!(test!(test, 0), 11, 17); assert_usize!(test!(test, 11), 11); assert_usize!(test!(test, 15), 15); assert_usize!(test!(test, 17), 17); assert_overflow!(test.extract(&Value::Integer(27)), 11, 17) } //********************************************************************************************* /// Test the extremes. #[test] fn validate() { let test = super::ItemUsize::with_name("b"); assert_usize!(test.extract(&Value::Integer(std::usize::MIN as i64)), std::usize::MIN as usize); assert_usize!(test.extract(&Value::Integer(std::i64::MAX)), std::i64::MAX as usize); assert_underrun!(test.extract(&Value::Integer(std::usize::MIN as i64 - 1)), 0); } }