You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.3 KiB
78 lines
2.3 KiB
import type { EidosConfig } from './config-types'
|
|
import { clonePlain, isPlainRecord } from './utils'
|
|
import { EidosConfigDocumentError } from '../errors'
|
|
|
|
export const EIDOS_CONFIG_DOCUMENT_KIND = 'uix.eidos-config' as const
|
|
export const EIDOS_CONFIG_DOCUMENT_VERSION = 1 as const
|
|
|
|
export type EidosConfigDocumentKind = typeof EIDOS_CONFIG_DOCUMENT_KIND
|
|
export type EidosConfigDocumentVersion = typeof EIDOS_CONFIG_DOCUMENT_VERSION
|
|
|
|
export interface EidosConfigDocument {
|
|
readonly kind: EidosConfigDocumentKind
|
|
readonly version: EidosConfigDocumentVersion
|
|
readonly options: EidosConfig
|
|
}
|
|
|
|
export { EidosConfigDocumentError } from '../errors'
|
|
|
|
export function createEidosConfigDocument(options: EidosConfig): EidosConfigDocument {
|
|
return Object.freeze({
|
|
kind: EIDOS_CONFIG_DOCUMENT_KIND,
|
|
version: EIDOS_CONFIG_DOCUMENT_VERSION,
|
|
options: clonePlain(options)
|
|
})
|
|
}
|
|
|
|
export function parseEidosConfigDocument(json: string): EidosConfigDocument {
|
|
let input: unknown
|
|
|
|
try {
|
|
input = JSON.parse(json)
|
|
} catch {
|
|
throw new EidosConfigDocumentError('cannot parse Eidos config document JSON')
|
|
}
|
|
|
|
return readEidosConfigDocument(input)
|
|
}
|
|
|
|
export function readEidosConfigDocument(input: unknown): EidosConfigDocument {
|
|
if (!isPlainRecord(input)) {
|
|
throw new EidosConfigDocumentError('Eidos config document must be a plain object')
|
|
}
|
|
|
|
if (input.kind !== EIDOS_CONFIG_DOCUMENT_KIND) {
|
|
throw new EidosConfigDocumentError('unknown Eidos config document kind')
|
|
}
|
|
|
|
if (input.version !== EIDOS_CONFIG_DOCUMENT_VERSION) {
|
|
throw new EidosConfigDocumentError(
|
|
`unsupported Eidos config document version "${String(input.version)}"`
|
|
)
|
|
}
|
|
|
|
if (!isPlainRecord(input.options)) {
|
|
throw new EidosConfigDocumentError('Eidos config document options must be a plain object')
|
|
}
|
|
|
|
return createEidosConfigDocument(input.options as unknown as EidosConfig)
|
|
}
|
|
|
|
export function serializeEidosConfigDocument(document: EidosConfigDocument): string {
|
|
return JSON.stringify(readEidosConfigDocument(document))
|
|
}
|
|
|
|
export function isEidosConfigDocument(input: unknown): input is EidosConfigDocument {
|
|
try {
|
|
readEidosConfigDocument(input)
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
export function isEidosConfigDocumentLike(input: unknown): input is Record<string, unknown> {
|
|
return isPlainRecord(input) && input.kind === EIDOS_CONFIG_DOCUMENT_KIND
|
|
}
|
|
|