All files / platform/core/src/services/DisplaySetService compileSplitRules.ts

2.43% Statements 1/41
0% Branches 0/18
0% Functions 0/7
2.5% Lines 1/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178                                                                                277x                                                                                                                                                                                                                                                                                  
import { createDisplaySetSplitRules, materializeSplitRules } from '@cornerstonejs/metadata';
import type {
  CreateDisplaySetSplitRulesOptions,
  RawSplitRule,
  SplitRule,
  SplitRuleSet,
} from '@cornerstonejs/metadata';
 
/**
 * A `useMetadataDisplaySet.displaySetRules` entry: a `@cornerstonejs/metadata` raw
 * rule. A raw field can hold a function at a function place (a whole
 * `matches`, a `groupBy` entry, a `sortBy` entry, `attributes`, ...).
 *
 * A rule that `createDisplaySetSplitRules` has already compiled is not a valid
 * entry: its `series`, `compareInstances` and `customAttributes` fields are
 * not raw fields, so it does not compile again.
 */
export type SplitRuleCustomizationEntry = RawSplitRule;
 
/** A rule that did not compile, or a registry or an order that is not valid. */
export type SplitRuleCompileError = {
  /** The rule id; undefined when the registry or the order itself is invalid. */
  ruleId?: string;
  /** Why the compiler rejected the entry. */
  message: string;
};
 
/** What {@link compileSplitRules} returns. */
export type CompiledSplitRules = {
  /**
   * The compiled rules, in rule order. Every function field is wrapped so
   * that a run-time error is a {@link SplitRuleRunError}. When `errors` is not
   * empty, the rule set is incomplete and must not be used (SP-SAFE-7).
   */
  rules: SplitRuleSet;
  /** One entry for each rule that did not compile. Empty when every rule compiled. */
  errors: SplitRuleCompileError[];
};
 
/** The rule fields that hold behaviour; each is a function once compiled. */
const BEHAVIOUR_FIELDS = [
  'matches',
  'groupBy',
  'series',
  'compareInstances',
  'customAttributes',
] as const;
 
/**
 * The error a split rule function throws at run time, with the rule and the
 * field that failed. The display set service adds the series.
 *
 * `field` is a rule field name (`matches`, `series`,
 * `compareInstances`, `customAttributes`), or `groupBy[<index>]` for one part
 * of `groupBy`. `cause` holds the original error.
 */
export class SplitRuleRunError extends Error {
  readonly ruleId: string;
  readonly field: string;
  readonly cause: unknown;
 
  constructor(ruleId: string, field: string, cause: unknown) {
    const reason = cause instanceof Error ? cause.message : String(cause);
    super(`Split rule '${ruleId}' failed in '${field}': ${reason}`);
    this.name = 'SplitRuleRunError';
    this.ruleId = ruleId;
    this.field = field;
    this.cause = cause;
  }
}
 
/** Wraps one rule function so that an error it throws names the rule and the field. */
function wrapRuleFunction<F extends (...args: unknown[]) => unknown>(
  ruleId: string,
  field: string,
  fn: F
): F {
  return function (this: unknown, ...args: unknown[]) {
    try {
      return fn.apply(this, args);
    } catch (error) {
      Iif (error instanceof SplitRuleRunError) {
        throw error;
      }
      throw new SplitRuleRunError(ruleId, field, error);
    }
  } as F;
}
 
/**
 * Returns a copy of a compiled entry with every function field wrapped by
 * {@link wrapRuleFunction}. The engine and the display set factory call the
 * wrapped functions, so an error from any of them - `customAttributes`
 * included, which the factory calls - names the rule and the field.
 */
function wrapRuleFunctions(ruleId: string, entry: SplitRule): SplitRule {
  const wrapped: SplitRule = { ...entry };
  for (const field of BEHAVIOUR_FIELDS) {
    const value = entry[field];
    if (typeof value === 'function') {
      (wrapped as Record<string, unknown>)[field] = wrapRuleFunction(
        ruleId,
        field,
        value as (...args: unknown[]) => unknown
      );
    } else Iif (field === 'groupBy' && Array.isArray(value)) {
      wrapped.groupBy = value.map((part, index) =>
        typeof part === 'function' ? wrapRuleFunction(ruleId, `groupBy[${index}]`, part) : part
      );
    }
  }
  return wrapped;
}
 
/**
 * Compiles the `useMetadataDisplaySet` registry (`displaySetRules`) in the
 * order of `displaySetRuleOrder` into the `SplitRuleSet` the split engine
 * takes. A registry rule that the order does not name is not compiled.
 *
 * Every rule is compiled by the `@cornerstonejs/metadata`
 * `createDisplaySetSplitRules` - there is no OHIF rule language. `options`
 * supplies the context (the `metadataContext` customization) and the named
 * functions the rules reference. The compiler keeps a function at a function
 * place as is, so an entry that mixes functions with data goes through the
 * same compiler as raw data.
 *
 * Each rule is compiled on its own (SP-PIPE-3), so that `errors` names every
 * rule that fails - an unknown name, an invalid expression, an id of the order
 * that is not in the registry, an id that differs from the key, an unsafe id
 * such as `__proto__`. An id that occurs again in the order is the same rule:
 * it keeps its first place, with one warning, as in `materializeSplitRules`. No rule is dropped: a rule set
 * with an error must stop display set creation (SP-SAFE-7), because the rule
 * that failed can be the rule a clinician depends on. The display set service
 * does that stop.
 *
 * Every function field of a compiled rule is wrapped, so that an error at run
 * time is a {@link SplitRuleRunError} that names the rule and the field.
 */
export function compileSplitRules(
  registry: Record<string, SplitRuleCustomizationEntry>,
  order: readonly string[],
  options: CreateDisplaySetSplitRulesOptions = {}
): CompiledSplitRules {
  const rules: SplitRuleSet = [];
  const errors: SplitRuleCompileError[] = [];
  Iif (!registry || typeof registry !== 'object' || Array.isArray(registry)) {
    errors.push({
      message: 'the displaySetRules customization must be an object keyed by rule id',
    });
  }
  Iif (!Array.isArray(order)) {
    errors.push({
      message: 'the displaySetRuleOrder customization must be an array of rule ids',
    });
  }
  Iif (errors.length) {
    return { rules, errors };
  }
 
  const unique = [...new Set(order)];
  Iif (unique.length < order.length) {
    const repeated = [...new Set(order.filter((id, index) => order.indexOf(id) !== index))];
    console.warn(
      `displaySetRuleOrder: ${repeated.join(', ')} occurs more than once; the first place is used.`
    );
  }
  for (const id of unique) {
    try {
      // Checks the id against the registry: present, safe, equal to the key.
      const [rule] = materializeSplitRules(registry as Record<string, RawSplitRule>, [id]);
      rules.push(wrapRuleFunctions(id, createDisplaySetSplitRules([rule], options)[0]));
    } catch (error) {
      errors.push({ ruleId: String(id), message: (error as Error).message });
    }
  }
  return { rules, errors };
}