char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somrearrange libs into lib and extensions into extsf4a0f5b

main
2.0 KiB55 linesraw
1use avril_core::serde_v8::JsBuffer;
2use std::io;
3
4pub const CHUNK: usize = 64 * 1024;
5/// Portable per-operation segment limit: the POSIX minimum vector count.
6pub const MAX_SLICES: usize = 16;
7pub type Response<T> = (Option<T>, Option<(&'static str, String)>);
8
9pub fn response<T>(result: io::Result<T>) -> Response<T> {
10    match result {
11        Ok(value) => (Some(value), None),
12        Err(error) => {
13            let kind = match error.kind() {
14                io::ErrorKind::NotFound => "NotFound",
15                io::ErrorKind::PermissionDenied => "PermissionDenied",
16                io::ErrorKind::AlreadyExists => "AlreadyExists",
17                io::ErrorKind::DirectoryNotEmpty => "NotEmpty",
18                io::ErrorKind::CrossesDevices => "CrossDevice",
19                io::ErrorKind::NotADirectory => "NotADirectory",
20                io::ErrorKind::IsADirectory => "IsADirectory",
21                io::ErrorKind::InvalidInput => "InvalidInput",
22                io::ErrorKind::NotConnected => "BadResource",
23                io::ErrorKind::WouldBlock => "Busy",
24                io::ErrorKind::BrokenPipe => "BrokenPipe",
25                io::ErrorKind::Unsupported => "NotSupported",
26                _ => "IoError",
27            };
28            (None, Some((kind, error.to_string())))
29        }
30    }
31}
32
33/// Keeps a vector's leading bounded prefix; the rest reports as a partial
34/// write. Nonempty data is never skipped or reordered.
35pub fn bound_vectored(buffers: Vec<JsBuffer>) -> Vec<JsBuffer> {
36    let mut kept = Vec::new();
37    let mut total = 0;
38    for mut segment in buffers {
39        if kept.len() >= MAX_SLICES || total >= CHUNK {
40            break;
41        }
42        if segment.is_empty() {
43            continue;
44        }
45        let room = CHUNK - total;
46        if segment.len() > room {
47            let mut slice = segment.into_parts();
48            slice.truncate(room);
49            segment = JsBuffer::from_parts(slice);
50        }
51        total += segment.len();
52        kept.push(segment);
53    }
54    kept
55}