moon_yazi

    A small zlib/DEFLATE implementation in MoonBit (ported from yazi).

    compression
    decompression
    deflate
    rfc1950
    rfc1951
    zlib
    Download zip
    Author
    Version
    0.1.1
    License
    Apache-2.0
    Last updated
    5 months ago
    Downloads
    41K

    #Milky2018/moon_yazi

    Yet another zlib implementation: RFC 1950 (zlib wrapper) + RFC 1951 (DEFLATE), ported from yazi-reference.

    #Install

    Add to your module dependencies, then import.

    #Quick Start

    Compress/decompress a buffer:

    let data = b"hello world"
    let compressed = compress(data, Format::Zlib, CompressionLevel::Default)
    let (decompressed, checksum) = decompress(Bytes::from_array(compressed), Format::Zlib)
    inspect(Bytes::from_array(decompressed) == data, content="true")
    inspect(checksum == Some(Adler32::from_buf(data).finish()), content="true")

    Streaming API:

    let out : Array[Byte] = []
    let encoder = Encoder::new()
    encoder.set_format(Format::Raw)
    let stream = encoder.stream_into_vec(out)
    stream.write(b"hello ")
    stream.write(b"world")
    stream.finish() |> ignore

    let decoder = Decoder::new()
    decoder.set_format(Format::Raw)
    let (plain, _) = decompress(Bytes::from_array(out), Format::Raw)
    inspect(Bytes::from_array(plain) == b"hello world", content="true")

    #Notes

    • Supports output sinks: stream_into_vec and stream_into_buf.
    • Does not currently provide a generic "write into any writer" sink (unlike the Rust crate's std::io::Write feature).

    YaziError

    type YaziError

    Errors for yazi (deflate/zlib).

    Ported from yazi/src/lib.rs (Apache-2.0 OR MIT).

    Note: upstream uses the type name Error, but MoonBit reserves Error. We use YaziError instead.

    YaziError::to_string

    fn YaziError::to_string(self : YaziError) -> String

    Adler32

    pub struct Adler32 {
    state : UInt
    }

    Rolling Adler-32 checksum.

    Ported from yazi/src/lib.rs (Apache-2.0 OR MIT).

    Adler32::finish

    fn Adler32::finish(self : Adler32) -> UInt

    Adler32::from_buf

    fn Adler32::from_buf(buf : Bytes) -> Adler32

    Adler32::new

    fn Adler32::new() -> Adler32

    Adler32::update

    fn Adler32::update(self : Adler32, buf : Bytes) -> Unit

    CompressionLevel

    pub(all) enum CompressionLevel {
    NoCompression
    BestSpeed
    Default
    BestSize
    Specific(UInt)
    }

    The level of compression-- a compromise between speed and size.

    CompressionStrategy

    pub(all) enum CompressionStrategy {
    Default
    RLE
    Filtered
    Static
    Huffman
    }

    Selects between various specialized compressor modes.

    Decoder

    type Decoder

    Stateful context for decompression.

    Decoder::boxed

    fn Decoder::boxed() -> Decoder

    Creates a new deflate decoder on the heap.

    For API parity with upstream yazi-reference, this is an alias of new() (our decoder context is heap-owned via Ref).

    Decoder::new

    fn Decoder::new() -> Decoder

    Decoder::set_format

    fn Decoder::set_format(self : Decoder, format : Format) -> Unit

    Sets the expected format of the input data for the next usage of the decoder.

    Decoder::stream_into_buf

    fn Decoder::stream_into_buf(self : Decoder, buf : Array[Byte]) -> DecoderBufStream

    Creates a decoder stream that will write into the specified buffer.

    Decoder::stream_into_vec

    fn Decoder::stream_into_vec(self : Decoder, vec : Array[Byte]) -> DecoderStream

    Creates a decoder stream that will append into the specified vector.

    DecoderBufStream

    type DecoderBufStream

    Decompression stream that writes into a fixed-size buffer.

    The buffer must be pre-sized. If it is too small to hold the decompressed output, YaziError::Overflow is raised.

    DecoderBufStream::decompressed_size

    fn DecoderBufStream::decompressed_size(self : DecoderBufStream) -> UInt64

    DecoderBufStream::finish

    fn DecoderBufStream::finish(self : DecoderBufStream) -> (UInt64, UInt?) raise YaziError

    DecoderBufStream::write

    fn DecoderBufStream::write(self : DecoderBufStream, buf : Bytes) -> Unit raise YaziError

    DecoderStream

    type DecoderStream

    Decompression stream combining a decoder context with an output.

    DecoderStream::decompressed_size

    fn DecoderStream::decompressed_size(self : DecoderStream) -> UInt64

    DecoderStream::finish

    fn DecoderStream::finish(self : DecoderStream) -> (UInt64, UInt?) raise YaziError

    Consumes the stream, flushing any input that may be buffered. Returns the decompressed byte count and an optional checksum if zlib encoded.

    DecoderStream::write

    fn DecoderStream::write(self : DecoderStream, buf : Bytes) -> Unit raise YaziError

    Encoder

    type Encoder

    Stateful context for compression.

    Encoder::boxed

    fn Encoder::boxed() -> Encoder

    Creates a new deflate encoder on the heap.

    For API parity with upstream yazi-reference, this is an alias of new() (our encoder context is heap-owned via Ref).

    Encoder::new

    fn Encoder::new() -> Encoder

    Encoder::set_format

    fn Encoder::set_format(self : Encoder, format : Format) -> Unit

    Sets the format of the output bitstream for the next usage of the encoder.

    Encoder::set_level

    fn Encoder::set_level(self : Encoder, level : CompressionLevel) -> Unit

    Sets the compression level for the next usage of the encoder.

    Encoder::set_strategy

    fn Encoder::set_strategy(self : Encoder, strategy : CompressionStrategy) -> Unit

    Sets the compression strategy for the next usage of the encoder.

    Encoder::stream_into_buf

    fn Encoder::stream_into_buf(self : Encoder, buf : Array[Byte]) -> EncoderBufStream

    Creates an encoder stream that will write into the specified buffer.

    Encoder::stream_into_vec

    fn Encoder::stream_into_vec(self : Encoder, vec : Array[Byte]) -> EncoderStream

    Creates an encoder stream that will append into the specified vector.

    EncoderBufStream

    type EncoderBufStream

    Compression stream that writes into a fixed-size buffer.

    The buffer must be pre-sized; if it's not large enough to hold the compressed output, YaziError::Overflow is raised.

    EncoderBufStream::compressed_size

    fn EncoderBufStream::compressed_size(self : EncoderBufStream) -> UInt64

    EncoderBufStream::finish

    fn EncoderBufStream::finish(self : EncoderBufStream) -> UInt64 raise YaziError

    EncoderBufStream::write

    fn EncoderBufStream::write(self : EncoderBufStream, buf : Bytes) -> Unit raise YaziError

    EncoderStream

    type EncoderStream

    Compression stream combining an encoder context with an output.

    EncoderStream::compressed_size

    fn EncoderStream::compressed_size(self : EncoderStream) -> UInt64

    Returns the number of compressed bytes that have been written to the output.

    EncoderStream::finish

    fn EncoderStream::finish(self : EncoderStream) -> UInt64 raise YaziError

    Consumes the stream, flushing any input that may be buffered and any remaining output. Returns the total number of compressed bytes written.

    EncoderStream::write

    fn EncoderStream::write(self : EncoderStream, buf : Bytes) -> Unit raise YaziError

    Writes the specified buffer to the stream, producing compressed data in the output.

    Format

    pub(all) enum Format {
    Raw
    Zlib
    }

    Bitstream format.

    Ported from yazi/src/lib.rs (Apache-2.0 OR MIT).

    compress

    fn compress(buf : Bytes, format : Format, level : CompressionLevel) -> Array[Byte] raise YaziError

    Compresses a buffer into a vector with the specified format and compression level.

    decompress

    fn decompress(buf : Bytes, format : Format) -> (Array[Byte], UInt?) raise YaziError

    Decompresses a buffer of the specified format into a vector.

    On success, returns the decompressed bytes and optionally an Adler-32 checksum if the source data was zlib encoded.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io