~stacyharper/bonsai

7983d1087c249d280575ffe7ad140811658ce013 — Stacy Harper 4 months ago 7411ddc v0.1.0
add a hole type of branch
M bonsai/tree/json_load.ha => bonsai/tree/json_load.ha +15 -0
@@ 122,6 122,19 @@ fn load_tree_branch_exec(object: json::object) (branch | error) = {
	};
};

fn load_tree_branch_hole(object: json::object) (branch | error) = {
	let branches = match (load_branch_childs(object)) {
	case let branches: []branch =>
		yield branches;
	case let err: error =>
		return err;
	};

	return branch_hole {
		branches = branches,
	};
};

fn load_tree_branch_context(object: json::object) (branch | error) = {
	let contexts = match(json::get(&object, "contexts")) {
	case let value: *json::value =>


@@ 196,6 209,8 @@ fn load_tree_branch(object: json::object) (branch | error) = {
		return load_tree_branch_delay(object);
	case "exec" =>
		return load_tree_branch_exec(object);
	case "hole" =>
		return load_tree_branch_hole(object);
	case =>
		return unsupported_type;
	};

M bonsai/tree/json_test.ha => bonsai/tree/json_test.ha +5 -1
@@ 30,9 30,13 @@ use strings;

	assert(subject.branches[0] is branch_event);
	let subject = subject.branches[0] as branch_event;
	assert(len(subject.branches) == 1);
	assert(len(subject.branches) == 3);
	assert(subject.event_name == "typical_event");

	assert(subject.branches[1] is branch_hole);
	let subject2 = subject.branches[1] as branch_hole;
	assert(len(subject2.branches) == 0);

	assert(subject.branches[0] is branch_delay);
	let subject = subject.branches[0] as branch_delay;
	assert(len(subject.branches) == 1);

M bonsai/tree/json_write.ha => bonsai/tree/json_write.ha +9 -0
@@ 68,6 68,15 @@ export fn write_tree(
				fmt::fprint(handle, ",\"branches\":")!;
				write_tree(handle, branch.branches);
			};
		case let branch: branch_hole =>
			fmt::fprintf(
				handle,
				"\"type\":\"hole\"",
			)!;
			if (0 < len(branch.branches)) {
				fmt::fprint(handle, ",\"branches\":")!;
				write_tree(handle, branch.branches);
			};
		};
		fmt::fprint(handle, "}")!;
		if (i < len(branches) - 1) {

M bonsai/tree/test_voyager.ha => bonsai/tree/test_voyager.ha +51 -0
@@ 495,3 495,54 @@ let tree6: []branch = [
	let subject = voyager.available_branches[0] as branch_event;
	assert(subject.event_name == "event1");
};

let tree7: []branch = [
	branch_event {
		event_name = "event1",
		branches = [
			branch_exec {
				command = [
					"sh",
					"-c",
					"exit 1"
				],
				...
			},
			branch_hole {
				branches = [
					branch_event {
						event_name = "target_1",
						...
					},
				]
			}
		]
	},
	branch_hole {
		branches = [
			branch_event {
				event_name = "target_unwanted",
				...
			},
		]
	},
];

@test fn voyager_forward_check_returned_init_test() void = {
	let voyager = voyager {
		available_branches = tree7,
		wait_pipes = void,
		...
	};

	voyager_forward(
		&voyager,
		voyager_action_event_received {
			event_name = "event1"
		}
	);

	assert(voyager.available_branches[0] is branch_event);
	let subject = voyager.available_branches[0] as branch_event;
	assert(subject.event_name == "target_1");
};

M bonsai/tree/type.ha => bonsai/tree/type.ha +5 -0
@@ 23,11 23,16 @@ export type branch_delay = struct {
	delay_duration: int,
};

export type branch_hole = struct {
	branches: []branch,
};

export type branch = (
	branch_event |
	branch_context |
	branch_exec |
	branch_delay |
	branch_hole |
);

export type voyager_action_event_received = struct {

M bonsai/tree/voyager.ha => bonsai/tree/voyager.ha +4 -0
@@ 91,6 91,10 @@ fn voyager_forward_available_branches(voyager: *voyager) void = {

				return voyager_forward_available_branches(voyager);
			};
		case let hole_branch: branch_hole =>
			voyager.available_branches = hole_branch.branches;

			return voyager_forward_available_branches(voyager);
		case branch =>
			continue;
		};

M fixtures/basic_tree.json => fixtures/basic_tree.json +15 -0
@@ 22,6 22,21 @@
                ]
              }
            ]
          },
          {
            "type": "hole"
          },
          {
            "type": "hole",
            "branches": [
              {
                "type": "exec",
                "command": [
                  "echo",
                  "boooh"
                ]
              }
            ]
          }
        ]
      },