|
| 1 | +/* |
| 2 | + * Copyright Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Wire / storage form of entity status (lowercase). |
| 19 | + * |
| 20 | + * @public |
| 21 | + */ |
| 22 | +export type DcmEntityStatus = 'success' | 'running'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Canonical lowercase entity status values for DCM data (mock, API, DB). |
| 26 | + * Use {@link displayDcmEntityStatus} for user-visible labels in the UI. |
| 27 | + * |
| 28 | + * When adding a status: extend {@link DcmEntityStatus}, add an entry here, and add a branch in |
| 29 | + * {@link displayDcmEntityStatus} (`assertNever` catches missing switch cases). |
| 30 | + * |
| 31 | + * @public |
| 32 | + */ |
| 33 | +export const DCM_ENTITY_STATUS = { |
| 34 | + success: 'success', |
| 35 | + running: 'running', |
| 36 | +} as const satisfies Record<DcmEntityStatus, DcmEntityStatus>; |
| 37 | + |
| 38 | +/** All known {@link DcmEntityStatus} values (for guards / tests). @public */ |
| 39 | +export const DCM_ENTITY_STATUS_VALUES: readonly DcmEntityStatus[] = |
| 40 | + Object.freeze(Object.values(DCM_ENTITY_STATUS) as DcmEntityStatus[]); |
| 41 | + |
| 42 | +function assertNever(x: never): never { |
| 43 | + throw new Error(`Unexpected entity status: ${String(x)}`); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Human-readable label for UI (e.g. table cells). |
| 48 | + * @public |
| 49 | + */ |
| 50 | +export function displayDcmEntityStatus(status: DcmEntityStatus): string { |
| 51 | + switch (status) { |
| 52 | + case DCM_ENTITY_STATUS.success: |
| 53 | + return 'Success'; |
| 54 | + case DCM_ENTITY_STATUS.running: |
| 55 | + return 'Running'; |
| 56 | + default: |
| 57 | + return assertNever(status); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Parse wire / JSON values; returns `undefined` if not a known status. |
| 63 | + * @public |
| 64 | + */ |
| 65 | +export function parseDcmEntityStatus(raw: string): DcmEntityStatus | undefined { |
| 66 | + return (DCM_ENTITY_STATUS_VALUES as readonly string[]).includes(raw) |
| 67 | + ? (raw as DcmEntityStatus) |
| 68 | + : undefined; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Display for possibly unknown wire values — falls back to the raw string. |
| 73 | + * @public |
| 74 | + */ |
| 75 | +export function displayDcmEntityStatusLoose(raw: string): string { |
| 76 | + const parsed = parseDcmEntityStatus(raw); |
| 77 | + return parsed === undefined ? raw : displayDcmEntityStatus(parsed); |
| 78 | +} |
0 commit comments