Commit
Author: Kevin Schoon [kevinschoon@gmail.com]
Hash: dc77df9c0e1293d88600ca5e32fcb5756e3be140
Timestamp: Mon, 21 Jun 2021 20:01:07 +0000 (3 years ago)

+32 -5 +/-2 browse
add remove function for manifest
1diff --git a/lib/manifest.ml b/lib/manifest.ml
2index 1fa6d1f..a4a6253 100644
3--- a/lib/manifest.ml
4+++ b/lib/manifest.ml
5 @@ -102,6 +102,7 @@ let exists ~path manifest =
6 |> List.exists ~f:(fun item -> item |> to_path ~manifest |> String.equal path)
7
8 let find ~path manifest =
9+ (* find exactly one item, duplicates are unallowed *)
10 manifest.items
11 |> List.find ~f:(fun item ->
12 let file_path = item |> to_path ~manifest in
13 @@ -137,3 +138,20 @@ let insert ~path ~slug ~title ~description ~tags manifest =
14 else
15 let items = item :: manifest.items in
16 { items }
17+
18+ let remove ~path manifest =
19+ match manifest |> find ~path with
20+ | Some item ->
21+ let others = manifest |> list ~path:(item |> to_path ~manifest) in
22+ if Int.is_positive (List.length others) then
23+ failwith "will not delete recursively"
24+ else
25+ let items =
26+ manifest.items
27+ |> List.filter ~f:(fun item ->
28+ phys_equal
29+ (Filename.equal path (item |> to_path ~manifest))
30+ false)
31+ in
32+ { items }
33+ | None -> failwith "not found"
34 diff --git a/test/manifest_test.ml b/test/manifest_test.ml
35index 45ead24..5126c3c 100644
36--- a/test/manifest_test.ml
37+++ b/test/manifest_test.ml
38 @@ -16,7 +16,6 @@ let test_manifest () =
39 |> Manifest.insert ~path:"/fuu" ~slug:"note-00000000-1.md" ~title:"bar"
40 ~description:"" ~tags:[]
41 in
42- print_endline (Manifest.to_string manifest);
43 let result = manifest |> Manifest.find ~path:"/fuu/bar" in
44 Alcotest.(check bool) "manifest loaded" (result |> Option.is_some) true;
45 let result_path = Option.value_exn result |> Manifest.to_path ~manifest in
46 @@ -27,10 +26,20 @@ let test_manifest () =
47 ~description:"" ~tags:[]
48 in
49 let results = manifest |> Manifest.list ~path:"/fuu" in
50- Alcotest.(check int) "n_results" 2 (List.length results)
51+ Alcotest.(check int) "n_results" 2 (List.length results);
52+ let manifest =
53+ manifest
54+ |> Manifest.insert ~path:"/fuu/bar" ~slug:"note-00000000-3.md" ~title:"qux"
55+ ~description:"" ~tags:[]
56+ in
57+ let results = manifest |> Manifest.list ~path:"/fuu/bar" in
58+ Alcotest.(check int) "n_results" 1 (List.length results);
59+ Alcotest.(check int) "n_items" 4 (List.length manifest.items);
60+ print_endline (Manifest.to_string manifest);
61+ let manifest = manifest |> Manifest.remove ~path:"/fuu/bar/qux" in
62+ print_endline (Manifest.to_string manifest);
63+ Alcotest.(check int) "remove" 3 (List.length manifest.items)
64
65 let () =
66 Alcotest.run "Config"
67- [
68- ("load", [ Alcotest.test_case "test manifest" `Quick test_manifest ]);
69- ]
70+ [ ("load", [ Alcotest.test_case "test manifest" `Quick test_manifest ]) ]