Refactor code to to have a dedicated type for Library.name

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
Rudi Grinberg 2018-08-07 12:07:16 +03:00
parent 6260dad66b
commit 317388fd95
2 changed files with 26 additions and 11 deletions

View File

@ -37,21 +37,36 @@ let module_name =
let module_names = list module_name >>| String.Set.of_list let module_names = list module_name >>| String.Set.of_list
let invalid_lib_name ~loc = of_sexp_errorf loc "invalid library name" module Lib_name : sig
type t
let library_name = val to_string : t -> string
plain_string (fun ~loc name ->
val of_string : loc:Loc.t -> string -> t
val t : t Sexp.Of_sexp.t
end = struct
type t = string
let invalid ~loc = of_sexp_errorf loc "invalid library name"
let to_string s = s
let of_string ~loc name =
match name with match name with
| "" -> invalid_lib_name ~loc | "" -> invalid ~loc
| s -> | s ->
if s.[0] = '.' then invalid_lib_name ~loc if s.[0] = '.' then invalid ~loc
else else
try try
String.iter s ~f:(function String.iter s ~f:(function
| 'A'..'Z' | 'a'..'z' | '_' | '.' | '0'..'9' -> () | 'A'..'Z' | 'a'..'z' | '_' | '.' | '0'..'9' -> ()
| _ -> raise_notrace Exit); | _ -> raise_notrace Exit);
s s
with Exit -> invalid_lib_name ~loc) with Exit -> invalid ~loc
let t = plain_string of_string
end
let file = let file =
plain_string (fun ~loc s -> plain_string (fun ~loc s ->
@ -868,7 +883,7 @@ module Library = struct
record record
(let%map buildable = Buildable.t (let%map buildable = Buildable.t
and loc = loc and loc = loc
and name = field_o "name" library_name and name = field_o "name" Lib_name.t
and public = Public_lib.public_name_field and public = Public_lib.public_name_field
and synopsis = field_o "synopsis" string and synopsis = field_o "synopsis" string
and install_c_headers = and install_c_headers =
@ -900,9 +915,9 @@ module Library = struct
let name = let name =
match name, public with match name, public with
| Some n, _ -> n | Some n, _ -> n
| None, Some { name = (_loc, name) ; _ } -> | None, Some { name = (loc, name) ; _ } ->
if dune_version >= (1, 1) then if dune_version >= (1, 1) then
name Lib_name.of_string ~loc name
else else
of_sexp_error loc "name field cannot be omitted before version \ of_sexp_error loc "name field cannot be omitted before version \
1.1 of the dune language" 1.1 of the dune language"
@ -914,7 +929,7 @@ module Library = struct
"name field is missing" "name field is missing"
) )
in in
{ name { name = Lib_name.to_string name
; public ; public
; synopsis ; synopsis
; install_c_headers ; install_c_headers

View File

@ -4,7 +4,7 @@ the name field can be omitted for libraries when public_name is present
this isn't possible for older syntax <= (1, 0) this isn't possible for older syntax <= (1, 0)
$ dune build --root no-name-lib-syntax-1-0 $ dune build --root no-name-lib-syntax-1-0
File "dune", line 1, characters 0-27: File "dune", line 1, characters 22-25:
Error: name field cannot be omitted before version 1.1 of the dune language Error: name field cannot be omitted before version 1.1 of the dune language
[1] [1]