package codept-lib

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file unitname.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
52
type t =
  { modname : Modname.t
  ; filepath : string }
(* TODO(dinosaure): we probably should validate [filename] too as regular
   file path. *)

let invalid_arg fmt = Format.kasprintf invalid_arg fmt

let modulize filepath =
  let filename = Filename.basename filepath in
  let name = Support.remove_extension filename in
  if String.length name < 1
  then invalid_arg "Impossible to modulize an empty string";
  if not (Support.is_upper name.[0] || Support.is_lower name.[0])
  then invalid_arg "Impossible to modulize %S (from %S)" name filename;
  let modname = String.capitalize_ascii name in
  let modname = Modname.v modname in
  { modname; filepath; }

let pp ppf { modname; filepath; } =
  Pp.fp ppf "%a(%s)" Modname.pp modname filepath
let pp_as_modname ppf { modname; _ } = Modname.pp ppf modname
let pp_as_filepath ppf { filepath; _ } = Format.pp_print_string ppf filepath


let modname { modname; _ } = modname

let reflect ppf t =
  Pp.fp ppf "(Unitname.modulize %S)" (Modname.to_string @@ modname @@ modulize t.filepath)

let filename { filepath; _ } = Filename.basename filepath
let filepath { filepath; _ } = filepath
let compare_as_modnames a b = Modname.compare a.modname b.modname

let change_file_extension f t =
  match Support.extension t.filepath with
  | "" -> t
  | ext ->
    let filepath = (Support.remove_extension t.filepath) ^ "." ^ (f ext) in
    { t with filepath }

module Map = Map.Make (struct
  type nonrec t = t

  let compare a b = Modname.compare a.modname b.modname
end)

module Set = Set.Make (struct
  type nonrec t = t

  let compare a b = Modname.compare a.modname b.modname
end)