Skip to content

Commit a0671be

Browse files
committed
Add doc for Result
1 parent 9ea34c5 commit a0671be

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

lib/init-action.js

Lines changed: 16 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/init-action.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ async function run(startedAt: Date) {
373373
logger,
374374
});
375375

376-
if (repositoryProperties.isError()) {
376+
if (repositoryProperties.isFailure()) {
377377
addDiagnostic(
378378
config,
379379
// Arbitrarily choose the first language. We could also choose all languages, but that
@@ -810,25 +810,25 @@ async function loadRepositoryProperties(
810810
"Skipping loading repository properties because the repository is owned by a user and " +
811811
"therefore cannot have repository properties.",
812812
);
813-
return Result.ok({});
813+
return Result.success({});
814814
}
815815

816816
if (!(await features.getValue(Feature.UseRepositoryProperties))) {
817817
logger.debug(
818818
"Skipping loading repository properties because the UseRepositoryProperties feature flag is disabled.",
819819
);
820-
return Result.ok({});
820+
return Result.success({});
821821
}
822822

823823
try {
824-
return Result.ok(
824+
return Result.success(
825825
await loadPropertiesFromApi(gitHubVersion, logger, repositoryNwo),
826826
);
827827
} catch (error) {
828828
logger.warning(
829829
`Failed to load repository properties: ${getErrorMessage(error)}`,
830830
);
831-
return Result.error(error);
831+
return Result.failure(error);
832832
}
833833
}
834834

src/util.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,32 +1293,42 @@ export function joinAtMost(
12931293
return array.join(separator);
12941294
}
12951295

1296+
/** A success result. */
12961297
type Success<T> = Result<T, never>;
1298+
/** A failure result. */
12971299
type Failure<E> = Result<never, E>;
12981300

1301+
/**
1302+
* A simple result type representing either a success or a failure.
1303+
*/
12991304
export class Result<T, E> {
13001305
private constructor(
13011306
private readonly _ok: boolean,
13021307
public readonly value: T | E,
13031308
) {}
13041309

1305-
static ok<T>(value: T): Success<T> {
1310+
/** A success result. */
1311+
static success<T>(value: T): Success<T> {
13061312
return new Result(true, value) as Success<T>;
13071313
}
13081314

1309-
static error<E>(error: E): Failure<E> {
1310-
return new Result(false, error) as Failure<E>;
1315+
/** A failure result. */
1316+
static failure<E>(value: E): Failure<E> {
1317+
return new Result(false, value) as Failure<E>;
13111318
}
13121319

1313-
isOk(): this is Success<T> {
1320+
/** Whether this result represents a success. */
1321+
isSuccess(): this is Success<T> {
13141322
return this._ok;
13151323
}
13161324

1317-
isError(): this is Failure<E> {
1325+
/** Whether this result represents a failure. */
1326+
isFailure(): this is Failure<E> {
13181327
return !this._ok;
13191328
}
13201329

1330+
/** Get the value if this is a success, or return the default value if this is a failure. */
13211331
orElse(defaultValue: T): T {
1322-
return this.isOk() ? this.value : defaultValue;
1332+
return this.isSuccess() ? this.value : defaultValue;
13231333
}
13241334
}

0 commit comments

Comments
 (0)