oc2_hlapi_v2 0.2.0

A client for the new version of OpenComputers II's HLAPI
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//! Shared test infrastructure.
//!
//! Everything here is in-memory (no sockets, no files), so the suite runs on every platform:
//!
//! * [`MemStream`]: one end of an in-memory duplex pipe implementing the crate's `Stream` trait,
//!   with real blocking / nonblocking / read-timeout semantics.
//! * [`Host`]: the other ends of the three HLAPI streams, i.e. the "VM host" side.
//! * [`serve`]: runs a scripted host on a thread, answering requests as they arrive.
//! * Builders for HLAPI messages, and a couple of device wrappers written the way a wrapper author
//!   would write them.
#![allow(dead_code)]

use std::collections::VecDeque;
use std::convert::Infallible;
use std::io::{self, Read, Write};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};

use oc2_hlapi_v2::client::{DeviceBase, HlapiClient, HlapiClientError};
use oc2_hlapi_v2::codecs::{BaseCodec, BaseStreams};
use oc2_hlapi_v2::messages::{BlobDescriptor, BlobMarker};
use oc2_hlapi_v2::uuid::Uuid;
use oc2_hlapi_v2::{Stream, Transport};
use serde_json::{Value, json};

/// Generous upper bound for "this must happen" waits. Only reached when a test is failing.
pub const LONG: Duration = Duration::from_secs(10);
/// How long to wait to be sure that something does *not* happen.
pub const SHORT: Duration = Duration::from_millis(150);

// ---------------------------------------------------------------------------------------------
// In-memory streams
// ---------------------------------------------------------------------------------------------

/// Which error kind a read timeout produces. Linux sockets report `WouldBlock`, Windows reports
/// `TimedOut`; the codec has to cope with both.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TimeoutKind {
    WouldBlock,
    TimedOut,
}

pub const KINDS: [TimeoutKind; 2] = [TimeoutKind::WouldBlock, TimeoutKind::TimedOut];

impl TimeoutKind {
    fn error(self) -> io::Error {
        match self {
            Self::WouldBlock => io::ErrorKind::WouldBlock.into(),
            Self::TimedOut => io::ErrorKind::TimedOut.into(),
        }
    }
}

#[derive(Default)]
struct PipeState {
    data: VecDeque<u8>,
    writer_gone: bool,
    reader_gone: bool,
}

#[derive(Default)]
struct Pipe {
    state: Mutex<PipeState>,
    cond: Condvar,
}

#[derive(Default)]
struct Config {
    nonblocking: bool,
    timeout: Option<Duration>,
}

/// A shared record of which stream was written to, in order.
pub type WriteLog = Arc<Mutex<Vec<String>>>;

/// One end of an in-memory duplex pipe.
///
/// Reads follow the semantics of the real streams the crate runs on: they return buffered data
/// first, then EOF (`Ok(0)`) once the other end is dropped, otherwise they block, fail with
/// `WouldBlock` (nonblocking mode) or fail with the configured timeout error kind (read timeout).
/// Writes fail with `BrokenPipe` once the other end is dropped.
pub struct MemStream {
    rx: Arc<Pipe>,
    tx: Arc<Pipe>,
    config: Mutex<Config>,
    kind: TimeoutKind,
    log: Option<(String, WriteLog)>,
}

impl std::fmt::Debug for MemStream {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MemStream").finish_non_exhaustive()
    }
}

/// Create a connected pair of streams.
pub fn duplex(kind: TimeoutKind) -> (MemStream, MemStream) {
    let a_to_b = Arc::new(Pipe::default());
    let b_to_a = Arc::new(Pipe::default());
    let make = |rx: &Arc<Pipe>, tx: &Arc<Pipe>| MemStream {
        rx: rx.clone(),
        tx: tx.clone(),
        config: Mutex::new(Config::default()),
        kind,
        log: None,
    };
    (make(&b_to_a, &a_to_b), make(&a_to_b, &b_to_a))
}

impl MemStream {
    /// Record every write to this stream in `log` under `label`.
    pub fn logged(mut self, label: &str, log: &WriteLog) -> Self {
        self.log = Some((label.to_owned(), log.clone()));
        self
    }

    /// Read at least one byte. `timeout`: `None` blocks, `Some(ZERO)` polls.
    pub fn recv(&self, buf: &mut [u8], timeout: Option<Duration>) -> io::Result<usize> {
        if buf.is_empty() {
            return Ok(0);
        }
        let deadline = timeout.map(|t| Instant::now() + t);
        let mut st = self.rx.state.lock().unwrap();
        loop {
            if !st.data.is_empty() {
                let n = buf.len().min(st.data.len());
                for slot in &mut buf[..n] {
                    *slot = st.data.pop_front().unwrap();
                }
                return Ok(n);
            }
            if st.writer_gone {
                return Ok(0);
            }
            match deadline {
                None => st = self.rx.cond.wait(st).unwrap(),
                Some(d) => {
                    let now = Instant::now();
                    if now >= d {
                        return Err(self.kind.error());
                    }
                    st = self.rx.cond.wait_timeout(st, d - now).unwrap().0;
                }
            }
        }
    }

    /// Write from a shared reference (so a [`Host`] can be used from several threads).
    pub fn send(&self, bytes: &[u8]) -> io::Result<()> {
        let mut st = self.tx.state.lock().unwrap();
        if st.reader_gone {
            return Err(io::ErrorKind::BrokenPipe.into());
        }
        st.data.extend(bytes);
        self.tx.cond.notify_all();
        drop(st);
        if let Some((label, log)) = &self.log {
            log.lock().unwrap().push(label.clone());
        }
        Ok(())
    }

    /// Everything that is currently readable, without waiting.
    pub fn available(&self) -> Vec<u8> {
        let mut st = self.rx.state.lock().unwrap();
        st.data.drain(..).collect()
    }

    /// Wait for exactly `n` bytes.
    pub fn read_n(&self, n: usize, timeout: Duration) -> io::Result<Vec<u8>> {
        let deadline = Instant::now() + timeout;
        let mut out = Vec::new();
        while out.len() < n {
            let mut buf = vec![0; n - out.len()];
            let left = deadline.saturating_duration_since(Instant::now());
            let got = self.recv(&mut buf, Some(left))?;
            if got == 0 {
                return Err(io::ErrorKind::UnexpectedEof.into());
            }
            out.extend_from_slice(&buf[..got]);
        }
        Ok(out)
    }

    /// Read one NUL-terminated frame (empty frames are skipped). `Ok(None)` on timeout, `Err` on
    /// EOF. `None` as the timeout waits forever.
    pub fn frame(&self, timeout: Option<Duration>) -> io::Result<Option<Vec<u8>>> {
        let deadline = timeout.map(|t| Instant::now() + t);
        let mut frame = Vec::new();
        loop {
            let left = deadline.map(|d| d.saturating_duration_since(Instant::now()));
            let mut byte = [0];
            match self.recv(&mut byte, left) {
                Ok(0) => return Err(io::ErrorKind::UnexpectedEof.into()),
                Ok(_) if byte[0] == 0 => {
                    if !frame.is_empty() {
                        return Ok(Some(frame));
                    }
                }
                Ok(_) => frame.push(byte[0]),
                Err(e)
                    if matches!(
                        e.kind(),
                        io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut
                    ) =>
                {
                    return Ok(None);
                }
                Err(e) => return Err(e),
            }
        }
    }

    /// Like [`frame`](Self::frame) but parsed as JSON.
    pub fn json(&self, timeout: Option<Duration>) -> io::Result<Option<Value>> {
        Ok(self
            .frame(timeout)?
            .map(|f| serde_json::from_slice(&f).expect("peer sent a frame that is not JSON")))
    }

    /// Write `{NUL}{json}{NUL}`.
    pub fn send_json(&self, value: &Value) {
        self.send(&framed(value)).unwrap();
    }
}

/// `NUL json NUL`, the way the host frames messages.
pub fn framed(value: &Value) -> Vec<u8> {
    let mut v = vec![0];
    v.extend(serde_json::to_vec(value).unwrap());
    v.push(0);
    v
}

impl Read for MemStream {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let (nonblocking, timeout) = {
            let c = self.config.lock().unwrap();
            (c.nonblocking, c.timeout)
        };
        if nonblocking {
            return self
                .recv(buf, Some(Duration::ZERO))
                .map_err(|_| io::ErrorKind::WouldBlock.into());
        }
        self.recv(buf, timeout)
    }
}

impl Write for MemStream {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.send(buf)?;
        Ok(buf.len())
    }
    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

impl Stream for MemStream {
    fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
        self.config.lock().unwrap().nonblocking = nonblocking;
        Ok(())
    }
    fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
        // Same contract as the std socket types
        if timeout == Some(Duration::ZERO) {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, "zero timeout"));
        }
        self.config.lock().unwrap().timeout = timeout;
        Ok(())
    }
}

impl Drop for MemStream {
    fn drop(&mut self) {
        self.rx.state.lock().unwrap().reader_gone = true;
        self.tx.state.lock().unwrap().writer_gone = true;
        self.rx.cond.notify_all();
        self.tx.cond.notify_all();
    }
}

// ---------------------------------------------------------------------------------------------
// Host side and transports
// ---------------------------------------------------------------------------------------------

/// The far end of the three HLAPI streams.
#[derive(Debug)]
pub struct Host {
    pub rpc: MemStream,
    pub blob: MemStream,
    pub event: MemStream,
}

impl Host {
    /// Send a framed message on the rpc stream.
    pub fn reply(&self, value: &Value) {
        self.rpc.send_json(value);
    }
    /// Send a framed message on the event stream.
    pub fn emit(&self, value: &Value) {
        self.event.send_json(value);
    }
    /// Send raw bytes on the blob stream.
    pub fn blob_out(&self, bytes: &[u8]) {
        self.blob.send(bytes).unwrap();
    }
}

/// A fresh set of connected streams: the codec side and the host side.
pub fn connection(kind: TimeoutKind) -> (BaseStreams<MemStream>, Host) {
    let (rpc_c, rpc_h) = duplex(kind);
    let (blob_c, blob_h) = duplex(kind);
    let (event_c, event_h) = duplex(kind);
    (
        BaseStreams {
            rpc: rpc_c,
            blob: blob_c,
            event: event_c,
        },
        Host {
            rpc: rpc_h,
            blob: blob_h,
            event: event_h,
        },
    )
}

type Opener = Box<dyn FnMut() -> io::Result<BaseStreams<MemStream>> + Send>;

/// A transport handing out in-memory connections.
pub struct MockTransport {
    next: Opener,
    pub opens: Arc<AtomicUsize>,
}

impl MockTransport {
    /// Hands out the given connections in order, then fails.
    pub fn sequence(conns: Vec<BaseStreams<MemStream>>) -> Self {
        let mut conns: VecDeque<_> = conns.into();
        Self::with(move || {
            conns
                .pop_front()
                .ok_or_else(|| io::Error::other("no more connections"))
        })
    }
    /// Calls `f` for every connection attempt.
    pub fn with(f: impl FnMut() -> io::Result<BaseStreams<MemStream>> + Send + 'static) -> Self {
        Self {
            next: Box::new(f),
            opens: Arc::new(AtomicUsize::new(0)),
        }
    }
}

impl Transport for MockTransport {
    type Streams = BaseStreams<MemStream>;
    type Error = io::Error;
    fn open_streams(&mut self) -> io::Result<Self::Streams> {
        self.opens.fetch_add(1, Ordering::SeqCst);
        (self.next)()
    }
}

pub type MockCodec = BaseCodec<MemStream, MockTransport>;

/// A codec on one connection, plus the host end of it.
pub fn codec(kind: TimeoutKind) -> (MockCodec, Host) {
    let (streams, host) = connection(kind);
    (
        BaseCodec::new(MockTransport::sequence(vec![streams])).unwrap(),
        host,
    )
}

/// A codec with `spares` further connections available for reconnecting. Returns the host ends of
/// all connections (the first is the one in use) and the open counter.
pub fn codec_with_spares(
    kind: TimeoutKind,
    spares: usize,
) -> (MockCodec, Vec<Host>, Arc<AtomicUsize>) {
    let mut streams = Vec::new();
    let mut hosts = Vec::new();
    for _ in 0..=spares {
        let (s, h) = connection(kind);
        streams.push(s);
        hosts.push(h);
    }
    let transport = MockTransport::sequence(streams);
    let opens = transport.opens.clone();
    (BaseCodec::new(transport).unwrap(), hosts, opens)
}

/// A codec whose write order across the rpc and blob streams is recorded.
pub fn codec_logged(kind: TimeoutKind) -> (MockCodec, Host, WriteLog) {
    let (streams, host) = connection(kind);
    let log = WriteLog::default();
    let streams = BaseStreams {
        rpc: streams.rpc.logged("rpc", &log),
        blob: streams.blob.logged("blob", &log),
        event: streams.event,
    };
    (
        BaseCodec::new(MockTransport::sequence(vec![streams])).unwrap(),
        host,
        log,
    )
}

// ---------------------------------------------------------------------------------------------
// Scripted host
// ---------------------------------------------------------------------------------------------

/// A request as the scripted host saw it.
#[derive(Clone, Debug)]
pub struct Seen {
    pub request: Value,
    /// Blob bytes that had been written by the time the request arrived.
    pub blobs: Vec<u8>,
}

/// A host running on its own thread, answering each request with the handler.
pub struct Server {
    pub host: Arc<Host>,
    seen: Arc<Mutex<Vec<Seen>>>,
    _thread: JoinHandle<()>,
}

impl Server {
    pub fn requests(&self) -> Vec<Value> {
        self.seen
            .lock()
            .unwrap()
            .iter()
            .map(|s| s.request.clone())
            .collect()
    }
    pub fn seen(&self) -> Vec<Seen> {
        self.seen.lock().unwrap().clone()
    }
    /// How many requests of the given `type` arrived.
    pub fn count(&self, typ: &str) -> usize {
        self.requests().iter().filter(|r| r["type"] == typ).count()
    }
    pub fn total(&self) -> usize {
        self.seen.lock().unwrap().len()
    }
}

/// Answer requests arriving on `host` until the codec side is dropped.
///
/// The handler gets the parsed request, the blob bytes that preceded it, and the host (to reply,
/// emit events, send blobs).
pub fn serve(host: Host, mut handler: impl FnMut(&Value, &[u8], &Host) + Send + 'static) -> Server {
    let host = Arc::new(host);
    let seen = Arc::new(Mutex::new(Vec::new()));
    let thread = {
        let host = host.clone();
        let seen = seen.clone();
        thread::spawn(move || {
            while let Ok(Some(raw)) = host.rpc.frame(None) {
                let request: Value = serde_json::from_slice(&raw).unwrap();
                let blobs = host.blob.available();
                seen.lock().unwrap().push(Seen {
                    request: request.clone(),
                    blobs: blobs.clone(),
                });
                handler(&request, &blobs, &host);
            }
        })
    };
    Server {
        host,
        seen,
        _thread: thread,
    }
}

/// A client talking to a scripted host.
pub fn client_serving(
    handler: impl FnMut(&Value, &[u8], &Host) + Send + 'static,
) -> (HlapiClient, Server) {
    client_serving_kind(TimeoutKind::WouldBlock, handler)
}

pub fn client_serving_kind(
    kind: TimeoutKind,
    handler: impl FnMut(&Value, &[u8], &Host) + Send + 'static,
) -> (HlapiClient, Server) {
    let (codec, host) = codec(kind);
    (HlapiClient::new(codec), serve(host, handler))
}

/// A client talking to a host that never answers (but can emit events).
pub fn client_silent() -> (HlapiClient, Server) {
    client_serving(|_, _, _| {})
}

/// Poll until `cond` holds, or panic after [`LONG`].
pub fn wait_until(what: &str, mut cond: impl FnMut() -> bool) {
    let deadline = Instant::now() + LONG;
    while !cond() {
        assert!(Instant::now() < deadline, "timed out waiting for {what}");
        thread::sleep(Duration::from_millis(2));
    }
}

/// Run `f` on a thread and fail instead of hanging if it takes longer than [`LONG`].
pub fn within<T: Send + 'static>(f: impl FnOnce() -> T + Send + 'static) -> T {
    let (tx, rx) = std::sync::mpsc::channel();
    thread::spawn(move || {
        let _ = tx.send(f());
    });
    rx.recv_timeout(LONG).expect("operation hung")
}

// ---------------------------------------------------------------------------------------------
// Message builders. `req` is the request being answered, its id is echoed.
// ---------------------------------------------------------------------------------------------

pub fn id(n: u128) -> Uuid {
    Uuid::from_u128(n)
}

fn with_id(req: &Value, mut v: Value) -> Value {
    if let Some(i) = req.get("id") {
        v["id"] = i.clone();
    }
    v
}

pub fn list_reply(req: &Value, gen_: u32, devices: &[(Uuid, &[&str])]) -> Value {
    let data: Vec<_> = devices
        .iter()
        .map(|(u, names)| json!({"deviceId": u, "typeNames": names}))
        .collect();
    with_id(req, json!({"type": "list", "gen": gen_, "data": data}))
}

pub fn methods_reply(req: &Value, gen_: u32, names: &[&str]) -> Value {
    let data: Vec<_> = names.iter().map(|n| json!({"name": n})).collect();
    with_id(req, json!({"type": "methods", "gen": gen_, "data": data}))
}

pub fn result_reply(req: &Value, gen_: u32, value: Value) -> Value {
    with_id(req, json!({"type": "result", "gen": gen_, "data": value}))
}

/// A result without a `data` field (a method returning nothing).
pub fn empty_result_reply(req: &Value, gen_: u32) -> Value {
    with_id(req, json!({"type": "result", "gen": gen_}))
}

pub fn error_reply(req: &Value, gen_: u32, msg: &str) -> Value {
    with_id(req, json!({"type": "error", "gen": gen_, "data": msg}))
}

pub fn descriptor(blob: &[u8]) -> Value {
    serde_json::to_value(BlobDescriptor::describe(blob).unwrap()).unwrap()
}

pub fn devices_changed(gen_: u32) -> Value {
    json!({"type": "devicesChanged", "gen": gen_})
}

pub fn events_dropped(gen_: u32, count: u32) -> Value {
    json!({"type": "eventsDropped", "gen": gen_, "data": count})
}

pub fn device_event(gen_: u32, typ: &str, device: Uuid, data: Value) -> Value {
    json!({"type": typ, "gen": gen_, "deviceId": device, "data": data})
}

/// A host that knows two devices and answers `list`/`methods`/`invoke` simply. Used by tests that
/// don't care about the details. Generation is always 1.
pub fn simple_host(req: &Value, _blobs: &[u8], host: &Host) {
    let redstone = id(1);
    let storage = id(2);
    let reply = match req["type"].as_str().unwrap() {
        "list" => list_reply(
            req,
            1,
            &[(redstone, &["redstone", "device"]), (storage, &["storage"])],
        ),
        "methods" => methods_reply(req, 1, &["getInput", "setOutput"]),
        "invoke" => result_reply(req, 1, json!(7)),
        other => panic!("unexpected request type {other}"),
    };
    host.reply(&reply);
}

// ---------------------------------------------------------------------------------------------
// Device wrappers, written the way the docs tell wrapper authors to write them
// ---------------------------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct Redstone {
    client: HlapiClient,
    uuid: Uuid,
}

impl DeviceBase for Redstone {
    const TYPE: &'static str = "redstone";
    type Event = Infallible;
    fn new(client: HlapiClient, uuid: Uuid) -> Result<Self, HlapiClientError> {
        Ok(Self { client, uuid })
    }
    fn uuid_base(&self) -> Uuid {
        self.uuid
    }
    fn client(&self) -> &HlapiClient {
        &self.client
    }
}

impl Redstone {
    pub fn get_input(&self, side: &str) -> Result<i64, HlapiClientError> {
        Ok(self.call::<i64>("getInput", &[&side], &[])?.0)
    }
    pub fn set_output(&self, side: &str, level: u8) -> Result<(), HlapiClientError> {
        self.call::<()>("setOutput", &[&side, &level], &[])?;
        Ok(())
    }
}

/// A wrapper whose methods take and return blobs.
#[derive(Debug, Clone)]
pub struct Storage {
    client: HlapiClient,
    uuid: Uuid,
}

impl DeviceBase for Storage {
    const TYPE: &'static str = "storage";
    type Event = Infallible;
    fn new(client: HlapiClient, uuid: Uuid) -> Result<Self, HlapiClientError> {
        Ok(Self { client, uuid })
    }
    fn uuid_base(&self) -> Uuid {
        self.uuid
    }
    fn client(&self) -> &HlapiClient {
        &self.client
    }
}

impl Storage {
    pub fn read(&self) -> Result<Vec<u8>, HlapiClientError> {
        let (marker, mut blobs) = self.call::<BlobMarker>("read", &[], &[])?;
        Ok(blobs.swap_remove(marker.blob_idx as usize))
    }
    pub fn write(&self, data: &[u8]) -> Result<(), HlapiClientError> {
        self.call::<()>("write", &[&BlobMarker { blob_idx: 0 }], &[data])?;
        Ok(())
    }
}