Start work on parsing logic for urls

This commit is contained in:
Austen Adler 2023-03-26 22:03:52 -04:00
parent c1ed165973
commit 9bea0077a7
2 changed files with 53 additions and 14 deletions

View File

@ -17,13 +17,29 @@ impl CoordinateUrls {
pub fn parse(i: &str) -> Result<Self, Error> { pub fn parse(i: &str) -> Result<Self, Error> {
let url = Url::parse(i.trim())?; let url = Url::parse(i.trim())?;
// Try to parse a url from google maps let pairs = url.query_pairs();
url.query_pairs() let mut possible_latlon = vec![];
.find_map(|(key, value)| {
if key != "query" {
return None;
}
for (key, value) in pairs {
if key == "query" {
possible_latlon.push(value.to_string());
}
}
if let Some(fragment) = url.fragment() {
if let Some(("map", v)) = fragment.split_once("=") {
if let Some((_, lat_slash_lon)) = v.split_once("/") {
if let Some((lat, lon)) = lat_slash_lon.split_once("/") {
possible_latlon.push(format!("{lat},{lon}"));
}
}
}
}
// Try to parse a url from google maps
possible_latlon
.iter()
.find_map(|value| {
LatLon::parse_full(value.as_ref()) LatLon::parse_full(value.as_ref())
.map(|(_, ret)| ret) .map(|(_, ret)| ret)
.ok() .ok()
@ -31,6 +47,29 @@ impl CoordinateUrls {
}) })
.ok_or_else(|| Error::NoUrlLatLon(i.to_string()))? .ok_or_else(|| Error::NoUrlLatLon(i.to_string()))?
} }
// fn parse_openstreetmap(i: Url) ->IResult<(), LatLon> {
// map_res(tuple((
// tag("map="),
// digit1,
// tag("/"),
// parse_f64 ,
// tag("/"),
// parse_f64 ,
// eof
// )), |(
// _,
// _,
// _,
// lat ,
// _,
// lon ,
// _,
// )| {
// LatLon::new(lat,lon)
// })(i.fragment().ok_or((Err(()))))
// }
} }
impl From<&CoordinateUrls> for LatLon { impl From<&CoordinateUrls> for LatLon {

View File

@ -19,14 +19,6 @@
<th /> <th />
</tr> </tr>
{#each formats as format}
<tr>
<th>{format.toUpperCase()}</th>
<td>{xpin.allCoordinates[format]}</td>
<td><CopyButton data={xpin.allCoordinates[format]} /></td>
</tr>
{/each}
{#each urlFormats as format} {#each urlFormats as format}
<tr> <tr>
<th>{format.toUpperCase()}</th> <th>{format.toUpperCase()}</th>
@ -38,4 +30,12 @@
<td><CopyButton data={xpin.coordinateUrls[format]} /></td> <td><CopyButton data={xpin.coordinateUrls[format]} /></td>
</tr> </tr>
{/each} {/each}
{#each formats as format}
<tr>
<th>{format.toUpperCase()}</th>
<td>{xpin.allCoordinates[format]}</td>
<td><CopyButton data={xpin.allCoordinates[format]} /></td>
</tr>
{/each}
</table> </table>