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
use crate::DecodeError;
use std::convert::TryFrom;

/// Represents a geo-location with latitude and longitude. It can be
/// constructed from a String.
/// 
/// # Example
/// ```rust
/// use std::convert::TryFrom;
/// use av_stream_info_rust::LatLong;
/// 
/// let lat_long_str = String::from("10.1,-3.1");
/// let lat_long = LatLong::try_from(lat_long_str).unwrap();
/// println!("{},{}", lat_long.lat, lat_long.long);
/// ```
#[derive(Debug, Serialize, Clone)]
pub struct LatLong {
    /// Latitude
    pub lat: f64,
    /// Longitude
    pub long: f64,
}

impl TryFrom<String> for LatLong {
    type Error = DecodeError;

    fn try_from(
        lat_long_str: String,
    ) -> std::result::Result<Self, <Self as TryFrom<String>>::Error> {
        let mut iter = lat_long_str.splitn(2, ",");
        Ok(LatLong {
            lat: iter
                .next()
                .ok_or(DecodeError::LatMissing)?
                .parse()
                .or(Err(DecodeError::NumberParseError))?,
            long: iter
                .next()
                .ok_or(DecodeError::LongMissing)?
                .parse()
                .or(Err(DecodeError::NumberParseError))?,
        })
    }
}