refactor(users/Profpatsch/aerc): move ini stuff out

First shot at generating a dhall FFI standard.

Change-Id: I1cdf7eeaa6b2668a49282315f308a8e51abd0cf6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/5887
Reviewed-by: Profpatsch <mail@profpatsch.de>
Tested-by: BuildkiteCI
This commit is contained in:
Profpatsch 2022-06-19 15:07:14 +02:00
parent 2cf67e113e
commit 3c3713c69f
5 changed files with 246 additions and 135 deletions

View file

@ -1,41 +1,25 @@
let NameVal = λ(T : Type) → { name : Text, value : T }
let Lib = ./dhall/lib.dhall
let List/map = Lib.List/map
let Ini = ./ini/ini.dhall
in λ ( imports
: { -- Take an aerc filter from the aerc distribution /share directory
aercFilter : Text → Text
, -- given a dsl of functions to create an Ini, render the ini file
toIni :
{ globalSection : List (NameVal Text)
, sections : List (NameVal (List (NameVal Text)))
} →
Text
, Ini/externs : Ini.Externs
}
) →
let List/map
: ∀(a : Type) → ∀(b : Type) → (a → b) → List a → List b
= λ(a : Type) →
λ(b : Type) →
λ(f : a → b) →
λ(xs : List a) →
List/build
b
( λ(list : Type) →
λ(cons : b → list → list) →
List/fold a xs list (λ(x : a) → cons (f x))
)
in { accounts =
imports.toIni
{ globalSection = [] : List (NameVal Text)
{ accounts =
imports.Ini/externs.renderIni
{ globalSection = [] : Ini.Section
, sections =
[ { name = "mail"
, value =
[ { name = "archive", value = "Archive" }
, { name = "copy-to", value = "Sent" }
, { name = "default", value = "INBOX" }
, { name = "from"
, value = "Profpatsch <mail@profpatsch.de>"
}
, { name = "from", value = "Profpatsch <mail@profpatsch.de>" }
, { name = "source", value = "maildir://~/.Mail/mail" }
, { name = "postpone", value = "Drafts" }
]
@ -43,14 +27,12 @@ in λ ( imports
]
}
, aerc =
imports.toIni
{ globalSection = [] : List (NameVal Text)
imports.Ini/externs.renderIni
{ globalSection = [] : Ini.Section
, sections =
[ { name = "filters"
, value =
[ { name = "text/html"
, value = imports.aercFilter "html"
}
[ { name = "text/html", value = imports.aercFilter "html" }
, let _ = "-- TODO: this awk should be taken from nix!"
in { name = "text/*"
@ -79,7 +61,7 @@ in λ ( imports
-- render a list of keys to config format
renderKeys =
λ(keys : List Key) →
List/map Key (NameVal Text) renderKey keys
List/map Key (Ini.NameVal Text) renderKey keys
let
-- create a section whith a name and a list of keys
@ -153,5 +135,5 @@ in λ ( imports
]
}
in imports.toIni config
in imports.Ini/externs.renderIni config
}

View file

@ -15,13 +15,16 @@ let
root = ./.;
files = [
"aerc.dhall"
"dhall/lib.dhall"
"ini/ini.dhall"
];
main = "aerc.dhall";
deps = [ ];
deps = [
];
}
{
aercFilter = name: "${aerc-patched}/share/aerc/filters/${name}";
toIni = depot.users.Profpatsch.toINI { };
"Ini/externs" = depot.users.Profpatsch.ini.externs;
};
aerc-config = pkgs.linkFarm "alacritty-config" [

View file

@ -0,0 +1,84 @@
let List/map
: ∀(a : Type) → ∀(b : Type) → (a → b) → List a → List b
= λ(a : Type) →
λ(b : Type) →
λ(f : a → b) →
λ(xs : List a) →
List/build
b
( λ(list : Type) →
λ(cons : b → list → list) →
List/fold a xs list (λ(x : a) → cons (f x))
)
let
--| Concatenate a `List` of `List`s into a single `List`
List/concat
: ∀(a : Type) → List (List a) → List a
= λ(a : Type) →
λ(xss : List (List a)) →
List/build
a
( λ(list : Type) →
λ(cons : a → list → list) →
λ(nil : list) →
List/fold
(List a)
xss
list
(λ(xs : List a) → λ(ys : list) → List/fold a xs list cons ys)
nil
)
let
-- Transform a list by applying a function to each element and flattening the results
List/concatMap
: ∀(a : Type) → ∀(b : Type) → (a → List b) → List a → List b
= λ(a : Type) →
λ(b : Type) →
λ(f : a → List b) →
λ(xs : List a) →
List/build
b
( λ(list : Type) →
λ(cons : b → list → list) →
List/fold a xs list (λ(x : a) → List/fold b (f x) list cons)
)
let Status = < Empty | NonEmpty : Text >
let
{-|
Transform each value in a `List` to `Text` and then concatenate them with a
separator in between each value
-}
Text/concatMapSep
: ∀(separator : Text) → ∀(a : Type) → (a → Text) → List a → Text
= λ(separator : Text) →
λ(a : Type) →
λ(f : a → Text) →
λ(elements : List a) →
let status =
List/fold
a
elements
Status
( λ(x : a) →
λ(status : Status) →
merge
{ Empty = Status.NonEmpty (f x)
, NonEmpty =
λ(result : Text) →
Status.NonEmpty (f x ++ separator ++ result)
}
status
)
Status.Empty
in merge { Empty = "", NonEmpty = λ(result : Text) → result } status
in { List/map, List/concat, List/concatMap, Text/concatMapSep }

View file

@ -0,0 +1,6 @@
{ depot, ... }:
{
externs = {
renderIni = depot.users.Profpatsch.toINI { };
};
}

View file

@ -0,0 +1,36 @@
let lib = ../dhall/lib.dhall
let NameVal = λ(T : Type) → { name : Text, value : T }
let ValueList = λ(T : Type) → List (NameVal T)
let Section = ValueList Text
let Sections = ValueList Section
let Ini = { globalSection : Section, sections : Sections }
let
-- Takes to INI files and merges their global sections and their section lists,
-- without duplicating by section name.
appendInis =
λ(inis : List Ini) →
{ globalSection =
lib.List/concat
(NameVal Text)
(lib.List/map Ini Section (λ(i : Ini) → i.globalSection) inis)
, sections =
lib.List/concat
(NameVal Section)
(lib.List/map Ini Sections (λ(i : Ini) → i.sections) inis)
}
: Ini
let
-- Signatures of functions that are input via FFI.
Externs =
{ -- given a dsl of functions to create an Ini, render the ini file
renderIni : Ini → Text
}
in { NameVal, ValueList, Section, Sections, Ini, appendInis, Externs }