summaryrefslogtreecommitdiff
path: root/model/pipeline/impl.ts
blob: 406a05e54a3a4e8181068f5a0b415413e0c82ec4 (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 './index.js';

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));
    }
}