|
| 1 | +import eyg/analysis/type_/binding |
| 2 | +import eyg/analysis/type_/isomorphic as t |
| 3 | +import eyg/interpreter/break |
| 4 | +import eyg/interpreter/cast |
| 5 | +import eyg/interpreter/value as v |
| 6 | +import gleam/http as gleam_http |
| 7 | +import gleam/option.{None, Some} |
| 8 | +import gleam/result.{try} |
| 9 | +import gleam/uri.{Uri} |
| 10 | +import touch_grass/http |
| 11 | + |
| 12 | +pub fn uri() -> binding.Mono { |
| 13 | + t.record([ |
| 14 | + #("scheme", http.scheme()), |
| 15 | + // host + port = authority |
| 16 | + #("host", t.String), |
| 17 | + #("port", t.option(t.String)), |
| 18 | + // path can be empty |
| 19 | + #("path", t.String), |
| 20 | + #("query", t.key_value_list(t.String)), |
| 21 | + ]) |
| 22 | +} |
| 23 | + |
| 24 | +pub fn uri_to_gleam(url: v.Value(a, b)) -> Result(uri.Uri, break.Reason(a, b)) { |
| 25 | + use scheme <- try(cast.field("scheme", http.scheme_to_gleam, url)) |
| 26 | + use host <- try(cast.field("host", cast.as_string, url)) |
| 27 | + use port <- try(cast.field("port", cast.as_option(_, cast.as_integer), url)) |
| 28 | + use path <- try(cast.field("path", cast.as_string, url)) |
| 29 | + use query <- try(cast.field( |
| 30 | + "query", |
| 31 | + cast.as_list_of(_, fn(item) { |
| 32 | + use key <- try(cast.field("key", cast.as_string, item)) |
| 33 | + use value <- try(cast.field("value", cast.as_string, item)) |
| 34 | + Ok(#(key, value)) |
| 35 | + }), |
| 36 | + url, |
| 37 | + )) |
| 38 | + let query = uri.query_to_string(query) |
| 39 | + Ok(Uri( |
| 40 | + scheme: Some(gleam_http.scheme_to_string(scheme)), |
| 41 | + userinfo: None, |
| 42 | + host: Some(host), |
| 43 | + port:, |
| 44 | + path:, |
| 45 | + query: Some(query), |
| 46 | + fragment: None, |
| 47 | + )) |
| 48 | +} |
| 49 | +// We shouldn't have the error cases, maybe touch grass needs a version of a more constrained uri to the encoding |
| 50 | +// pub fn url_to_eyg(url: uri.Uri) { |
| 51 | +// case url { |
| 52 | +// Uri(scheme: Some(scheme), host: Some(host), port:, path:, query:, ..) -> { |
| 53 | +// case gleam_http.scheme_from_string(scheme) { |
| 54 | +// Ok(scheme) -> { |
| 55 | +// let query = option.unwrap(query, "") |
| 56 | +// let query = uri.parse_query(query) |> result.unwrap([]) |
| 57 | +// v.Record( |
| 58 | +// dict.from_list([ |
| 59 | +// #("scheme", http.scheme_to_eyg(scheme)), |
| 60 | +// #("host", v.String(host)), |
| 61 | +// #("port", v.option(port, v.Integer)), |
| 62 | +// #("path", v.String(path)), |
| 63 | +// #( |
| 64 | +// "query", |
| 65 | +// v.LinkedList( |
| 66 | +// list.map(query, fn(pair) { |
| 67 | +// let #(key, value) = pair |
| 68 | +// v.Record( |
| 69 | +// dict.from_list([ |
| 70 | +// #("key", v.String(key)), |
| 71 | +// #("value", v.String(value)), |
| 72 | +// ]), |
| 73 | +// ) |
| 74 | +// }), |
| 75 | +// ), |
| 76 | +// ), |
| 77 | +// ]), |
| 78 | +// ) |
| 79 | +// |> v.ok() |
| 80 | +// } |
| 81 | +// Error(_) -> v.error(v.String("invalid scheme")) |
| 82 | +// } |
| 83 | +// } |
| 84 | +// _ -> v.error(v.String("invalid url")) |
| 85 | +// } |
| 86 | +// } |
0 commit comments