translate commands on error

This commit is contained in:
Timo Hocker 2020-05-08 09:44:37 +02:00
parent 6e53d8d260
commit 7a351b7dab
2 changed files with 37 additions and 15 deletions

View File

@ -2,7 +2,10 @@
/* eslint-disable no-inline-comments */ /* eslint-disable no-inline-comments */
import { Transform } from 'stream'; import { Transform } from 'stream';
import { GraphStreamJSON } from '../interfaces/GraphStreamJSON'; import { GraphStreamJSON } from '../interfaces/GraphStreamJSON';
import { GraphStreamCommand } from '../enums/GraphStreamCommand'; import {
GraphStreamCommand,
translate_command
} from '../enums/GraphStreamCommand';
import { validate_name } from '../Helper'; import { validate_name } from '../Helper';
interface Stringable { interface Stringable {
@ -12,7 +15,7 @@ interface Stringable {
export class GraphStream extends Transform { export class GraphStream extends Transform {
private _path: string[] = []; private _path: string[] = [];
private _state = ''; private _state: GraphStreamCommand | '' = '';
private _directional = false; private _directional = false;
public get path (): string { public get path (): string {
@ -24,7 +27,7 @@ export class GraphStream extends Transform {
} }
private expect_state (instr: GraphStreamCommand): void { private expect_state (instr: GraphStreamCommand): void {
const states = []; const states: (GraphStreamCommand|'')[] = [];
if ([ if ([
'cug', 'cug',
'cdg' 'cdg'
@ -50,9 +53,11 @@ export class GraphStream extends Transform {
break; break;
} }
if (!states.includes (this._state)) { if (!states.includes (this._state)) {
throw new Error (`invalid state to execute command ${instr} throw new Error (`invalid state to execute command ${
expected: ${states.join (', ')} translate_command (instr)}
actual: ${this._state}`); expected: ${states.map ((s) => translate_command (s))
.join (', ')}
actual: ${translate_command (this._state)}`);
} }
} }

View File

@ -1,11 +1,28 @@
/* eslint-disable line-comment-position */ /* eslint-disable line-comment-position */
/* eslint-disable no-inline-comments */ /* eslint-disable no-inline-comments */
export type GraphStreamCommand = type GraphStreamCommand =
'cn'| // create node 'cn'|
'en'| // end node 'en'|
'cug'| // create unordered graph 'cug'|
'cdg'| // create directional graph 'cdg'|
'csg'| // create subgraph 'csg'|
'eg'| // end graph 'eg'|
'at'| // add attributes 'at'|
'ce' // create edge 'ce'
function translate_command (cmd: GraphStreamCommand|''): string {
const translations = {
cn: 'create node',
en: 'end node',
cug: 'create unordered graph',
cdg: 'create directional graph',
csg: 'create subgraph',
eg: 'end graph',
at: 'attributes',
ce: 'create edge',
'': 'start'
};
return translations[cmd];
}
export { GraphStreamCommand, translate_command };