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
use crate::types::errors::TypeError;
/// The radius from the given latitude and longitude for a [Place](crate::types::extended::object::place::Place).
/// The units is expressed by the [units](crate::types::properties::units::Units) property.
///
/// If [units](crate::types::properties::units::Units) is not specified, the default is assumed to be "`m`" indicating "meters".
///
/// Specifications: <https://www.w3.org/TR/activitystreams-vocabulary/#dfn-radius>
#[derive(Debug, PartialEq)]
pub struct Radius(f32);
impl Radius {
pub const MIN_BOUND: f32 = 0.0;
pub fn new(value: f32) -> Result<Self, TypeError> {
if value < Self::MIN_BOUND {
return Err(TypeError::OutOfBoundsMin {
min: Self::MIN_BOUND.to_string(),
found: value.to_string(),
});
}
Ok(Self(value))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let radius = Radius::new(42.13);
assert!(radius.is_ok());
assert_eq!(radius.unwrap(), Radius(42.13));
}
#[test]
fn test_new_out_of_bounds_min() {
let radius = Radius::new(-13.9);
assert!(radius.is_err());
assert_eq!(
radius.unwrap_err(),
TypeError::OutOfBoundsMin {
min: Radius::MIN_BOUND.to_string(),
found: "-13.9".to_string()
}
);
}
}