Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a 'only_child' blanket implementation #408

Merged
merged 2 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/minidom_utils/try_only_child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ pub trait TryOnlyChild {
fn try_only_child<'a>(&'a self, child_name: &str) -> Result<&'a Self> {
self.try_only_child_with_filter(child_name, |_| true)
}

/// Get a unique child from its name and return an [Option](std::Option)
/// Returns None if the child can't be found or if the child is not unique
fn only_child<'a>(&'a self, child_name: &str) -> Option<&'a Self> {
self.try_only_child(child_name).ok()
}
}

impl TryOnlyChild for Element {
Expand Down
4 changes: 2 additions & 2 deletions src/netex_idf/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ fn load_netex_lines(
for line in lines_node.children().filter(|e| e.name() == "Line") {
let id = line.try_attribute("id")?;
let name = line.try_only_child("Name")?.text().parse()?;
let code = line.try_only_child("ShortName").map(Element::text).ok();
let private_code = line.try_only_child("PrivateCode").map(Element::text).ok();
let code = line.only_child("ShortName").map(Element::text);
let private_code = line.only_child("PrivateCode").map(Element::text);
let network_id = if let Some(network_id) = map_line_network.get(&id) {
network_id.to_string()
} else {
Expand Down
17 changes: 7 additions & 10 deletions src/transxchange/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,10 @@ fn load_network(transxchange: &Element) -> Result<Network> {
.trim()
.to_string();
let timezone = Some(String::from(EUROPE_LONDON_TIMEZONE));
let url = operator.try_only_child("WebSite").map(Element::text).ok();
let url = operator.only_child("WebSite").map(Element::text);
let phone = operator
.try_only_child("ContactTelephoneNumber")
.map(Element::text)
.ok();
.only_child("ContactTelephoneNumber")
.map(Element::text);
let network = Network {
id,
name,
Expand Down Expand Up @@ -437,14 +436,13 @@ fn get_stop_point_activity<'a>(
vehicle_journey_timing_links
.iter()
.find(|vjtl| {
vjtl.try_only_child("JourneyPatternTimingLinkRef")
vjtl.only_child("JourneyPatternTimingLinkRef")
.map(Element::text)
.ok()
== journey_pattern_timing_link
.attr("id")
.map(|s| s.to_string())
})
.and_then(|vjtl| vjtl.try_only_child(direction).ok())
.and_then(|vjtl| vjtl.only_child(direction))
.unwrap_or(stop_point)
}

Expand Down Expand Up @@ -651,10 +649,9 @@ fn load_routes_vehicle_journeys_calendars(
)?;
let route_id = route.id.clone();
let headsign = journey_pattern
.try_only_child("DestinationDisplay")
.only_child("DestinationDisplay")
.map(Element::text)
.map(|head_sign| head_sign.trim().to_string())
.ok();
.map(|head_sign| head_sign.trim().to_string());

// Insert only at the last moment and if no duplicate calendar exist
if dup_calendar.is_none() {
Expand Down