result.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * from https://github.com/dsehnal/CIFTools.js
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. type ReaderResult<T> = ReaderResult.Success<T> | ReaderResult.Error
  8. namespace ReaderResult {
  9. export function error<T>(message: string, line = -1): ReaderResult<T> {
  10. return new Error(message, line);
  11. }
  12. export function success<T>(result: T, warnings: string[] = []): ReaderResult<T> {
  13. return new Success<T>(result, warnings);
  14. }
  15. export class Error {
  16. isError: true = true;
  17. toString() {
  18. if (this.line >= 0) {
  19. return `[Line ${this.line}] ${this.message}`;
  20. }
  21. return this.message;
  22. }
  23. constructor(
  24. public message: string,
  25. public line: number) {
  26. }
  27. }
  28. export class Success<T> {
  29. isError: false = false;
  30. constructor(public result: T, public warnings: string[]) { }
  31. }
  32. }
  33. export { ReaderResult };