summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2025-11-16 17:54:51 -0600
committerJohn Estabrook <jestabro@vyos.io>2025-11-24 10:32:49 -0600
commitff9fb213f99b4730ae6042fa723eedb517a41bd9 (patch)
treedef2e10b2573f4811728144e462cc261f50d50d9 /src
parent2d58176886ca731c91484b0367083ad46f2ede14 (diff)
downloadvyos1x-config-ff9fb213f99b4730ae6042fa723eedb517a41bd9.tar.gz
vyos1x-config-ff9fb213f99b4730ae6042fa723eedb517a41bd9.zip
T8009: add allowed_edit_level to test necessary conditions on path
Diffstat (limited to 'src')
-rw-r--r--src/reference_tree.ml35
-rw-r--r--src/reference_tree.mli2
2 files changed, 37 insertions, 0 deletions
diff --git a/src/reference_tree.ml b/src/reference_tree.ml
index 205109f..afde451 100644
--- a/src/reference_tree.ml
+++ b/src/reference_tree.ml
@@ -646,6 +646,41 @@ let set_leaf_data rtree ctree path =
if is_leaf rtree refp then (Config_tree.set_leaf[@alert "-exn"]) ctree path true
else ctree
+let potential_tag_value rtree cpath =
+ (* check given path against reftree for potential to be tag value
+ *)
+ match cpath with
+ | [] | [_] -> false
+ | _ ->
+ let ref_drop_last = refpath rtree (Util.drop_last cpath) in
+ if is_tag rtree ref_drop_last then true
+ else false
+
+(* The 'edit' command can descend along a not-as-yet configured path,
+ assuming that it is
+ (1) a valid path of the reference tree
+ (2) neither a tag nor leaf node
+ To confirm (2) in the case of a tag node, one has to allow for a
+ 'potential' tag value as final element of the path.
+ *)
+let allowed_edit_level rtree path =
+ try
+ let refp = refpath rtree path
+ in
+ if Util.is_empty refp then
+ Error "The \"edit\" command cannot be issued at an empty path"
+ else
+ if is_tag rtree refp && not (potential_tag_value rtree path)
+ then
+ Error "The \"edit\" command cannot be issued at the level of tag node"
+ else
+ if is_leaf rtree refp
+ then
+ Error "The \"edit\" command cannot be issued at the level of leaf node"
+ else Ok ()
+ with Vytree.Nonexistent_path ->
+ Error "The \"edit\" command cannot be issued at a non-existent path of the reference tree"
+
let get_ceil_data f reftree path =
(* raises:
[Vytree.Empty_path]
diff --git a/src/reference_tree.mli b/src/reference_tree.mli
index 4c8072b..c2805ae 100644
--- a/src/reference_tree.mli
+++ b/src/reference_tree.mli
@@ -119,6 +119,8 @@ val set_leaf_data : t -> Config_tree.t -> string list -> Config_tree.t
[@@alert exn "Vytree.Empty_path"]
[@@alert exn "Vytree.Nonexistent_path"]
+val allowed_edit_level : t -> string list -> (unit, string) result
+
val get_ceil_data : (ref_node_data -> string option) -> t -> string list -> string option
[@@alert exn "Vytree.Empty_path"]
[@@alert exn "Vytree.Nonexistent_path"]