Skip to content

Commit

Permalink
Handle sub-conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
ltouroumov committed Jul 29, 2024
1 parent f427bcc commit 96478d5
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions composables/conditions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as R from 'ramda';
import { isEmpty, map } from 'ramda';
import { P, match } from 'ts-pattern';

import { ConditionTerm, HasRequirements } from '~/composables/project';
Expand All @@ -16,7 +17,6 @@ export type ConditionExec = {

export const buildConditions = (item: HasRequirements): Term => {
const { exec } = buildRootCondition(item.requireds);

return exec;
};

Expand All @@ -33,7 +33,8 @@ export const buildRootCondition = (terms: ConditionTerm[]): ConditionExec => {
};

const buildCondition = (term: ConditionTerm): Condition => {
return match(term)
// Compute the base condition microcode
const base = match(term)
.with({ type: 'id', required: true }, () => {
const ids = R.reject(R.isEmpty, [
term.reqId,
Expand All @@ -60,6 +61,14 @@ const buildCondition = (term: ConditionTerm): Condition => {
},
)
.otherwise(() => ALWAYS);

if (isEmpty(term.requireds)) {
return base;
} else {
// Handle sub-conditions
const inner = map(buildCondition, term.requireds);
return ALT(AND(inner), base, ALWAYS);
}
};

const mergeDeps = (terms: Condition[]) =>
Expand Down Expand Up @@ -109,3 +118,14 @@ const OR = (terms: Condition[]): Condition => {
terms,
);
};

const ALT = (
pred: Condition,
ifTrue: Condition,
ifFalse: Condition,
): Condition => {
return {
code: `(${pred.code}) ? (${ifTrue.code}) : (${ifFalse.code})`,
deps: mergeDeps([pred, ifTrue, ifFalse]),
};
};

0 comments on commit 96478d5

Please sign in to comment.