From 4220b6236c4660d0ff0d7320a27cf898b54187b6 Mon Sep 17 00:00:00 2001 From: Mathieu Barbin Date: Wed, 4 Sep 2024 18:58:13 +0200 Subject: [PATCH] Rename Fpart, reduce deps and add tests --- CHANGES.md | 7 +++ dune-project | 48 --------------- fpath-base.opam | 6 -- fpath-sexp0.opam | 6 -- lib/fpath_base/src/dune | 7 +-- lib/fpath_base/src/fpath_base.ml | 4 +- lib/fpath_base/src/fpath_base.mli | 6 +- lib/fpath_base/test/test__absolute_path.ml | 40 ++++++++++-- lib/fpath_base/test/test__file_name.ml | 33 ---------- lib/fpath_base/test/test__fpart.ml | 61 +++++++++++++++++++ .../{test__file_name.mli => test__fpart.mli} | 0 lib/fpath_base/test/test__relative_path.ml | 42 +++++++++++-- lib/fpath_sexp0/src/dune | 3 +- .../src/{file_name.ml => fpart.ml} | 2 +- .../src/{file_name.mli => fpart.mli} | 18 +++--- lib/fpath_sexp0/src/fpath0.mli | 3 +- lib/fpath_sexp0/src/fpath_sexp0.ml | 2 +- lib/fpath_sexp0/src/fpath_sexp0.mli | 2 +- lib/fpath_sexp0/src/path.ml | 2 +- lib/fpath_sexp0/src/path.mli | 12 ++-- 20 files changed, 170 insertions(+), 134 deletions(-) delete mode 100644 lib/fpath_base/test/test__file_name.ml create mode 100644 lib/fpath_base/test/test__fpart.ml rename lib/fpath_base/test/{test__file_name.mli => test__fpart.mli} (100%) rename lib/fpath_sexp0/src/{file_name.ml => fpart.ml} (95%) rename lib/fpath_sexp0/src/{file_name.mli => fpart.mli} (53%) diff --git a/CHANGES.md b/CHANGES.md index 666814a..6905dd0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,10 @@ +## 0.2.0 (2024-09-04) + +### Changed + +- Rename `File_name` into `Fpart`. +- Remove need for ppx preprocessing. + ## 0.1.0 (2024-09-03) ### Added diff --git a/dune-project b/dune-project index d2a31d5..368bc03 100644 --- a/dune-project +++ b/dune-project @@ -21,43 +21,18 @@ (depends (ocaml (>= 5.2)) - (ocamlformat - (and - :with-dev-setup - (= 0.26.2))) (base (and (>= v0.17) (< v0.18))) - (bisect_ppx - (and - :with-dev-setup - (>= 2.8.3))) (fpath (>= 0.7.3)) (fpath-sexp0 (= :version)) - (ppx_compare - (and - (>= v0.17) - (< v0.18))) (ppx_hash (and (>= v0.17) (< v0.18))) - (ppx_js_style - (and - :with-dev-setup - (>= v0.17) - (< v0.18))) - (ppx_sexp_conv - (and - (>= v0.17) - (< v0.18))) - (ppx_sexp_value - (and - (>= v0.17) - (< v0.18))) (ppxlib (>= 0.33)))) @@ -68,31 +43,8 @@ (depends (ocaml (>= 5.2)) - (ocamlformat - (and - :with-dev-setup - (= 0.26.2))) - (bisect_ppx - (and - :with-dev-setup - (>= 2.8.3))) (fpath (>= 0.7.3)) - (ppx_js_style - (and - :with-dev-setup - (>= v0.17) - (< v0.18))) - (ppx_sexp_conv - (and - (>= v0.17) - (< v0.18))) - (ppx_sexp_value - (and - (>= v0.17) - (< v0.18))) - (ppxlib - (>= 0.33)) (sexplib0 (and (>= v0.17) diff --git a/fpath-base.opam b/fpath-base.opam index 7a8376a..33013ac 100644 --- a/fpath-base.opam +++ b/fpath-base.opam @@ -10,16 +10,10 @@ bug-reports: "https://github.com/mbarbin/fpath-base/issues" depends: [ "dune" {>= "3.16"} "ocaml" {>= "5.2"} - "ocamlformat" {with-dev-setup & = "0.26.2"} "base" {>= "v0.17" & < "v0.18"} - "bisect_ppx" {with-dev-setup & >= "2.8.3"} "fpath" {>= "0.7.3"} "fpath-sexp0" {= version} - "ppx_compare" {>= "v0.17" & < "v0.18"} "ppx_hash" {>= "v0.17" & < "v0.18"} - "ppx_js_style" {with-dev-setup & >= "v0.17" & < "v0.18"} - "ppx_sexp_conv" {>= "v0.17" & < "v0.18"} - "ppx_sexp_value" {>= "v0.17" & < "v0.18"} "ppxlib" {>= "0.33"} "odoc" {with-doc} ] diff --git a/fpath-sexp0.opam b/fpath-sexp0.opam index 868947f..c598903 100644 --- a/fpath-sexp0.opam +++ b/fpath-sexp0.opam @@ -11,13 +11,7 @@ bug-reports: "https://github.com/mbarbin/fpath-base/issues" depends: [ "dune" {>= "3.16"} "ocaml" {>= "5.2"} - "ocamlformat" {with-dev-setup & = "0.26.2"} - "bisect_ppx" {with-dev-setup & >= "2.8.3"} "fpath" {>= "0.7.3"} - "ppx_js_style" {with-dev-setup & >= "v0.17" & < "v0.18"} - "ppx_sexp_conv" {>= "v0.17" & < "v0.18"} - "ppx_sexp_value" {>= "v0.17" & < "v0.18"} - "ppxlib" {>= "0.33"} "sexplib0" {>= "v0.17" & < "v0.18"} "odoc" {with-doc} ] diff --git a/lib/fpath_base/src/dune b/lib/fpath_base/src/dune index 9525c67..0c99604 100644 --- a/lib/fpath_base/src/dune +++ b/lib/fpath_base/src/dune @@ -8,9 +8,4 @@ (lint (pps ppx_js_style -check-doc-comments)) (preprocess - (pps - -unused-code-warnings=force - ppx_compare - ppx_hash - ppx_sexp_conv - ppx_sexp_value))) + (pps -unused-code-warnings=force ppx_hash))) diff --git a/lib/fpath_base/src/fpath_base.ml b/lib/fpath_base/src/fpath_base.ml index 676024a..f1207e2 100644 --- a/lib/fpath_base/src/fpath_base.ml +++ b/lib/fpath_base/src/fpath_base.ml @@ -25,8 +25,8 @@ module Relative_path = struct let hash_fold_t state t = String.hash_fold_t state (T.to_string t) end -module File_name = struct - module T = Fpath_sexp0.File_name +module Fpart = struct + module T = Fpath_sexp0.Fpart include T include Comparable.Make (T) diff --git a/lib/fpath_base/src/fpath_base.mli b/lib/fpath_base/src/fpath_base.mli index 3b4f3ef..cb40a47 100644 --- a/lib/fpath_base/src/fpath_base.mli +++ b/lib/fpath_base/src/fpath_base.mli @@ -19,9 +19,9 @@ module Relative_path : sig include Comparable.S with type t := t end -module File_name : sig - type t = Fpath_sexp0.File_name.t [@@deriving hash] +module Fpart : sig + type t = Fpath_sexp0.Fpart.t [@@deriving hash] - include module type of Fpath_sexp0.File_name with type t := t + include module type of Fpath_sexp0.Fpart with type t := t include Comparable.S with type t := t end diff --git a/lib/fpath_base/test/test__absolute_path.ml b/lib/fpath_base/test/test__absolute_path.ml index 29e1760..64b85a5 100644 --- a/lib/fpath_base/test/test__absolute_path.ml +++ b/lib/fpath_base/test/test__absolute_path.ml @@ -88,11 +88,11 @@ let%expect_test "append" = let%expect_test "extend" = let abs = Absolute_path.v in - let file str = str |> File_name.v in + let file str = str |> Fpart.v in let test a b = print_s [%sexp (Absolute_path.extend a b : Absolute_path.t)] in - require_does_raise [%here] (fun () : File_name.t -> file "a/b"); - [%expect {| (Invalid_argument "a/b: invalid file name") |}]; - require_does_not_raise [%here] (fun () -> ignore (file ".." : File_name.t)); + require_does_raise [%here] (fun () : Fpart.t -> file "a/b"); + [%expect {| (Invalid_argument "a/b: invalid file part") |}]; + require_does_not_raise [%here] (fun () -> ignore (file ".." : Fpart.t)); [%expect {||}]; test (abs "/") (file "a"); [%expect {| /a |}]; @@ -288,3 +288,35 @@ let%expect_test "relativize" = [%expect {| /foo/bar |}]; () ;; + +let%expect_test "hashtbl" = + let t = Hashtbl.create (module Absolute_path) in + Hashtbl.set t ~key:(Absolute_path.v "/tmp/my-file") ~data:42; + print_s [%sexp (t : int Hashtbl.M(Absolute_path).t)]; + [%expect {| ((/tmp/my-file 42)) |}] +;; + +module Pair = struct + [@@@coverage off] + + type t = + { a : Absolute_path.t + ; b : Absolute_path.t + } + [@@deriving compare, hash, sexp_of] +end + +let%expect_test "hash-fold-t" = + let t = Hashtbl.create (module Pair) in + Hashtbl.set + t + ~key:{ a = Absolute_path.v "/tmp/a"; b = Absolute_path.v "/tmp/a" } + ~data:42; + print_s [%sexp (t : int Hashtbl.M(Pair).t)]; + [%expect {| + (( + ((a /tmp/a) + (b /tmp/a)) + 42)) + |}] +;; diff --git a/lib/fpath_base/test/test__file_name.ml b/lib/fpath_base/test/test__file_name.ml deleted file mode 100644 index eca8dd6..0000000 --- a/lib/fpath_base/test/test__file_name.ml +++ /dev/null @@ -1,33 +0,0 @@ -let%expect_test "of_string" = - let test str = - print_s [%sexp (File_name.of_string str : (File_name.t, [ `Msg of string ]) Result.t)] - in - test ""; - [%expect {| (Error (Msg ": invalid file name")) |}]; - test "a"; - [%expect {| (Ok a) |}]; - test ".a"; - [%expect {| (Ok .a) |}]; - test ".."; - [%expect {| (Ok ..) |}]; - test "/"; - [%expect {| (Error (Msg "/: invalid file name")) |}]; - test "a/b"; - [%expect {| (Error (Msg "a/b: invalid file name")) |}]; - test "a\000b"; - [%expect {| (Error (Msg "a\000b: invalid file name")) |}]; - () -;; - -let%expect_test "hard coded" = - List.iter - ~f:(fun (name, t) -> - print_endline (Printf.sprintf "%10s: " name ^ File_name.to_string t)) - File_name.[ "dot", dot; "dot_dot", dot_dot; "dot_git", dot_git; "dot_hg", dot_hg ]; - [%expect {| - dot: . - dot_dot: .. - dot_git: .git - dot_hg: .hg |}]; - () -;; diff --git a/lib/fpath_base/test/test__fpart.ml b/lib/fpath_base/test/test__fpart.ml new file mode 100644 index 0000000..6f7514f --- /dev/null +++ b/lib/fpath_base/test/test__fpart.ml @@ -0,0 +1,61 @@ +let%expect_test "of_string" = + let test str = + print_s [%sexp (Fpart.of_string str : (Fpart.t, [ `Msg of string ]) Result.t)] + in + test ""; + [%expect {| (Error (Msg ": invalid file part")) |}]; + test "a"; + [%expect {| (Ok a) |}]; + test ".a"; + [%expect {| (Ok .a) |}]; + test ".."; + [%expect {| (Ok ..) |}]; + test "/"; + [%expect {| (Error (Msg "/: invalid file part")) |}]; + test "a/b"; + [%expect {| (Error (Msg "a/b: invalid file part")) |}]; + test "a\000b"; + [%expect {| (Error (Msg "a\000b: invalid file part")) |}]; + () +;; + +let%expect_test "hard coded" = + List.iter + ~f:(fun (name, t) -> print_endline (Printf.sprintf "%10s: " name ^ Fpart.to_string t)) + Fpart.[ "dot", dot; "dot_dot", dot_dot; "dot_git", dot_git; "dot_hg", dot_hg ]; + [%expect {| + dot: . + dot_dot: .. + dot_git: .git + dot_hg: .hg |}]; + () +;; + +let%expect_test "hashtbl" = + let t = Hashtbl.create (module Fpart) in + Hashtbl.set t ~key:(Fpart.v "my-file") ~data:42; + print_s [%sexp (t : int Hashtbl.M(Fpart).t)]; + [%expect {| ((my-file 42)) |}] +;; + +module Pair = struct + [@@@coverage off] + + type t = + { a : Fpart.t + ; b : Fpart.t + } + [@@deriving compare, hash, sexp_of] +end + +let%expect_test "hash-fold-t" = + let t = Hashtbl.create (module Pair) in + Hashtbl.set t ~key:{ a = Fpart.v "a"; b = Fpart.v "b" } ~data:42; + print_s [%sexp (t : int Hashtbl.M(Pair).t)]; + [%expect {| + (( + ((a a) + (b b)) + 42)) + |}] +;; diff --git a/lib/fpath_base/test/test__file_name.mli b/lib/fpath_base/test/test__fpart.mli similarity index 100% rename from lib/fpath_base/test/test__file_name.mli rename to lib/fpath_base/test/test__fpart.mli diff --git a/lib/fpath_base/test/test__relative_path.ml b/lib/fpath_base/test/test__relative_path.ml index 3fe5d5b..ab2ed4e 100644 --- a/lib/fpath_base/test/test__relative_path.ml +++ b/lib/fpath_base/test/test__relative_path.ml @@ -105,11 +105,11 @@ let%expect_test "append" = let%expect_test "extend" = let rel = Relative_path.v in - let file str = str |> File_name.v in + let file str = str |> Fpart.v in let test a b = print_s [%sexp (Relative_path.extend a b : Relative_path.t)] in - require_does_raise [%here] (fun () : File_name.t -> file "a/b"); - [%expect {| (Invalid_argument "a/b: invalid file name") |}]; - require_does_not_raise [%here] (fun () -> ignore (file ".." : File_name.t)); + require_does_raise [%here] (fun () : Fpart.t -> file "a/b"); + [%expect {| (Invalid_argument "a/b: invalid file part") |}]; + require_does_not_raise [%here] (fun () -> ignore (file ".." : Fpart.t)); [%expect {| |}]; test Relative_path.empty (file "a"); [%expect {| a |}]; @@ -155,7 +155,7 @@ let%expect_test "parent" = let%expect_test "of_list" = let test files = - let result = Relative_path.of_list (List.map files ~f:File_name.v) in + let result = Relative_path.of_list (List.map files ~f:Fpart.v) in print_s [%sexp (result : Relative_path.t)] in test []; @@ -291,3 +291,35 @@ let%expect_test "to_dir_path" = [%expect {| ./ |}]; () ;; + +let%expect_test "hashtbl" = + let t = Hashtbl.create (module Relative_path) in + Hashtbl.set t ~key:(Relative_path.v "path/to/my-file") ~data:42; + print_s [%sexp (t : int Hashtbl.M(Relative_path).t)]; + [%expect {| ((path/to/my-file 42)) |}] +;; + +module Pair = struct + [@@@coverage off] + + type t = + { a : Relative_path.t + ; b : Relative_path.t + } + [@@deriving compare, hash, sexp_of] +end + +let%expect_test "hash-fold-t" = + let t = Hashtbl.create (module Pair) in + Hashtbl.set + t + ~key:{ a = Relative_path.v "path/to/a"; b = Relative_path.v "path/to/b" } + ~data:42; + print_s [%sexp (t : int Hashtbl.M(Pair).t)]; + [%expect {| + (( + ((a path/to/a) + (b path/to/b)) + 42)) + |}] +;; diff --git a/lib/fpath_sexp0/src/dune b/lib/fpath_sexp0/src/dune index 2b44b13..1a25179 100644 --- a/lib/fpath_sexp0/src/dune +++ b/lib/fpath_sexp0/src/dune @@ -7,5 +7,4 @@ (backend bisect_ppx)) (lint (pps ppx_js_style -check-doc-comments)) - (preprocess - (pps -unused-code-warnings=force ppx_sexp_conv ppx_sexp_value))) + (preprocess no_preprocessing)) diff --git a/lib/fpath_sexp0/src/file_name.ml b/lib/fpath_sexp0/src/fpart.ml similarity index 95% rename from lib/fpath_sexp0/src/file_name.ml rename to lib/fpath_sexp0/src/fpart.ml index c451952..5e5d75c 100644 --- a/lib/fpath_sexp0/src/file_name.ml +++ b/lib/fpath_sexp0/src/fpart.ml @@ -12,7 +12,7 @@ let invariant t = let to_string t = t let of_string s = - if invariant s then Ok s else Error (`Msg (Printf.sprintf "%s: invalid file name" s)) + if invariant s then Ok s else Error (`Msg (Printf.sprintf "%s: invalid file part" s)) ;; let v t = diff --git a/lib/fpath_sexp0/src/file_name.mli b/lib/fpath_sexp0/src/fpart.mli similarity index 53% rename from lib/fpath_sexp0/src/file_name.mli rename to lib/fpath_sexp0/src/fpart.mli index 5e7fa3d..f72251b 100644 --- a/lib/fpath_sexp0/src/file_name.mli +++ b/lib/fpath_sexp0/src/fpart.mli @@ -1,19 +1,19 @@ -(** Basename of a File Path +(** Part of a file path. - A [File_name.t] represents the basename of a file path, i.e., the name of an - entry in the directory that contains it. It is not to be confused with a - full path. + A [Fpart.t] represents a segment of a file path, i.e., the parts of the + path that are separated by the directory separator character. For example, in the file path ["/home/user/documents/file.txt"], - ["file.txt"] is the file_name. + the parts are [["home" ; "user" ; "documents" ; "file.txt"]]. - A valid file_name cannot contain ['/'] or null characters. + A valid file part cannot contain ['/'] or null characters. - This module provides functions to convert between strings and file_names, - validate file_names, and some common file_names. *) + This module provides functions to convert between strings and file parts, + validate file parts, and some common file parts. *) -type t [@@deriving sexp_of] +type t +val sexp_of_t : t -> Sexplib0.Sexp.t val compare : t -> t -> int val equal : t -> t -> bool val of_string : string -> (t, [ `Msg of string ]) Result.t diff --git a/lib/fpath_sexp0/src/fpath0.mli b/lib/fpath_sexp0/src/fpath0.mli index 6a009f3..270952f 100644 --- a/lib/fpath_sexp0/src/fpath0.mli +++ b/lib/fpath_sexp0/src/fpath0.mli @@ -1,6 +1,7 @@ (** Adding to sexp serialization to [Fpath]. *) -type t = Fpath.t [@@deriving sexp_of] +type t = Fpath.t +val sexp_of_t : t -> Sexplib0.Sexp.t val compare : t -> t -> int val equal : t -> t -> bool diff --git a/lib/fpath_sexp0/src/fpath_sexp0.ml b/lib/fpath_sexp0/src/fpath_sexp0.ml index 3a608c2..f9035e3 100644 --- a/lib/fpath_sexp0/src/fpath_sexp0.ml +++ b/lib/fpath_sexp0/src/fpath_sexp0.ml @@ -6,4 +6,4 @@ end module Absolute_path = Path.Absolute_path module Relative_path = Path.Relative_path -module File_name = File_name +module Fpart = Fpart diff --git a/lib/fpath_sexp0/src/fpath_sexp0.mli b/lib/fpath_sexp0/src/fpath_sexp0.mli index 6d0074f..d7e0a0f 100644 --- a/lib/fpath_sexp0/src/fpath_sexp0.mli +++ b/lib/fpath_sexp0/src/fpath_sexp0.mli @@ -8,4 +8,4 @@ end module Absolute_path = Path.Absolute_path module Relative_path = Path.Relative_path -module File_name = File_name +module Fpart = Fpart diff --git a/lib/fpath_sexp0/src/path.ml b/lib/fpath_sexp0/src/path.ml index d9e106b..1f7b414 100644 --- a/lib/fpath_sexp0/src/path.ml +++ b/lib/fpath_sexp0/src/path.ml @@ -2,7 +2,7 @@ type absolute_path = Fpath.t type relative_path = Fpath.t let append a r = Fpath.(a // r) |> Fpath.normalize -let extend t f = Fpath.(t / File_name.to_string f) |> Fpath.normalize +let extend t f = Fpath.(t / Fpart.to_string f) |> Fpath.normalize let parent t = let t' = Fpath.normalize t |> Fpath.parent in diff --git a/lib/fpath_sexp0/src/path.mli b/lib/fpath_sexp0/src/path.mli index c6dc148..d13fdb0 100644 --- a/lib/fpath_sexp0/src/path.mli +++ b/lib/fpath_sexp0/src/path.mli @@ -6,8 +6,9 @@ type absolute_path = private Fpath.t type relative_path = private Fpath.t module Absolute_path : sig - type t = absolute_path [@@deriving sexp_of] + type t = absolute_path + val sexp_of_t : t -> Sexplib0.Sexp.t val compare : t -> t -> int val equal : t -> t -> bool val to_fpath : t -> Fpath.t @@ -27,7 +28,7 @@ module Absolute_path : sig val root : t val append : t -> relative_path -> t - val extend : t -> File_name.t -> t + val extend : t -> Fpart.t -> t val parent : t -> t option val chop_prefix : t -> prefix:t -> relative_path option val chop_suffix : t -> suffix:relative_path -> t option @@ -41,8 +42,9 @@ module Absolute_path : sig end module Relative_path : sig - type t = relative_path [@@deriving sexp_of] + type t = relative_path + val sexp_of_t : t -> Sexplib0.Sexp.t val compare : t -> t -> int val equal : t -> t -> bool val to_fpath : t -> Fpath.t @@ -64,9 +66,9 @@ module Relative_path : sig val empty : t val append : t -> t -> t - val extend : t -> File_name.t -> t + val extend : t -> Fpart.t -> t val parent : t -> t option - val of_list : File_name.t list -> t + val of_list : Fpart.t list -> t val chop_prefix : t -> prefix:t -> t option val chop_suffix : t -> suffix:t -> t option val is_dir_path : t -> bool