av_stream_info_rust/
lat_long.rs

1use crate::DecodeError;
2use std::convert::TryFrom;
3use serde::{Deserialize, Serialize};
4
5/// Represents a geo-location with latitude and longitude. It can be
6/// constructed from a String.
7/// 
8/// # Example
9/// ```rust
10/// use std::convert::TryFrom;
11/// use av_stream_info_rust::LatLong;
12/// 
13/// let lat_long_str = String::from("10.1,-3.1");
14/// let lat_long = LatLong::try_from(lat_long_str).unwrap();
15/// println!("{},{}", lat_long.lat, lat_long.long);
16/// ```
17#[derive(Debug, Serialize, Deserialize, Clone)]
18pub struct LatLong {
19    /// Latitude
20    pub lat: f64,
21    /// Longitude
22    pub long: f64,
23}
24
25impl TryFrom<String> for LatLong {
26    type Error = DecodeError;
27
28    fn try_from(
29        lat_long_str: String,
30    ) -> std::result::Result<Self, <Self as TryFrom<String>>::Error> {
31        let mut iter = lat_long_str.splitn(2, ",");
32        Ok(LatLong {
33            lat: iter
34                .next()
35                .ok_or(DecodeError::LatMissing)?
36                .parse()
37                .or(Err(DecodeError::NumberParseError))?,
38            long: iter
39                .next()
40                .ok_or(DecodeError::LongMissing)?
41                .parse()
42                .or(Err(DecodeError::NumberParseError))?,
43        })
44    }
45}