euv-cli 0.50.0

Cross-platform CLI tool for the euv native UI framework supporting run, build, check, fmt, doctor, new, and Hyperlane SSE dynamic updates across all supported platforms.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use super::*;

/// LSB-first bitstream reader methods for RFC 1951 DEFLATE inflation.
impl<'a> DeflateBitReader<'a> {
    /// Creates a new bit reader positioned at the start of `bytes`.
    ///
    /// # Arguments
    ///
    /// - `&'a [u8]` - Compressed DEFLATE payload slice.
    ///
    /// # Returns
    ///
    /// - `Self` - Initialized bit reader.
    pub(crate) fn new(bytes: &'a [u8]) -> Self {
        Self {
            bytes,
            byte_pos: 0,
            bit_buf: 0,
            bit_count: 0,
        }
    }

    /// Ensures at least `needed` bits are buffered in `bit_buf`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit reader.
    /// - `u32` - Minimum number of bits needed.
    ///
    /// # Returns
    ///
    /// - `io::Result<()>` - Error if input ends before `needed` bits are available.
    pub(crate) fn ensure_bits(&mut self, needed: u32) -> io::Result<()> {
        let mut count: u32 = self.get_bit_count();
        let mut buf: u64 = self.get_bit_buf();
        let mut pos: usize = self.get_byte_pos();
        let bytes: &[u8] = self.get_bytes();
        while count < needed {
            let next_byte: u8 = *bytes
                .get(pos)
                .ok_or_else(|| Error::new(ErrorKind::UnexpectedEof, ERR_DEFLATE_INVALID))?;
            buf |= u64::from(next_byte) << count;
            pos += 1;
            count += 8;
        }
        self.set_bit_buf(buf);
        self.set_bit_count(count);
        self.set_byte_pos(pos);
        Ok(())
    }

    /// Reads `count` unsigned bits in LSB-first order.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit reader.
    /// - `u32` - Number of bits to read (`0..=32`).
    ///
    /// # Returns
    ///
    /// - `io::Result<u32>` - Decoded bit integer.
    pub(crate) fn read_bits(&mut self, count: u32) -> io::Result<u32> {
        if count == 0 {
            return Ok(0);
        }
        self.ensure_bits(count)?;
        let buf: u64 = self.get_bit_buf();
        let mask: u64 = (1u64 << count) - 1;
        let value: u32 = (buf & mask) as u32;
        self.set_bit_buf(buf >> count);
        self.set_bit_count(self.get_bit_count() - count);
        Ok(value)
    }

    /// Discards remaining bits in the current partial byte to align to the next byte boundary.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit reader.
    pub(crate) fn align_byte(&mut self) {
        let rem: u32 = self.get_bit_count() % 8;
        if rem > 0 {
            self.set_bit_buf(self.get_bit_buf() >> rem);
            self.set_bit_count(self.get_bit_count() - rem);
        }
    }

    /// Reads a byte-aligned little-endian `u16`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit reader.
    ///
    /// # Returns
    ///
    /// - `io::Result<u16>` - Decoded 16-bit integer.
    pub(crate) fn read_aligned_u16(&mut self) -> io::Result<u16> {
        let low: u32 = self.read_bits(8)?;
        let high: u32 = self.read_bits(8)?;
        Ok((low | (high << 8)) as u16)
    }

    /// Reads `len` byte-aligned raw bytes directly into `out`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit reader.
    /// - `usize` - Exact byte count to copy.
    /// - `&mut Vec<u8>` - Destination output buffer.
    ///
    /// # Returns
    ///
    /// - `io::Result<()>` - Error if insufficient bytes remain.
    pub(crate) fn read_aligned_bytes(&mut self, len: usize, out: &mut Vec<u8>) -> io::Result<()> {
        let mut remaining: usize = len;
        while remaining > 0 && self.get_bit_count() >= 8 {
            let byte_val: u8 = self.read_bits(8)? as u8;
            out.push(byte_val);
            remaining -= 1;
        }
        if remaining > 0 {
            let start: usize = self.get_byte_pos();
            let end: usize = start
                .checked_add(remaining)
                .ok_or_else(|| Error::new(ErrorKind::InvalidData, ERR_DEFLATE_INVALID))?;
            let slice: &[u8] = self
                .get_bytes()
                .get(start..end)
                .ok_or_else(|| Error::new(ErrorKind::UnexpectedEof, ERR_DEFLATE_INVALID))?;
            out.extend_from_slice(slice);
            self.set_byte_pos(end);
        }
        Ok(())
    }

    /// Returns the input byte slice reference.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit reader.
    ///
    /// # Returns
    ///
    /// - `&'a [u8]` - Input bytes.
    fn get_bytes(&self) -> &'a [u8] {
        self.bytes
    }

    /// Returns the current byte cursor index.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit reader.
    ///
    /// # Returns
    ///
    /// - `usize` - Byte cursor index.
    fn get_byte_pos(&self) -> usize {
        self.byte_pos
    }

    /// Updates the current byte cursor index.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit reader.
    /// - `usize` - New byte cursor index.
    fn set_byte_pos(&mut self, value: usize) {
        self.byte_pos = value;
    }

    /// Returns the current bit buffer value.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit reader.
    ///
    /// # Returns
    ///
    /// - `u64` - Buffered bits.
    fn get_bit_buf(&self) -> u64 {
        self.bit_buf
    }

    /// Updates the bit buffer value.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit reader.
    /// - `u64` - New bit buffer value.
    fn set_bit_buf(&mut self, value: u64) {
        self.bit_buf = value;
    }

    /// Returns the number of valid low bits in the buffer.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit reader.
    ///
    /// # Returns
    ///
    /// - `u32` - Buffered bit count.
    fn get_bit_count(&self) -> u32 {
        self.bit_count
    }

    /// Updates the buffered bit count.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit reader.
    /// - `u32` - New buffered bit count.
    fn set_bit_count(&mut self, value: u32) {
        self.bit_count = value;
    }
}

/// LSB-first bitstream writer methods for RFC 1951 DEFLATE compression.
impl DeflateBitWriter {
    /// Creates an empty bit writer with preallocated byte capacity.
    ///
    /// # Arguments
    ///
    /// - `usize` - Initial byte capacity hint.
    ///
    /// # Returns
    ///
    /// - `Self` - Initialized bit writer.
    pub(crate) fn with_capacity(capacity: usize) -> Self {
        Self {
            out: Vec::with_capacity(capacity),
            bit_buf: 0,
            bit_count: 0,
        }
    }

    /// Writes `count` low bits of `value` in LSB-first order.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit writer.
    /// - `u32` - Bit value to emit.
    /// - `u32` - Number of low bits to write.
    pub(crate) fn write_bits(&mut self, value: u32, count: u32) {
        let mut buf: u64 = self.get_bit_buf() | (u64::from(value) << self.get_bit_count());
        let mut bit_count: u32 = self.get_bit_count() + count;
        while bit_count >= 8 {
            let byte_val: u8 = (buf & 0xFF) as u8;
            self.get_mut_out().push(byte_val);
            buf >>= 8;
            bit_count -= 8;
        }
        self.set_bit_buf(buf);
        self.set_bit_count(bit_count);
    }

    /// Writes `count` bits of `code` in MSB-first order (as required for Huffman codes).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit writer.
    /// - `u32` - Canonical Huffman codeword.
    /// - `u32` - Codeword bit length.
    pub(crate) fn write_huffman_bits(&mut self, code: u32, count: u32) {
        let mut reversed: u32 = 0;
        let mut bit_idx: u32 = 0;
        while bit_idx < count {
            let bit: u32 = (code >> (count - 1 - bit_idx)) & 1;
            reversed |= bit << bit_idx;
            bit_idx += 1;
        }
        self.write_bits(reversed, count);
    }

    /// Flushes any trailing partial byte padded with zero bits and returns the output bytes.
    ///
    /// # Arguments
    ///
    /// - `Self` - Consumed bit writer.
    ///
    /// # Returns
    ///
    /// - `Vec<u8>` - Complete encoded DEFLATE byte stream.
    pub(crate) fn finish(mut self) -> Vec<u8> {
        if self.get_bit_count() > 0 {
            let byte_val: u8 = (self.get_bit_buf() & 0xFF) as u8;
            self.get_mut_out().push(byte_val);
            self.set_bit_buf(0);
            self.set_bit_count(0);
        }
        self.get_owned_out()
    }

    /// Returns a mutable reference to the output buffer.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit writer.
    ///
    /// # Returns
    ///
    /// - `&mut Vec<u8>` - Mutable output byte vector.
    fn get_mut_out(&mut self) -> &mut Vec<u8> {
        &mut self.out
    }

    /// Consumes the writer and returns the owned output buffer.
    ///
    /// # Arguments
    ///
    /// - `Self` - Consumed bit writer.
    ///
    /// # Returns
    ///
    /// - `Vec<u8>` - Owned output byte vector.
    fn get_owned_out(self) -> Vec<u8> {
        self.out
    }

    /// Returns the buffered bits.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit writer.
    ///
    /// # Returns
    ///
    /// - `u64` - Buffered bits.
    fn get_bit_buf(&self) -> u64 {
        self.bit_buf
    }

    /// Updates the buffered bits.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit writer.
    /// - `u64` - New buffered bits.
    fn set_bit_buf(&mut self, value: u64) {
        self.bit_buf = value;
    }

    /// Returns the buffered bit count.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit writer.
    ///
    /// # Returns
    ///
    /// - `u32` - Buffered bit count.
    fn get_bit_count(&self) -> u32 {
        self.bit_count
    }

    /// Updates the buffered bit count.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Active bit writer.
    /// - `u32` - New buffered bit count.
    fn set_bit_count(&mut self, value: u32) {
        self.bit_count = value;
    }
}

/// Canonical Huffman table construction and symbol decoding for RFC 1951 inflation.
impl HuffmanDecoder {
    /// Builds a canonical Huffman decoder from symbol bit lengths.
    ///
    /// # Arguments
    ///
    /// - `&[u8]` - Slice of code bit lengths indexed by symbol value.
    ///
    /// # Returns
    ///
    /// - `io::Result<Self>` - Validated canonical decoder table.
    pub(crate) fn from_lengths(lengths: &[u8]) -> io::Result<Self> {
        let mut counts: [u16; DEFLATE_MAX_BITS + 1] = [0u16; DEFLATE_MAX_BITS + 1];
        for &bit_len in lengths.iter() {
            let len_idx: usize = usize::from(bit_len);
            if len_idx > DEFLATE_MAX_BITS {
                return Err(Error::new(ErrorKind::InvalidData, ERR_DEFLATE_INVALID));
            }
            if len_idx > 0 {
                counts[len_idx] = counts[len_idx]
                    .checked_add(1)
                    .ok_or_else(|| Error::new(ErrorKind::InvalidData, ERR_DEFLATE_INVALID))?;
            }
        }
        let mut offsets: [usize; DEFLATE_MAX_BITS + 1] = [0usize; DEFLATE_MAX_BITS + 1];
        let mut total: usize = 0;
        let mut bits: usize = 1;
        while bits <= DEFLATE_MAX_BITS {
            offsets[bits] = total;
            total += usize::from(counts[bits]);
            bits += 1;
        }
        let mut symbols: Vec<u16> = vec![0u16; total];
        for (sym_idx, &bit_len) in lengths.iter().enumerate() {
            let len_idx: usize = usize::from(bit_len);
            if len_idx > 0 {
                let slot: usize = offsets[len_idx];
                symbols[slot] = sym_idx as u16;
                offsets[len_idx] = slot + 1;
            }
        }
        Ok(Self { counts, symbols })
    }

    /// Decodes the next Huffman symbol from `reader`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Canonical Huffman decoder.
    /// - `&mut DeflateBitReader<'_>` - Active input bit reader.
    ///
    /// # Returns
    ///
    /// - `io::Result<u16>` - Decoded symbol value.
    pub(crate) fn decode(&self, reader: &mut DeflateBitReader<'_>) -> io::Result<u16> {
        let counts: &[u16; DEFLATE_MAX_BITS + 1] = self.get_counts();
        let symbols: &[u16] = self.get_symbols();
        let mut code: i32 = 0;
        let mut first: i32 = 0;
        let mut index: i32 = 0;
        let mut bits: usize = 1;
        while bits <= DEFLATE_MAX_BITS {
            let next_bit: i32 = reader.read_bits(1)? as i32;
            code |= next_bit;
            let count: i32 = i32::from(counts[bits]);
            if code - count < first {
                let sym_pos: usize = (index + (code - first)) as usize;
                return symbols
                    .get(sym_pos)
                    .copied()
                    .ok_or_else(|| Error::new(ErrorKind::InvalidData, ERR_DEFLATE_INVALID));
            }
            index += count;
            first += count;
            first <<= 1;
            code <<= 1;
            bits += 1;
        }
        Err(Error::new(ErrorKind::InvalidData, ERR_DEFLATE_INVALID))
    }

    /// Returns the symbol count per bit-length table.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Canonical Huffman decoder.
    ///
    /// # Returns
    ///
    /// - `&[u16; DEFLATE_MAX_BITS + 1]` - Bit-length count array.
    fn get_counts(&self) -> &[u16; DEFLATE_MAX_BITS + 1] {
        &self.counts
    }

    /// Returns the canonically sorted symbol table.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Canonical Huffman decoder.
    ///
    /// # Returns
    ///
    /// - `&[u16]` - Sorted symbol slice.
    fn get_symbols(&self) -> &[u16] {
        &self.symbols
    }
}