UNPKG

alacritty-theme-switch

Version:
51 lines (50 loc) 1.97 kB
import * as dntShim from "../../_dnt.shims.js"; import { extname } from "../../deps/jsr.io/@std/path/1.1.4/extname.js"; import { parse } from "../../deps/jsr.io/@std/toml/1.0.11/parse.js"; import { fromPromise, fromThrowable, } from "neverthrow"; import { FileNotReadableError } from "./fs-errors.js"; import { TomlParseError, TomlStringifyError } from "./toml-errors.js"; import { stringify } from "../../deps/jsr.io/@std/toml/1.0.11/stringify.js"; /** * Check if the given path is a TOML file. */ export function isToml(path) { const ext = extname(path); // Handle edge case where filename is just ".toml" (hidden file with no extension) // In this case, extname returns "" but we should still consider it a TOML file if (ext === "" && path.endsWith(".toml")) { return true; } return ext === ".toml"; } /** * Safely parses TOML content from a string. * * @param content - TOML content as a string * @returns Result containing the parsed TOML object or a TomlParseError */ export function safeParseTomlContent(content) { const parseToml = fromThrowable(parse, (error) => new TomlParseError(content, { cause: error })); return parseToml(content); } /** * Safely reads and parses a TOML file. * * @param path - Path to the TOML file * @returns ResultAsync containing the parsed TOML content or an error * ``` */ export function safeParseToml(path) { const readContent = fromPromise(dntShim.Deno.readTextFile(path), (error) => new FileNotReadableError(path, { cause: error })); return readContent.andThen((content) => safeParseTomlContent(content)); } /** * Safely stringifies a TOML object. * * @param obj - TOML object to stringify * @returns Result containing the stringified TOML content or a TomlStringifyError */ export function safeStringifyToml(obj) { const stringifyToml = fromThrowable(stringify, (error) => new TomlStringifyError(obj, { cause: error })); return stringifyToml(obj); }