Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use result-type for return #223

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/io/upm-config-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { assertIsError } from "../utils/error-type-guards";
import { tryReadTextFromFile, tryWriteTextToFile } from "./file-io";
import { tryGetEnv } from "../utils/env-util";
import { tryGetHomePath } from "./home";
import { tryParseToml } from "../utils/data-parsing";

const configFileName = ".upmconfig.toml";

Expand Down Expand Up @@ -87,7 +88,7 @@ export const tryLoadUpmConfig = async (
// immediately caught, we should make this function return a result
// and use mapping.
const content = (await tryReadTextFromFile(configPath).promise).unwrap();
const config = TOML.parse(content);
const config = tryParseToml(content).unwrap();

// NOTE: We assume correct format
return config as UPMConfig;
Expand Down
12 changes: 11 additions & 1 deletion src/utils/data-parsing.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { AnyJson } from "@iarna/toml";
import TOML, { AnyJson, JsonMap } from "@iarna/toml";
import { Result } from "ts-results-es";

export type TomlParseError = Error;

/**
* Attempts to parse a json-string.
* @param json The string to be parsed.
*/
export function tryParseJson(json: string): Result<AnyJson, SyntaxError> {
return Result.wrap(() => JSON.parse(json));
}

/**
* Attempts to parse a toml-string.
* @param toml The string to be parsed.
*/
export function tryParseToml(toml: string): Result<JsonMap, TomlParseError> {
return Result.wrap(() => TOML.parse(toml));
}
Loading