result.ts 975 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright (c) 2017 molio 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> = Success<T> | 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. }
  16. export class Error {
  17. isError: true = true;
  18. toString() {
  19. if (this.line >= 0) {
  20. return `[Line ${this.line}] ${this.message}`;
  21. }
  22. return this.message;
  23. }
  24. constructor(
  25. public message: string,
  26. public line: number) {
  27. }
  28. }
  29. export class Success<T> {
  30. isError: false = false;
  31. constructor(public result: T, public warnings: string[]) { }
  32. }
  33. export default ReaderResult