refactor lookups to separate functions
This allows them to be used separately.
This commit is contained in:
parent
913f865fbd
commit
a7823fc059
1 changed files with 38 additions and 25 deletions
|
@ -86,35 +86,40 @@ fn unpercent(encoded: String) -> String {
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn the_lookup(
|
/// Looks up a query in a Map by exact value, no fuzzy matching.
|
||||||
|
fn lookup_exact(query: String, lookup: Map(String, String)) -> #(Int, String) {
|
||||||
|
case map.get(lookup, query) {
|
||||||
|
Ok(result) -> #(200, result)
|
||||||
|
_ -> #(404, "??")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Looks up a station by its name, with fuzzy matching.
|
||||||
|
fn lookup_by_name(
|
||||||
query: String,
|
query: String,
|
||||||
stations: Map(String, String),
|
stations: Map(String, String),
|
||||||
ds100s: Map(String, String),
|
ds100s: Map(String, String),
|
||||||
fuzzy: fn(String) -> List(String)
|
fuzzy: fn(String) -> List(String)
|
||||||
) -> #(Int, String) {
|
) -> #(Int, String) {
|
||||||
case map.get(ds100s, query) {
|
io.println(query)
|
||||||
Ok(name) -> #(200, name)
|
|
||||||
|
case map.get(stations, query) {
|
||||||
|
Ok(ds100) -> #(200, ds100)
|
||||||
_ -> {
|
_ -> {
|
||||||
io.println(query)
|
let results = fuzzy(query)
|
||||||
case map.get(stations, query) {
|
|> list.filter_map(fn (res) { map.get(ds100s, string.uppercase(res)) })
|
||||||
Ok(ds100) -> #(200, ds100)
|
case results {
|
||||||
_ -> {
|
// results -> {
|
||||||
let results = fuzzy(query)
|
// let names = results
|
||||||
|> list.filter_map(fn (res) { map.get(ds100s, string.uppercase(res)) })
|
// |> list.map (fn (res) {
|
||||||
case results {
|
// map.get(ds100s, string.uppercase(res))
|
||||||
// results -> {
|
// |> result.map(fn(a) { "/" <> a })
|
||||||
// let names = results
|
// |> result.unwrap("/")})
|
||||||
// |> list.map (fn (res) {
|
// #(200, string.join(names, "\n"))
|
||||||
// map.get(ds100s, string.uppercase(res))
|
// }
|
||||||
// |> result.map(fn(a) { "/" <> a })
|
[res] -> #(302, res)
|
||||||
// |> result.unwrap("/")})
|
[res, ..] -> #(302, res)
|
||||||
// #(200, string.join(names, "\n"))
|
_ -> #(404, "??")
|
||||||
// }
|
|
||||||
[res] -> #(302, res)
|
|
||||||
[res, ..] -> #(302, res)
|
|
||||||
_ -> #(404, "??")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -135,8 +140,16 @@ fn lookup_station(
|
||||||
200,
|
200,
|
||||||
"ds100 → Name: " <> baseurl <> "/NN\n" <> "Name → ds100: " <> baseurl <> "/Nürnberg Hbf",
|
"ds100 → Name: " <> baseurl <> "/NN\n" <> "Name → ds100: " <> baseurl <> "/Nürnberg Hbf",
|
||||||
)
|
)
|
||||||
Request(method: Get, path: "/" <> path, ..) ->
|
Request(method: Get, path: "/" <> path, ..) -> {
|
||||||
the_lookup(unpercent(path), stations, ds100s, fuzzy)
|
let path = unpercent(path)
|
||||||
|
|
||||||
|
let by_ds100 = lookup_exact(path, ds100s)
|
||||||
|
|
||||||
|
case by_ds100.0 {
|
||||||
|
200 -> by_ds100
|
||||||
|
_ -> lookup_by_name(path, stations, ds100s, fuzzy)
|
||||||
|
}
|
||||||
|
}
|
||||||
_ -> #(404, "intended usage is e.g. curl " <> baseurl <> "/FF")
|
_ -> #(404, "intended usage is e.g. curl " <> baseurl <> "/FF")
|
||||||
}
|
}
|
||||||
let body = bit_builder.from_string(text)
|
let body = bit_builder.from_string(text)
|
||||||
|
|
Loading…
Reference in a new issue