Skip to main content

BYNK_URL_SRC

Constant BYNK_URL_SRC 

Source
pub const BYNK_URL_SRC: &str = "commons bynk.url {\n\t---\n\tOne `name=value` pair of a query string, decoded.\n\t---\n\ttype QueryParam = {\n\t\tname: String,\n\t\tvalue: String,\n\t}\n\n\t---\n\tAn absolute URL with an authority, as `parse` reads it: `scheme` and `host`\n\tin lower case, `path` as written (percent-encoded, `\"\"` or starting with\n\t`/`), and `query` and `fragment` decoded. `format` writes it back.\n\t---\n\ttype Url = {\n\t\tscheme: String,\n\t\thost: String,\n\t\tport: Option[Int],\n\t\tpath: String,\n\t\tquery: List[QueryParam],\n\t\tfragment: Option[String],\n\t}\n\n\t---\n\tReads an absolute URL, `scheme://host[:port][path][?query][#fragment]`,\n\tafter RFC 3986, not the WHATWG URL standard: nothing is repaired. The\n\tscheme is a letter, then letters, digits, `+`, `-` or `.`. The host is not\n\tempty and holds no `@`, `[`, `]`, `:`, space or `%`, so a user-info part or\n\tan IPv6 literal is refused. The port is 0 to 65535. The path is checked by\n\t`isPath`. The query is read as `decodeQuery` reads it. Anything else, a\n\tmalformed escape included, is `None`.\n\t---\n\tfn parse(s: String) -> Option[Url] {\n\t\tlet schemeEnd = s.indexOf(\"://\").getOrElse(-1)\n\t\tlet scheme = s.slice(0, schemeEnd)\n\t\tlet rest = s.slice(schemeEnd + 3, s.length())\n\t\tlet hashAt = rest.indexOf(\"#\").getOrElse(rest.length())\n\t\tlet beforeHash = rest.slice(0, hashAt)\n\t\tlet fragment: Option[Option[String]] = if hashAt == rest.length() {\n\t\t\tSome(None)\n\t\t} else {\n\t\t\trest.slice(hashAt + 1, rest.length()).percentDecode().map((f) => Some(f))\n\t\t}\n\t\tlet queryAt = beforeHash.indexOf(\"?\").getOrElse(beforeHash.length())\n\t\tlet beforeQuery = beforeHash.slice(0, queryAt)\n\t\tlet query: Option[List[QueryParam]] = if queryAt == beforeHash.length() {\n\t\t\tSome(List.empty())\n\t\t} else {\n\t\t\tdecodeQuery(beforeHash.slice(queryAt + 1, beforeHash.length()))\n\t\t}\n\t\tlet pathAt = beforeQuery.indexOf(\"/\").getOrElse(beforeQuery.length())\n\t\tlet authority = beforeQuery.slice(0, pathAt)\n\t\tlet path = beforeQuery.slice(pathAt, beforeQuery.length())\n\t\tlet colonAt = authority.indexOf(\":\").getOrElse(authority.length())\n\t\tlet host = authority.slice(0, colonAt)\n\t\tlet port: Option[Option[Int]] = if colonAt == authority.length() {\n\t\t\tSome(None)\n\t\t} else {\n\t\t\tparsePort(authority.slice(colonAt + 1, authority.length())).map((p) => Some(p))\n\t\t}\n\t\tif schemeEnd < 0 || !isScheme(scheme) || !isHost(host) || !isPath(path) {\n\t\t\tNone\n\t\t} else {\n\t\t\tfragment.andThen((f) => query.andThen((q) => port.map((p) => Url {\n\t\t\t\tscheme: scheme.toLower(),\n\t\t\t\thost: host.toLower(),\n\t\t\t\tport: p,\n\t\t\t\tpath: path,\n\t\t\t\tquery: q,\n\t\t\t\tfragment: f,\n\t\t\t})))\n\t\t}\n\t}\n\n\t---\n\tWrites `u` as `scheme://host[:port]path[?query][#fragment]`. The query is\n\t`formatQuery(u.query)` and the fragment is percent-encoded; the path is\n\twritten as given, so it should already be encoded. An empty query writes no\n\t`?`. `parse(format(u))` is `Some(u)` for every `u` that `parse` returned.\n\t---\n\tfn format(u: Url) -> String {\n\t\tlet port = u.port.map((p) => \":\".concat(p.toString())).getOrElse(\"\")\n\t\tlet query = if u.query.length() == 0 { \"\" } else { \"?\".concat(formatQuery(u.query)) }\n\t\tlet fragment = u.fragment.map((f) => \"#\".concat(f.percentEncode())).getOrElse(\"\")\n\t\tu.scheme\n\t\t\t.concat(\"://\")\n\t\t\t.concat(u.host)\n\t\t\t.concat(port)\n\t\t\t.concat(u.path)\n\t\t\t.concat(query)\n\t\t\t.concat(fragment)\n\t}\n\n\t---\n\tWhether `s` is a URL scheme: a letter, then letters, digits, `+`, `-` or\n\t`.`.\n\t---\n\tfn isScheme(s: String) -> Bool {\n\t\tlet letters = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n\t\tlet rest = letters.concat(\"0123456789+-.\")\n\t\ts.length() > 0 && letters.contains(s.slice(0, 1)) && s.chars().all((c) => rest.contains(c))\n\t}\n\n\t---\n\tWhether `s` can be the host of a URL `parse` reads: not empty, and holding\n\tno `@`, `[`, `]`, `:`, `/`, `?`, `#`, space or `%`.\n\t---\n\tfn isHost(s: String) -> Bool {\n\t\ts.length() > 0 && s.chars().all((c) => !\"@[]:/?# %\".contains(c))\n\t}\n\n\t---\n\tWhether `s` is a path `parse` accepts: empty or starting with `/`, holding\n\tonly RFC 3986 path characters (letters, digits, `-._~!$&\'()*+,;=:@/` and\n\t`%`), with every `%` starting a valid escape. A space or a non-ASCII\n\tcharacter must be percent-encoded.\n\t---\n\tfn isPath(s: String) -> Bool {\n\t\tlet allowed = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~!$&\'()*+,;=:@/%\"\n\t\t(s.length() == 0 || s.startsWith(\"/\"))\n\t\t\t&& s.chars().all((c) => allowed.contains(c))\n\t\t\t&& s.percentDecode().isSome()\n\t}\n\n\t---\n\tReads a port: 1 to 5 digits, at most 65535.\n\t---\n\tfn parsePort(s: String) -> Option[Int] {\n\t\tlet none: Option[Int] = None\n\t\tlet digits = s.length() > 0\n\t\t\t&& s.length() <= 5\n\t\t\t&& s.chars().all((c) => \"0123456789\".contains(c))\n\t\tlet parsed = if digits { Int.parse(s) } else { none }\n\t\tparsed.andThen((p) => if p > 65535 { none } else { Some(p) })\n\t}\n\n\t---\n\tReads a query string strictly: split on `&` (empty pairs skipped), each\n\tpair on its first `=` (none means an empty value), `+` read as a space, then\n\tpercent-decoded. `None` if any pair has a malformed escape. A leading `?`\n\tis part of the first name; `parseQuery` drops it.\n\t---\n\tfn decodeQuery(raw: String) -> Option[List[QueryParam]] {\n\t\tlet pairs = raw.split(\"&\").filter((p) => p.length() > 0).map((p) => decodePair(p))\n\t\tif pairs.all((p) => p.isSome()) {\n\t\t\tSome(pairs.flatMap((p) => p.map((q) => [q]).getOrElse(List.empty())))\n\t\t} else {\n\t\t\tNone\n\t\t}\n\t}\n\n\t---\n\tReads one `name=value` pair as `decodeQuery` does.\n\t---\n\tfn decodePair(pair: String) -> Option[QueryParam] {\n\t\tlet eqAt = pair.indexOf(\"=\").getOrElse(pair.length())\n\t\tlet name = pair.slice(0, eqAt).replace(\"+\", \" \").percentDecode()\n\t\tlet value = pair.slice(eqAt + 1, pair.length()).replace(\"+\", \" \").percentDecode()\n\t\tname.andThen((n) => value.map((v) => QueryParam { name: n, value: v }))\n\t}\n\n\t---\n\tReads a query string leniently, as a handler reading user input usually\n\twants: a leading `?` is dropped, and a pair with a malformed escape is\n\tskipped rather than failing the whole string.\n\t---\n\tfn parseQuery(s: String) -> List[QueryParam] {\n\t\tlet raw = if s.startsWith(\"?\") { s.slice(1, s.length()) } else { s }\n\t\traw.split(\"&\").filter((p) => p.length() > 0).flatMap((p) => decodePair(p)\n\t\t\t.map((q) => [q])\n\t\t\t.getOrElse(List.empty()))\n\t}\n\n\t---\n\tWrites `params` as a query string, without the `?`: each name and value\n\tpercent-encoded, pairs joined with `&`, in order.\n\t---\n\tfn formatQuery(params: List[QueryParam]) -> String {\n\t\tlet init: Option[String] = None\n\t\tparams.fold(init, (acc, p) => {\n\t\t\tlet pair = p.name.percentEncode().concat(\"=\").concat(p.value.percentEncode())\n\t\t\tmatch acc {\n\t\t\t\tSome(s) => Some(s.concat(\"&\").concat(pair)),\n\t\t\t\tNone => Some(pair),\n\t\t\t}\n\t\t}).getOrElse(\"\")\n\t}\n\n\t---\n\tThe first value of the query parameter `name`, if there is one.\n\t---\n\tfn param(u: Url, name: String) -> Option[String] {\n\t\tu.query.filter((p) => p.name == name).first().map((p) => p.value)\n\t}\n\n\t---\n\tEvery value of the query parameter `name`, in order.\n\t---\n\tfn params(u: Url, name: String) -> List[String] {\n\t\tu.query.filter((p) => p.name == name).map((p) => p.value)\n\t}\n\n\t---\n\t`u` with `name=value` appended to its query.\n\t---\n\tfn withParam(u: Url, name: String, value: String) -> Url {\n\t\tUrl {\n\t\t\t...u,\n\t\t\tquery: u.query.append(QueryParam { name: name, value: value }),\n\t\t}\n\t}\n\n\t---\n\t`u` with every `name` parameter removed from its query.\n\t---\n\tfn withoutParam(u: Url, name: String) -> Url {\n\t\tUrl {\n\t\t\t...u,\n\t\t\tquery: u.query.filter((p) => p.name != name),\n\t\t}\n\t}\n\n\t---\n\tThe path\'s segments, split on `/` and percent-decoded, with no empty first\n\tsegment: `/a/b%20c/` is `[\"a\", \"b c\", \"\"]`, so a trailing `/` gives an empty\n\tlast segment and the path `/` alone is `[\"\"]`. An empty path is `[]`. A\n\tsegment that does not decode is kept as written (a `Url` built by hand may\n\thold one; `parse` refuses it).\n\t---\n\tfn segments(u: Url) -> List[String] {\n\t\tlet parts = u.path.split(\"/\")\n\t\tlet rest = if u.path.startsWith(\"/\") { parts.skip(1) } else { parts }\n\t\tif u.path.length() == 0 {\n\t\t\tList.empty()\n\t\t} else {\n\t\t\trest.map((s) => s.percentDecode().getOrElse(s))\n\t\t}\n\t}\n}\n";
Expand description

bynk.url (standard-library K2, #2594) — a Url record with RFC 3986 parse/format and query-string helpers, written in Bynk over the String kernel. Only percent-encoding is kernel (percentEncode, percentDecode): it needs a string’s UTF-8 bytes. Url is a record here, not a base type, because a base type’s name is a lexer token, and Url as a keyword would break every user type Url.