This commit is contained in:
parent
169b62e6f3
commit
2d3da197ee
13 changed files with 711 additions and 74 deletions
38
lib/options/default.nix
Normal file
38
lib/options/default.nix
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
{ lib, ...}:
|
||||
let
|
||||
inherit (builtins) isString typeOf;
|
||||
inherit (lib) mkOption types throwIfNot concatStringsSep splitStringBy toLower map;
|
||||
in
|
||||
{
|
||||
options = {
|
||||
mkUrlOptions =
|
||||
defaults:
|
||||
{
|
||||
host = mkOption {
|
||||
type = types.str;
|
||||
example = "host.tld";
|
||||
description = ''
|
||||
Hostname
|
||||
'';
|
||||
} // (defaults.host or {});
|
||||
|
||||
port = mkOption {
|
||||
type = types.port;
|
||||
default = 1234;
|
||||
example = "1234";
|
||||
description = ''
|
||||
Port
|
||||
'';
|
||||
} // (defaults.port or {});
|
||||
|
||||
protocol = mkOption {
|
||||
type = types.str;
|
||||
default = "https";
|
||||
example = "https";
|
||||
description = ''
|
||||
Which protocol to use when creating a url string
|
||||
'';
|
||||
} // (defaults.protocol or {});
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
@ -1,10 +1,15 @@
|
|||
{ lib, ...}:
|
||||
let
|
||||
inherit (builtins) isString typeOf;
|
||||
inherit (lib) throwIfNot concatStringsSep splitStringBy toLower map;
|
||||
inherit (builtins) isString typeOf match toString head;
|
||||
inherit (lib) throwIfNot concatStringsSep splitStringBy toLower map concatMapAttrsStringSep;
|
||||
in
|
||||
{
|
||||
strings = {
|
||||
#========================================================================================
|
||||
# Converts a string to snake case
|
||||
#
|
||||
# simply replaces any uppercase letter to its lowercase variant preceeded by an underscore
|
||||
#========================================================================================
|
||||
toSnakeCase =
|
||||
str:
|
||||
throwIfNot (isString str) "toSnakeCase only accepts string values, but got ${typeOf str}" (
|
||||
|
|
@ -13,5 +18,22 @@ in
|
|||
|> map (p: toLower p)
|
||||
|> concatStringsSep "_"
|
||||
);
|
||||
|
||||
#========================================================================================
|
||||
# Converts a set of url parts to a string
|
||||
#========================================================================================
|
||||
toUrl =
|
||||
{ protocol ? null, host, port ? null, path ? null, query ? null, hash ? null }:
|
||||
let
|
||||
trim_slashes = str: str |> match "^\/*(.+?)\/*$" |> head;
|
||||
encode_to_str = set: concatMapAttrsStringSep "&" (n: v: "${n}=${v}") set;
|
||||
|
||||
_protocol = if protocol != null then "${protocol}://" else "";
|
||||
_port = if port != null then ":${toString port}" else "";
|
||||
_path = if path != null then "/${path |> trim_slashes}" else "";
|
||||
_query = if query != null then "?${query |> encode_to_str}" else "";
|
||||
_hash = if hash != null then "#${hash |> encode_to_str}" else "";
|
||||
in
|
||||
"${_protocol}${host}${_port}${_path}${_query}${_hash}";
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue