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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::directions::{
error::Error,
request::Request,
travel_mode::TravelMode
};
impl<'a> Request<'a> {
pub fn validate(&'a mut self) -> Result<&'a mut Request, Error> {
if let Some(travel_mode) = &self.travel_mode {
if *travel_mode == TravelMode::Transit {
if let Some(waypoints) = &self.waypoints {
return Err(Error::EitherWaypointsOrTransitMode(waypoints.len()));
}
} else {
if let Some(arrival_time) = &self.arrival_time {
return Err(Error::ArrivalTimeIsForTransitOnly(
travel_mode.to_string(),
arrival_time.format("%F %r").to_string(),
));
}
if let Some(transit_modes) = &self.transit_modes {
return Err(Error::TransitModeIsForTransitOnly(
travel_mode.to_string(),
String::from(
transit_modes
.iter()
.map(|mode| mode.to_string() + "|")
.collect::<String>()
.trim_end_matches('|'),
),
));
}
if let Some(transit_route_preference) = &self.transit_route_preference {
return Err(Error::TransitRoutePreferenceIsForTransitOnly(
travel_mode.to_string(),
transit_route_preference.to_string(),
));
}
}
}
if let Some(waypoints) = &self.waypoints {
if let Some(alternatives) = &self.alternatives {
if !alternatives {
return Err(Error::EitherAlternativesOrWaypoints(waypoints.len()));
}
}
if let Some(restrictions) = &self.restrictions {
return Err(Error::EitherRestrictionsOrWaypoints(
waypoints.len(),
String::from(
restrictions
.iter()
.map(|avoid| avoid.to_string() + "|")
.collect::<String>()
.trim_end_matches('|'),
),
));
}
if waypoints.len() > 25 {
return Err(Error::TooManyWaypoints(waypoints.len()));
}
}
if let Some(arrival_time) = &self.arrival_time {
if let Some(departure_time) = &self.departure_time {
return Err(Error::EitherDepartureTimeOrArrivalTime(
arrival_time.format("%F %r").to_string(),
departure_time.to_string(),
));
}
}
self.validated = true;
Ok(self)
}
}