summaryrefslogtreecommitdiff
path: root/model/pipeline/impl.ts
blob: 2e08d6e2da49a48f6bd328e857b869795c9f9e0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { Either, IEither } from '@emprespresso/pengueno';
import { isPipeline, Pipeline, PipelineStage } from '.';

export class PipelineImpl implements Pipeline {
    constructor(public readonly serialJobs: Array<PipelineStage>) {}

    public serialize() {
        return JSON.stringify(this.serialJobs);
    }

    public static from(s: string): IEither<Error, Pipeline> {
        return Either.fromFailable<Error, unknown>(() => JSON.parse(s))
            .flatMap<Pipeline>((eitherPipelineJson) =>
                isPipeline(eitherPipelineJson)
                    ? Either.right(eitherPipelineJson)
                    : Either.left(new Error('oh noes D: its a bad pipewine :((')),
            )
            .mapRight((pipeline) => new PipelineImpl(pipeline.serialJobs));
    }
}