Skip to content

Commit

Permalink
Strengthen Node.of_path correctness.
Browse files Browse the repository at this point in the history
This commit strengthens the construction of a node via
`Node.of_path` by checking the node invariants are not
violated at every stage of the node construction.
  • Loading branch information
zoj613 committed Jun 29, 2024
1 parent a597a97 commit 4c14ca0
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/node.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ let rep_ok name =

let root = Root

let unsafe_create p n = Cons (p, n)

let create parent name =
if rep_ok name then
Ok (unsafe_create parent name)
Result.ok @@ Cons (parent, name)
else
Error (`Node_invariant_error name)

Expand All @@ -35,10 +33,10 @@ let of_path = function
Result.error @@
`Node_invariant_error "path should not end with a /"
else
Result.ok @@
let open Util.Result_syntax in
List.fold_left
unsafe_create
Root (List.tl @@ String.split_on_char '/' str)
(fun acc n -> acc >>= fun p -> create p n)
(Ok Root) (List.tl @@ String.split_on_char '/' str)

let name = function
| Root -> ""
Expand Down

0 comments on commit 4c14ca0

Please sign in to comment.