Function parses a string and if successful returns a `GammaDistribution` struct. Currently this function assumes that the args in the string is ordered. The format is: {`shape`,`rate`,`loc`} Example: {1.0,2.0,1} Where: `shape`: 1.0 `rate`: 2.0 `loc`: 1.0 # Errors If input fails to parse the specified format
(s: &str)
| 20 | /// # Errors |
| 21 | /// If input fails to parse the specified format |
| 22 | fn from_str(s: &str) -> Result<Self, String> { |
| 23 | let [shape, rate, loc] = { |
| 24 | let rv = s |
| 25 | .strip_prefix('{') |
| 26 | .and_then(|s| s.strip_suffix('}')) |
| 27 | .ok_or(String::from( |
| 28 | "Parsing distribution. Expected braces. No braces found.", |
| 29 | ))?; |
| 30 | let rv: Vec<&str> = rv.split(',').collect(); |
| 31 | |
| 32 | if rv.len() > 3 { |
| 33 | return Err(format!( |
| 34 | "Parsing distribution. Too many inputs. Expected 3. Found {}", |
| 35 | rv.len() |
| 36 | )); |
| 37 | } |
| 38 | if rv.len() < 3 { |
| 39 | return Err(format!( |
| 40 | "Parsing distribution. Too few inputs. Expected 3. Found {}", |
| 41 | rv.len() |
| 42 | )); |
| 43 | } |
| 44 | |
| 45 | [rv[0], rv[1], rv[2]] |
| 46 | }; |
| 47 | |
| 48 | let shape = shape |
| 49 | .trim() |
| 50 | .parse::<f64>() |
| 51 | .map_err(|_| String::from("ParseDelayArgs::ParseParam"))?; |
| 52 | let rate = rate |
| 53 | .trim() |
| 54 | .parse::<f64>() |
| 55 | .map_err(|_| String::from("ParseDelayArgs::ParseParam"))?; |
| 56 | let loc = loc |
| 57 | .trim() |
| 58 | .parse::<f64>() |
| 59 | .map_err(|_| String::from("ParseDelayArgs::ParseParam"))?; |
| 60 | |
| 61 | Result::Ok(GammaDistribution::new(shape, rate, loc).unwrap()) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #[cfg(test)] |