Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #[derive(Default)]
  2. pub struct Coord {
  3. pub lon: f64,
  4. pub lat: f64,
  5. }
  6.  
  7. impl From<(String, String)> for Coord {
  8. fn from((str_lon, str_lat): (String, String)) -> Self {
  9. Self {
  10. lon: if str_lon.is_empty() {
  11. <f64>::default()
  12. } else {
  13. str_lon.parse::<f64>().unwrap()
  14. },
  15. lat: if str_lat.is_empty() {
  16. <f64>::default()
  17. } else {
  18. str_lat.parse::<f64>().unwrap()
  19. },
  20. }
  21. }
  22. }
  23.  
  24. impl Into<(String, String)> for Coord {
  25. fn into(self) -> (String, String) {
  26. (
  27. if self.lon == <f64>::default() {
  28. "".to_string()
  29. } else {
  30. self.lon.to_string()
  31. },
  32. if self.lat == <f64>::default() {
  33. "".to_string()
  34. } else {
  35. self.lat.to_string()
  36. },
  37. )
  38. }
  39. }
  40.  
  41.  
  42. fn main() {
  43. let test = "".to_string();
  44. assert!(test.is_empty());
  45. let coord = Coord{lon: 0.0, lat: 0.0};
  46. let (lat, lon) = coord.into();
  47.  
  48. assert_eq!(lat, "");
  49. assert_eq!(lon, "");
  50.  
  51. let coord = Coord::from(("".to_string(),"".to_string()));
  52. assert_eq!(coord.lon, <f64>::default());
  53. assert_eq!(coord.lat, <f64>::default());
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement