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 */
import { Transform } from 'stream';
import { GraphStreamJSON } from '../interfaces/GraphStreamJSON';
import { GraphStreamCommand } from '../enums/GraphStreamCommand';
import {
GraphStreamCommand,
translate_command
} from '../enums/GraphStreamCommand';
import { validate_name } from '../Helper';
interface Stringable {
@ -12,7 +15,7 @@ interface Stringable {
export class GraphStream extends Transform {
private _path: string[] = [];
private _state = '';
private _state: GraphStreamCommand | '' = '';
private _directional = false;
public get path (): string {
@ -24,7 +27,7 @@ export class GraphStream extends Transform {
}
private expect_state (instr: GraphStreamCommand): void {
const states = [];
const states: (GraphStreamCommand|'')[] = [];
if ([
'cug',
'cdg'
@ -50,9 +53,11 @@ export class GraphStream extends Transform {
break;
}
if (!states.includes (this._state)) {
throw new Error (`invalid state to execute command ${instr}
expected: ${states.join (', ')}
actual: ${this._state}`);
throw new Error (`invalid state to execute command ${
translate_command (instr)}
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 no-inline-comments */
export type GraphStreamCommand =
'cn'| // create node
'en'| // end node
'cug'| // create unordered graph
'cdg'| // create directional graph
'csg'| // create subgraph
'eg'| // end graph
'at'| // add attributes
'ce' // create edge
type GraphStreamCommand =
'cn'|
'en'|
'cug'|
'cdg'|
'csg'|
'eg'|
'at'|
'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 };