Skip to content

Commit de13059

Browse files
authored
build: adopt asai 0.3 (#78)
1 parent e83d74b commit de13059

16 files changed

+112
-110
lines changed

Diff for: src/Bantorra.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Manager = Manager
22
module Router = Router
3-
module Logger = Logger
3+
module Reporter = Reporter
44
module UnitPath = UnitPath
55
module FilePath = FilePath
66
module File = File

Diff for: src/Bantorra.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Manager = Manager
66

77
module Router = Router
88

9-
module Logger = Logger
9+
module Reporter : Asai.MinimumSigs.Reporter
1010

1111
(** {1 Helper Modules} *)
1212

Diff for: src/File.ml

+28-28
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,45 @@ type path = F.t
66

77
let (/) = F.add_unit_seg
88

9-
let wrap_bos =
9+
let wrap_bos_error code =
1010
function
1111
| Ok r -> r
12-
| Error (`Msg msg) -> Logger.fatal `System msg
12+
| Error (`Msg msg) -> Reporter.fatal code msg
1313

14-
let get_cwd () = F.of_fpath @@ wrap_bos @@ Bos.OS.Dir.current ()
14+
let get_cwd () = F.of_fpath @@ wrap_bos_error SystemError @@ Bos.OS.Dir.current ()
1515

1616
(** Read the entire file as a string. *)
1717
let read p =
18-
Logger.tracef "When reading the file `%a'" (F.pp ~relative_to:(get_cwd())) p @@ fun () ->
19-
wrap_bos @@ Bos.OS.File.read (F.to_fpath p)
18+
Reporter.tracef "when@ reading@ the@ file@ `%a'" (F.pp ~relative_to:(get_cwd())) p @@ fun () ->
19+
wrap_bos_error FileError @@ Bos.OS.File.read (F.to_fpath p)
2020

2121
(** Write a string to a file. *)
2222
let write p s =
23-
Logger.tracef "When writing the file `%a'" (F.pp ~relative_to:(get_cwd())) p @@ fun () ->
24-
wrap_bos @@ Bos.OS.File.write (F.to_fpath p) s
23+
Reporter.tracef "when@ writing@ the@ file@ `%a'" (F.pp ~relative_to:(get_cwd())) p @@ fun () ->
24+
wrap_bos_error FileError @@ Bos.OS.File.write (F.to_fpath p) s
2525

2626
let ensure_dir p =
27-
Logger.tracef "When calling `ensure_dir' on `%a'" (F.pp ~relative_to:(get_cwd())) p @@ fun () ->
28-
ignore @@ wrap_bos @@ Bos.OS.Dir.create (F.to_fpath p)
27+
Reporter.tracef "when@ calling@ `ensure_dir'@ on@ `%a'" (F.pp ~relative_to:(get_cwd())) p @@ fun () ->
28+
ignore @@ wrap_bos_error FileError @@ Bos.OS.Dir.create (F.to_fpath p)
2929

3030
let file_exists p =
31-
wrap_bos @@ Bos.OS.File.exists (F.to_fpath p)
31+
wrap_bos_error FileError @@ Bos.OS.File.exists (F.to_fpath p)
3232

3333
let locate_anchor ~anchor start_dir =
34-
Logger.tracef "When locating the anchor `%s' from `%a'"
34+
Reporter.tracef "when@ locating@ the@ anchor@ `%s'@ from@ `%a'"
3535
anchor (F.pp ~relative_to:(get_cwd())) start_dir @@ fun () ->
3636
let rec go cwd path_acc =
3737
if file_exists (cwd/anchor) then
3838
cwd, UnitPath.of_list path_acc
3939
else
4040
if F.is_root cwd
41-
then Logger.fatal `AnchorNotFound "No anchor found all the way up to the root"
41+
then Reporter.fatalf AnchorNotFound "no@ anchor@ found@ all@ the@ way@ up@ to@ the@ root"
4242
else go (F.parent cwd) @@ F.basename cwd :: path_acc
4343
in
4444
go (F.to_dir_path start_dir) []
4545

4646
let locate_hijacking_anchor ~anchor ~root path =
47-
Logger.tracef "When checking whether there's any hijacking anchor `%s'@ between `%a' and `%a'"
47+
Reporter.tracef "when@ checking@ whether@ there's@ any@ hijacking@ anchor@ `%s'@ between@ `%a' and@ `%a'"
4848
anchor (F.pp ~relative_to:(get_cwd())) root UnitPath.pp path @@ fun () ->
4949
match UnitPath.to_list path with
5050
| [] -> None
@@ -87,28 +87,28 @@ let guess_scheme =
8787
end
8888

8989
let get_home () =
90-
F.of_fpath @@ wrap_bos @@ Bos.OS.Dir.user ()
90+
F.of_fpath @@ wrap_bos_error MissingEnvironmentVariables @@ Bos.OS.Dir.user ()
9191

9292
let read_env_path var =
9393
Result.map (F.of_fpath ~relative_to:(get_cwd ())) @@ Bos.OS.Env.path var
9494

9595
(* XXX I did not test the following code on different platforms. *)
9696
let get_xdg_config_home ~app_name =
97-
Logger.trace "When calculating the XDG_CONFIG_HOME" @@ fun () ->
97+
Reporter.tracef "when@ determining@ the@ value@ of@ XDG_CONFIG_HOME" @@ fun () ->
9898
match read_env_path "XDG_CONFIG_HOME" with
9999
| Ok dir -> dir/app_name
100100
| Error _ ->
101101
match Lazy.force guess_scheme with
102102
| Linux ->
103103
let home =
104-
Logger.try_with get_home
105-
~fatal:(fun _ -> Logger.fatal `System "Both XDG_CONFIG_HOME and HOME are not set")
104+
Reporter.try_with get_home
105+
~fatal:(fun _ -> Reporter.fatalf MissingEnvironmentVariables "both@ XDG_CONFIG_HOME@ and@ HOME@ are@ absent")
106106
in
107107
home/".config"/app_name
108108
| MacOS ->
109109
let home =
110-
Logger.try_with get_home
111-
~fatal:(fun _ -> Logger.fatal `System "Both XDG_CONFIG_HOME and HOME are not set")
110+
Reporter.try_with get_home
111+
~fatal:(fun _ -> Reporter.fatalf MissingEnvironmentVariables "both@ XDG_CONFIG_HOME@ and@ HOME@ are@ absent")
112112
in
113113
home/"Library"/"Application Support"/app_name
114114
| Windows ->
@@ -117,33 +117,33 @@ let get_xdg_config_home ~app_name =
117117
| Ok app_data ->
118118
app_data/app_name/"config"
119119
| Error _ ->
120-
Logger.fatal `System "Both XDG_CONFIG_HOME and APPDATA are not set"
120+
Reporter.fatalf MissingEnvironmentVariables "both@ XDG_CONFIG_HOME@ and@ APPDATA@ are@ absent"
121121
end
122122

123123
(* XXX I did not test the following code on different platforms. *)
124124
let get_xdg_cache_home ~app_name =
125-
Logger.tracef "When calculating XDG_CACHE_HOME" @@ fun () ->
125+
Reporter.tracef "when calculating XDG_CACHE_HOME" @@ fun () ->
126126
match read_env_path "XDG_CACHE_HOME" with
127127
| Ok dir -> dir/app_name
128128
| Error _ ->
129129
match Lazy.force guess_scheme with
130130
| Linux ->
131131
let home =
132-
Logger.try_with get_home
133-
~fatal:(fun _ -> Logger.fatal `System "Both XDG_CACHE_HOME and HOME are not set")
132+
Reporter.try_with get_home
133+
~fatal:(fun _ -> Reporter.fatalf MissingEnvironmentVariables "both@ XDG_CACHE_HOME@ and@ HOME@ are@ absent")
134134
in
135135
home/".cache"/app_name
136136
| MacOS ->
137137
let home =
138-
Logger.try_with get_home
139-
~fatal:(fun _ -> Logger.fatal `System "Both XDG_CACHE_HOME and HOME are not set")
138+
Reporter.try_with get_home
139+
~fatal:(fun _ -> Reporter.fatalf MissingEnvironmentVariables "both@ XDG_CACHE_HOME@ and@ HOME@ are@ absent")
140140
in
141141
home/"Library"/"Caches"/app_name
142142
| Windows ->
143143
begin
144144
match read_env_path "LOCALAPPDATA" with
145145
| Error _ ->
146-
Logger.fatal `System "Both XDG_CACHE_HOME and LOCALAPPDATA are not set"
146+
Reporter.fatalf MissingEnvironmentVariables "both@ XDG_CACHE_HOME@ and@ LOCALAPPDATA@ are@ absent"
147147
| Ok local_app_data ->
148148
local_app_data/app_name/"cache"
149149
end
@@ -158,6 +158,6 @@ let get_package_dir pkg =
158158
FilePath.of_string @@ Findlib.package_directory pkg
159159
with
160160
| Findlib.No_such_package (pkg, msg) ->
161-
Logger.fatalf `System "@[<2>No package named `%s':@ %s@]" pkg msg
161+
Reporter.fatalf InvalidOCamlPackage "@[<2>@[no@ package@ named@ `%s':@]@ %s@]" pkg msg
162162
| Findlib.Package_loop pkg ->
163-
Logger.fatalf `System "Package `%s' is required by itself" pkg
163+
Reporter.fatalf InvalidOCamlPackage "package@ `%s'@ is@ requiring@ itself@ (circularity)" pkg

Diff for: src/FilePath.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let add_ext = Fpath.add_ext
2222

2323
let add_unit_seg p s =
2424
if not (UnitPath.is_seg s) then
25-
Logger.fatalf `System "`%s' not a valid unit segment" s;
25+
Reporter.fatalf IllFormedFilePath "`%s'@ not@ a@ valid@ unit@ segment" s;
2626
Fpath.add_seg p s
2727

2828
let append_unit p u =
@@ -42,18 +42,18 @@ let of_fpath ?relative_to ?expanding_tilde p =
4242
let p_str = Fpath.to_string p in
4343
if p_str == "~" || String.starts_with ~prefix:"~/" p_str then
4444
match expanding_tilde with
45-
| None -> Logger.fatalf `System "Tilde expansion was not enabled for the file path `%a'" Fpath.pp p
45+
| None -> Reporter.fatalf IllFormedFilePath "tilde@ expansion@ is@ not@ enabled@ for@ the@ file@ path@ `%a'" Fpath.pp p
4646
| Some home ->
4747
Fpath.v (Fpath.to_string home ^ String.sub p_str 1 (String.length p_str - 1))
4848
else
49-
Logger.fatalf `System "File path `%a' is not an absolute path" Fpath.pp p
49+
Reporter.fatalf IllFormedFilePath "file@ path@ `%a'@ is@ not@ an@ absolute@ path" Fpath.pp p
5050

5151
let to_fpath p = p
5252

5353
let of_string ?relative_to ?expanding_tilde p =
54-
Logger.tracef "When parsing the file path `%s'" (String.escaped p) @@ fun () ->
54+
Reporter.tracef "when@ parsing@ the@ file@ path@ `%s'" (String.escaped p) @@ fun () ->
5555
match Fpath.of_string p with
56-
| Error (`Msg msg) -> Logger.fatal `System msg
56+
| Error (`Msg msg) -> Reporter.fatal IllFormedFilePath msg
5757
| Ok p -> of_fpath ?relative_to ?expanding_tilde p
5858

5959
let to_string = Fpath.to_string

Diff for: src/Logger.ml

-34
This file was deleted.

Diff for: src/Manager.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ let library_root = Library.root
4848

4949
let resolve lm ?(max_depth=255) =
5050
let rec global ~depth ?starting_dir route path ~suffix =
51-
Logger.tracef "@[<2>When resolving library via the route:@ %a@]" (Json_repr.pp (module Json_repr.Ezjsonm)) route @@ fun () ->
51+
Reporter.tracef "@[<2>@[when@ resolving@ library@ via@ the@ route:@]@ @[%a@]@]" (Json_repr.pp (module Json_repr.Ezjsonm)) route @@ fun () ->
5252
if depth > max_depth then
53-
Logger.fatalf `InvalidLibrary "Library resolution stack overflow (max depth = %i)" max_depth
53+
Reporter.fatalf LibraryNotFound "library@ resolution@ stack@ overflow@ (max depth = %i)" max_depth
5454
else
5555
let lib = load_library_from_route lm ?starting_dir route in
5656
Library.resolve ~depth ~global lib path ~suffix

Diff for: src/Marshal.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let rec normalize : value -> value =
1313
let sorted_uniq_pairs = List.sort_uniq (fun (key1, _) (key2, _) -> String.compare key1 key2) pairs in
1414
if List.length pairs <> List.length sorted_uniq_pairs then
1515
let sorted_pairs = List.sort (fun (key1, _) (key2, _) -> String.compare key1 key2) pairs in
16-
Logger.fatalf `JSONFormat "Duplicate key: %s" (find_duplicate_key sorted_pairs)
16+
Reporter.fatalf IllFormedJSON "duplicate@ key@ `%s'" (find_duplicate_key sorted_pairs)
1717
else
1818
`O sorted_uniq_pairs
1919
| `A elems -> `A (List.map normalize elems)
@@ -23,19 +23,19 @@ let destruct enc json =
2323
try
2424
Json_encoding.destruct enc json
2525
with e ->
26-
Logger.fatalf `JSONFormat "%a" (Json_encoding.print_error ?print_unknown:None) e
26+
Reporter.fatalf IllFormedJSON "%a" (Json_encoding.print_error ?print_unknown:None) e
2727

2828
let construct enc data =
2929
try
3030
Json_encoding.construct enc data
3131
with e ->
32-
Logger.fatalf `JSONFormat "%a" (Json_encoding.print_error ?print_unknown:None) e
32+
Reporter.fatalf IllFormedJSON "%a" (Json_encoding.print_error ?print_unknown:None) e
3333

3434
let parse enc s =
3535
destruct enc @@
3636
try Ezjsonm.value_from_string s with
3737
| Ezjsonm.Parse_error (_, msg) ->
38-
Logger.fatal `JSONFormat msg
38+
Reporter.fatal IllFormedJSON msg
3939

4040
let read enc path =
4141
File.read path |> parse enc

Diff for: src/Reporter.ml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Message =
2+
struct
3+
(** Type of error codes. See the asai documentation. *)
4+
type t =
5+
| SystemError (** Generic system errors. *)
6+
| MissingEnvironmentVariables (** Missing HOME or XDG_* environment variables. *)
7+
| FileError (** File paths are valid, but the files do not exist or file permissions are missing. *)
8+
| IllFormedFilePath (** File paths are ill-formed (independent of the file system state). *)
9+
| WebError (** All the network-related errors. *)
10+
11+
| IllFormedJSON (** Low level JSON parsing errors. *)
12+
13+
| AnchorNotFound (** Could not find the anchor at the expected library location. *)
14+
| HijackingAnchor (** Having an anchor on the path to the expected anchor. *)
15+
| IllFormedAnchor (** The anchor itself is ill-formed. *)
16+
17+
| InvalidRouter (** The routing table itself is broken. *)
18+
| LibraryNotFound (** The routing table is okay, but the library cannot be found. *)
19+
| LibraryConflict (** Conflicting libraries are being loaded. *)
20+
| UnitNotFound (** Libraries are loaded, but the unit is not found. *)
21+
| IllFormedUnitPath (** The unit path is ill-formed. *)
22+
23+
| InvalidOCamlPackage (** Invalid OCaml package. *)
24+
25+
(** Default severity of error codes. See the asai documentation. *)
26+
let default_severity : t -> Asai.Diagnostic.severity =
27+
function
28+
| InvalidRouter -> Bug
29+
| _ -> Error
30+
31+
(** String representation of error codes. See the asai documentation. *)
32+
let short_code : t -> string =
33+
function _ -> "E0001" (** XXX assign actual code *)
34+
end
35+
36+
include Asai.Reporter.Make(Message)

Diff for: src/Router.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ type t = param -> FilePath.t
33
type pipe = param -> param
44

55
type env = {version : string; starting_dir : FilePath.t option}
6-
module Eff = Algaeff.Reader.Make(struct type nonrec env = env end)
6+
module Eff = Algaeff.Reader.Make(struct type t = env end)
77
let get_version () = (Eff.read ()).version
88
let get_starting_dir () = (Eff.read ()).starting_dir
99
let run ~version ?starting_dir = Eff.run ~env:{version; starting_dir}
@@ -12,12 +12,12 @@ let dispatch lookup param =
1212
let name, param = Marshal.destruct Json_encoding.(tup2 string any_ezjson_value) param in
1313
match lookup name with
1414
| Some route -> route param
15-
| None -> Logger.fatalf `InvalidRoute "Router %s does not exist" name
15+
| None -> Reporter.fatalf LibraryNotFound "no@ router@ is@ called@ `%s'" name
1616

1717
let fix ?(hop_limit=255) (f : t -> t) route =
1818
let rec go i route =
1919
if i <= 0 then
20-
Logger.fatalf `InvalidLibrary "Exceeded hop limit (%d)" hop_limit
20+
Reporter.fatalf LibraryNotFound "exceeded@ hop@ limit@ (%d)" hop_limit
2121
else
2222
f (go (i-1)) route
2323
in
@@ -37,13 +37,13 @@ let rewrite_try_once lookup param =
3737
let rewrite_err_on_missing lookup param =
3838
let param = Marshal.normalize param in
3939
match lookup param with
40-
| None -> Logger.fatalf `InvalidRoute "Entry `%s' does not exist" (Marshal.to_string param)
40+
| None -> Reporter.fatalf LibraryNotFound "entry@ `%s'@ does@ not@ exist" (Marshal.to_string param)
4141
| Some param -> param
4242

4343
let rewrite_recursively max_tries lookup param =
4444
let rec go i =
4545
if i = max_tries then
46-
Logger.fatalf `InvalidRoute "Could not resolve %s within %i rewrites" (Marshal.to_string param) max_tries
46+
Reporter.fatalf LibraryNotFound "could@ not@ resolve@ %s@ within@ %i@ rewrites" (Marshal.to_string param) max_tries
4747
else
4848
let param = Marshal.normalize param in
4949
match lookup param with

Diff for: src/UnitPath.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let is_seg s = s <> "" && Fpath.is_seg s && not (Fpath.is_rel_seg s)
1212

1313
let assert_seg s =
1414
if not (is_seg s) then
15-
Logger.fatalf `InvalidLibrary "`%s' not a valid unit segment" (String.escaped s)
15+
Reporter.fatalf IllFormedUnitPath "`%s'@ not@ a@ valid@ unit@ segment" (String.escaped s)
1616

1717
let of_seg s = assert_seg s; [s]
1818

@@ -25,7 +25,7 @@ let to_list l = l
2525
let of_list l = List.iter assert_seg l; l
2626

2727
let of_string ?(allow_ending_slash=false) ?(allow_extra_dots=false) p =
28-
Logger.tracef "When parsing `%s' as a unit path" (String.escaped p) @@ fun () ->
28+
Reporter.tracef "when@ parsing@ `%s'@ as@ a@ unit@ path" (String.escaped p) @@ fun () ->
2929
let p =
3030
if allow_ending_slash && String.ends_with ~suffix:"/" p then
3131
String.sub p 0 (String.length p - 1)

Diff for: src/Web.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
let get ?(follow_redirects=true) url =
22
let args = if follow_redirects then ["-L"] else [] in
3-
Logger.tracef "When calling Web.get(%s)" url @@ fun () ->
3+
Reporter.tracef "when reading content from `%s'" url @@ fun () ->
44
match Curly.get ~args url with
55
| Ok {code = 200; body; _} -> body
6-
| Ok {code; _} -> Logger.fatalf `Web "Got code %d" code
7-
| Error err -> Logger.fatalf `Web "%a" Curly.Error.pp err
6+
| Ok {code; _} -> Reporter.fatalf WebError "got@ HTTP@ code@ %d,@ which@ is@ not@ 200" code
7+
| Error err -> Reporter.fatalf WebError "%a" Curly.Error.pp err
88

99
(* See https://firefox-source-docs.mozilla.org/networking/captive_portals.html *)
1010
let online =
1111
lazy begin
12-
Logger.try_with ~emit:(fun _ -> ()) ~fatal:(fun _ -> false) @@ fun () ->
12+
Reporter.try_with ~emit:(fun _ -> ()) ~fatal:(fun _ -> false) @@ fun () ->
1313
String.equal
1414
(get "http://detectportal.firefox.com/canonical.html")
1515
"<meta http-equiv=\"refresh\" content=\"0;url=https://support.mozilla.org/kb/captive-portal\"/>"

0 commit comments

Comments
 (0)