Source file DerivPkg.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
type source = Pkg.source =
  | Local
  | Unknown
  | Pkg of Namespaced.t
  | Special of string

let pp_source ppf = function
  | Local -> Format.pp_print_string ppf "local"
  | Unknown -> Format.pp_print_string ppf "unknown"
  | Pkg namespaced -> Format.fprintf ppf "pkg %a" Namespaced.pp namespaced
  | Special special -> Format.fprintf ppf "special %s" special

let compare_source (t1 : source) (t2 : source) =
  match (t1, t2) with
  | Local, Local -> 0
  | Local, Unknown -> 1
  | Local, Pkg _ -> 2
  | Local, Special _ -> 3
  | Unknown, Local -> -1
  | Unknown, Unknown -> 0
  | Unknown, Pkg _ -> 1
  | Unknown, Special _ -> 2
  | Pkg _, Local -> -2
  | Pkg _, Unknown -> -1
  | Pkg a1, Pkg a2 -> Namespaced.compare a1 a2
  | Pkg _, Special _ -> 1
  | Special _, Local -> -3
  | Special _, Unknown -> -2
  | Special _, Pkg _ -> -1
  | Special a1, Special a2 -> String.compare a1 a2

type t = Pkg.t = { source : source; file : Namespaced.t }

let pp ppf { source; file } =
  Format.fprintf ppf "@[<hov 2>%a@ @@ %a@]" Namespaced.pp file pp_source source

let compare { source = a1; file = b1 } { source = a2; file = b2 } =
  match compare_source a1 a2 with 0 -> Namespaced.compare b1 b2 | c -> c

(** Use when you want two {!Pkg.t} to be different if they have different file extensions.
    Constrast with {!Namespaced.compare} which does not compare the file extensions. *)
module Distinct = struct
  let pp ppf { source; file } =
    Format.fprintf ppf "@[<hov 2>%a@ @@ %a@]" Namespaced.pp_as_filepath file
      pp_source source

  let compare { source = a1; file = b1 } { source = a2; file = b2 } =
    match compare_source a1 a2 with
    | 0 -> DerivNamespaced.Distinct.compare b1 b2
    | c -> c
end