/**
* @since 1.0.0
*/
import type * as Cause from "effect/Cause"
import * as Channel from "effect/Channel"
import type * as Chunk from "effect/Chunk"
import { dual, pipe } from "effect/Function"
import type { ParseError } from "effect/ParseResult"
import * as Schema from "effect/Schema"
/**
* @since 1.0.0
* @category constructors
*/
export const encode = (
schema: Schema.Schema
) =>
(): Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
IE | ParseError,
IE,
Done,
Done,
R
> => {
const encode = Schema.encode(Schema.ChunkFromSelf(schema))
const loop: Channel.Channel, Chunk.Chunk, IE | ParseError, IE, Done, Done, R> = Channel
.readWithCause({
onInput: (input: Chunk.Chunk) =>
Channel.zipRight(
Channel.flatMap(encode(input), Channel.write),
loop
),
onFailure: (cause: Cause.Cause) => Channel.failCause(cause),
onDone: Channel.succeed
})
return loop
}
/**
* @since 1.0.0
* @category constructors
*/
export const encodeUnknown: (
schema: Schema.Schema
) => () => Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
IE | ParseError,
IE,
Done,
Done,
R
> = encode as any
/**
* @since 1.0.0
* @category constructors
*/
export const decode = (
schema: Schema.Schema
) =>
(): Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
ParseError | IE,
IE,
Done,
Done,
R
> => {
const decode = Schema.decode(Schema.ChunkFromSelf(schema))
const loop: Channel.Channel, Chunk.Chunk, ParseError | IE, IE, Done, Done, R> = Channel
.readWithCause({
onInput(chunk: Chunk.Chunk) {
return decode(chunk).pipe(
Channel.flatMap(Channel.write),
Channel.zipRight(loop)
)
},
onFailure(cause: Cause.Cause) {
return Channel.failCause(cause)
},
onDone(done: Done) {
return Channel.succeed(done)
}
})
return loop
}
/**
* @since 1.0.0
* @category constructors
*/
export const decodeUnknown: (
schema: Schema.Schema
) => () => Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
ParseError | IE,
IE,
Done,
Done,
R
> = decode as any
/**
* @since 1.0.0
* @category combinators
*/
export const duplex: {
/**
* @since 1.0.0
* @category combinators
*/
(
options: {
readonly inputSchema: Schema.Schema
readonly outputSchema: Schema.Schema
}
): (
self: Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
OutErr,
ParseError | InErr,
OutDone,
InDone,
R
>
) => Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
ParseError | OutErr,
InErr,
OutDone,
InDone,
R | IR | OR
>
/**
* @since 1.0.0
* @category combinators
*/
(
self: Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
OutErr,
ParseError | InErr,
OutDone,
InDone,
R
>,
options: {
readonly inputSchema: Schema.Schema
readonly outputSchema: Schema.Schema
}
): Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
ParseError | OutErr,
InErr,
OutDone,
InDone,
R | IR | OR
>
} = dual(2, (
self: Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
OutErr,
ParseError | InErr,
OutDone,
InDone,
R
>,
options: {
readonly inputSchema: Schema.Schema
readonly outputSchema: Schema.Schema
}
): Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
ParseError | OutErr,
InErr,
OutDone,
InDone,
R | IR | OR
> => {
const decode = Schema.decode(Schema.ChunkFromSelf(options.outputSchema))
return pipe(
encode(options.inputSchema)**
* @since 1.0.0
* @category combinators
*/
InErr, /**
* @since 1.0.0
* @category combinators
*/
InDone>(),
Channel.pipeTo(self),
Channel.mapOutEffect(decode)
);
})
/**
* @since 1.0.0
* @category combinators
*/
export const duplexUnknown: {
/**
* @since 1.0.0
* @category combinators
*/
(
options: {
readonly inputSchema: Schema.Schema
readonly outputSchema: Schema.Schema
}
): (
self: Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
OutErr,
ParseError | InErr,
OutDone,
InDone,
R
>
) => Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
ParseError | OutErr,
InErr,
OutDone,
InDone,
R | IR | OR
>
/**
* @since 1.0.0
* @category combinators
*/
(
self: Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
OutErr,
ParseError | InErr,
OutDone,
InDone,
R
>,
options: {
readonly inputSchema: Schema.Schema
readonly outputSchema: Schema.Schema
}
): Channel.Channel<
Chunk.Chunk,
Chunk.Chunk,
ParseError | OutErr,
InErr,
OutDone,
InDone,
R | IR | OR
>
} = duplex as any