haematite-cli 0.21.3

The haematite command: serve a store, its door and Studio, answer MCP, query with jq, and host the store's assistant seat
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! Manual local Unix-socket service with bounded clients and frame deadlines.

use std::fs;
use std::io::{self, Read, Write};
use std::num::{NonZeroU32, NonZeroUsize};
use std::os::unix::fs::{MetadataExt, PermissionsExt};
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant};

use serde_json::json;

use super::{
    frame,
    protocol::{
        DecodedRequest, MutationContext, Operation, Request, ResponseFailure, decode_request,
    },
    service::Service,
    storage::Failure,
};

#[derive(Clone, Copy, Debug)]
pub struct Limits {
    pub max_frame: NonZeroU32,
    pub max_clients: NonZeroUsize,
    pub frame_timeout: Duration,
}

/// Holds this listener's socket and optional published record until owned cleanup.
pub struct Listener {
    socket: UnixListener,
    path: PathBuf,
    identity: (u64, u64),
    entry: Option<super::registry::Publication>,
    // Retained until after Drop removes our socket; never unlink the lock anchor.
    _ownership: fs::File,
}

impl Listener {
    /// # Errors
    ///
    /// Propagates any error from `acquire`, `clear_stale`, `bind` or `set_permissions`.
    pub fn bind(path: &Path) -> io::Result<Self> {
        let ownership = super::socket_owner::acquire(path)?;
        super::socket_owner::clear_stale(path)?;
        let socket = UnixListener::bind(path)?;
        let setup = (|| {
            fs::set_permissions(path, fs::Permissions::from_mode(0o600))?;
            fs::symlink_metadata(path)
        })();
        let metadata = match setup {
            Ok(metadata) => metadata,
            Err(error) => {
                if let Err(cleanup) = fs::remove_file(path) {
                    eprintln!("haematite socket setup cleanup: {cleanup}");
                }
                return Err(error);
            }
        };
        Ok(Self {
            socket,
            path: path.to_owned(),
            identity: (metadata.dev(), metadata.ino()),
            entry: None,
            _ownership: ownership,
        })
    }

    /// Publishes the store only after this listener holds socket ownership.
    ///
    /// # Errors
    /// Returns a named publication refusal; callers must withhold readiness on failure.
    pub fn publish(
        &mut self,
        endpoint: &super::runtime::Endpoint,
    ) -> Result<(), super::registry::Refusal> {
        if endpoint.socket != self.path || self.entry.is_some() {
            return Err(super::registry::Refusal::new(
                "registry_publication_failed",
                "listener endpoint differs or already has an entry",
            ));
        }
        self.entry = Some(super::registry::Publication::publish(endpoint)?);
        Ok(())
    }

    /// Removes this listener's entry while it still holds socket ownership.
    ///
    /// # Errors
    /// Returns a named cleanup refusal without removing a replacement entry.
    pub fn finish(&mut self) -> Result<(), super::registry::Refusal> {
        if let Some(mut entry) = self.entry.take() {
            entry.finish()?;
        }
        Ok(())
    }

    #[cfg(test)]
    pub(crate) fn publish_in(
        &mut self,
        directory: &Path,
        entry: super::registry::Entry,
    ) -> Result<(), super::registry::Refusal> {
        if entry.socket != self.path || self.entry.is_some() {
            return Err(super::registry::Refusal::new(
                "registry_publication_failed",
                "listener endpoint differs or already has an entry",
            ));
        }
        self.entry = Some(super::registry::Publication::publish_in(directory, entry)?);
        Ok(())
    }

    /// # Errors
    ///
    /// Refuses invalid serving limits or reclaim activation; client failures are logged.
    pub fn run(&self, service: &Arc<Service>, limits: Limits) -> io::Result<()> {
        Self::accepting(self.socket.incoming(), service, limits, spawn_client)
    }

    fn accepting(
        incoming: impl Iterator<Item = io::Result<UnixStream>>,
        service: &Arc<Service>,
        limits: Limits,
        mut spawn: impl FnMut(UnixStream, Arc<Service>, Limits, Permit) -> io::Result<()>,
    ) -> io::Result<()> {
        let active = Self::begin_serving(service, limits)?;
        for incoming in incoming {
            let stream = match incoming {
                Ok(stream) => stream,
                Err(error) => {
                    let name = accept_error_name(&error);
                    eprintln!("haematite accept_failed: {name}: {error}");
                    if matches!(error.raw_os_error(), Some(23 | 24)) {
                        std::thread::sleep(Duration::from_millis(25));
                    }
                    continue;
                }
            };
            if let Err(count) = active.fetch_update(Ordering::AcqRel, Ordering::Acquire, |count| {
                (count < limits.max_clients.get()).then(|| count + 1)
            }) {
                refuse_client(
                    &stream,
                    limits,
                    "busy_max_clients",
                    &format!("max_clients={}; active_clients={count}", limits.max_clients),
                );
                continue;
            }
            let permit = Permit(Arc::clone(&active));
            let service = Arc::clone(service);
            let refusal = match stream.try_clone() {
                Ok(refusal) => refusal,
                Err(error) => {
                    refuse_client(
                        &stream,
                        limits,
                        "client_spawn_failed",
                        &format!("socket_clone_failed: {error}"),
                    );
                    continue;
                }
            };
            if let Err(error) = spawn(stream, service, limits, permit) {
                refuse_client(&refusal, limits, "client_spawn_failed", &error.to_string());
            }
        }
        Ok(())
    }

    fn begin_serving(service: &Service, limits: Limits) -> io::Result<Arc<AtomicUsize>> {
        if limits.frame_timeout.is_zero() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "frame timeout must be positive",
            ));
        }
        service
            .activate_reclaim()
            .map_err(|error| io::Error::other(format!("reclaim_activation_failed: {error}")))?;
        Ok(Arc::new(AtomicUsize::new(0)))
    }
}

impl Drop for Listener {
    fn drop(&mut self) {
        if let Err(error) = self.finish() {
            eprintln!("{error}");
        }
        if let Ok(metadata) = fs::symlink_metadata(&self.path)
            && (metadata.dev(), metadata.ino()) == self.identity
            && let Err(error) = fs::remove_file(&self.path)
        {
            eprintln!("haematite socket cleanup: {error}");
        }
    }
}

/// The log line for a connection that ended in an error, saying which kind of
/// end it was. A client that closes between frames ends with no error at all.
fn ending(error: &io::Error) -> String {
    let what = match error.kind() {
        io::ErrorKind::UnexpectedEof => "closed inside a frame",
        // an idle held connection ends here too; macOS names it EAGAIN
        io::ErrorKind::TimedOut | io::ErrorKind::WouldBlock => {
            "sent no whole frame within the frame deadline"
        }
        io::ErrorKind::InvalidData => "sent a frame that was refused",
        io::ErrorKind::BrokenPipe => "closed before its answer was written",
        _ => "connection failed",
    };
    format!("haematite client {what}: {error}")
}

fn accept_error_name(error: &io::Error) -> String {
    match error.raw_os_error() {
        Some(24) => "EMFILE".to_owned(),
        Some(23) => "ENFILE".to_owned(),
        _ if error.kind() == io::ErrorKind::ConnectionAborted => "ECONNABORTED".to_owned(),
        _ => format!("{:?}", error.kind()),
    }
}

fn refusal_id(stream: &UnixStream, limits: Limits) -> io::Result<String> {
    stream.set_read_timeout(Some(limits.frame_timeout.min(Duration::from_millis(250))))?;
    let cap = limits.max_frame.min(NonZeroU32::MIN.saturating_add(65_535));
    let bytes = frame::read(&mut &*stream, cap)?
        .ok_or_else(|| io::Error::new(io::ErrorKind::UnexpectedEof, "peer sent no request id"))?;
    let request: serde_json::Value = serde_json::from_slice(&bytes)
        .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
    request
        .get("id")
        .and_then(serde_json::Value::as_str)
        .map(str::to_owned)
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "request has no string id"))
}

fn refuse_client(stream: &UnixStream, limits: Limits, code: &str, message: &str) {
    let (id, message) = match refusal_id(stream, limits) {
        Ok(id) => (id, message.to_owned()),
        Err(error) => (
            String::new(),
            format!(
                "{message}; request_id_unavailable: {:?}: {error}",
                error.kind()
            ),
        ),
    };
    let result = (|| {
        let response = text(
            &json!({"id": id, "error": ResponseFailure::new(Failure::new(code, message.clone()), None)}),
        )?;
        stream.set_write_timeout(Some(limits.frame_timeout.min(Duration::from_millis(250))))?;
        frame::write(&mut &*stream, response.get().as_bytes(), limits.max_frame)
    })();
    match result {
        Ok(()) => eprintln!("haematite {code}: {message}"),
        Err(error) => eprintln!("haematite {code}: {message}; refusal_delivery_failed: {error}"),
    }
}

fn spawn_client(
    stream: UnixStream,
    service: Arc<Service>,
    limits: Limits,
    permit: Permit,
) -> io::Result<()> {
    std::thread::Builder::new()
        .name("haematite-client".to_owned())
        .spawn(move || {
            let _permit = permit;
            if let Err(error) = connection(stream, &service, limits) {
                eprintln!("{}", ending(&error));
            }
        })
        .map(|_| ())
}

struct Permit(Arc<AtomicUsize>);
impl Drop for Permit {
    fn drop(&mut self) {
        self.0.fetch_sub(1, Ordering::AcqRel);
    }
}

#[derive(Debug)]
struct ReceiptDeliveryError {
    original: ResponseFailure,
    sealed: Option<super::receipt_fragments::EncodedReceipt>,
    cause: Box<dyn std::error::Error + Send + Sync>,
}
impl std::fmt::Display for ReceiptDeliveryError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "receipt_delivery_failed: {}; {}; retained {} sealed bytes",
            self.original,
            self.cause,
            self.sealed
                .as_ref()
                .map_or(0, |receipt| receipt.bytes().len())
        )
    }
}
impl std::error::Error for ReceiptDeliveryError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(self.cause.as_ref())
    }
}

fn connection(stream: UnixStream, service: &Service, limits: Limits) -> io::Result<()> {
    let mut stream = Deadline {
        stream,
        end: deadline(limits.frame_timeout)?,
    };
    let mut cap = limits.max_frame;
    let mut greeted = false;
    // The Lys pass this connection presented, if any; every later request on
    // it is decided from it.
    let mut pass: Option<Presented> = None;
    loop {
        stream.end = deadline(limits.frame_timeout)?;
        let Some(bytes) = frame::read(&mut stream, cap)? else {
            return Ok(());
        };
        let response_limit = NonZeroUsize::new(cap.get() as usize)
            .ok_or_else(|| io::Error::other("invalid response frame limit"))?;
        let (id, hello, mutation, result) = match decode_request(&bytes)? {
            DecodedRequest::Request(request) if follows(&request, greeted) => {
                follow_head(
                    &mut stream,
                    service,
                    limits.frame_timeout,
                    cap,
                    &request,
                    &bytes,
                    pass.as_ref(),
                )?;
                continue;
            }
            DecodedRequest::Request(request) => {
                let hello = matches!(&request.operation, Operation::Hello(_));
                let mutation = MutationContext::from_operation(&request.operation);
                let agreement = match &request.operation {
                    Operation::Hello(agreement) => agreement.validate(limits.max_frame),
                    _ => Ok(cap),
                };
                let result = match agreement {
                    Err(failure) => Err(ResponseFailure::new(failure, mutation.clone())),
                    Ok(_) if !greeted && !hello => Err(ResponseFailure::new(
                        Failure::new("hello_required", "send hello before other requests"),
                        mutation.clone(),
                    )),
                    Ok(agreed) => {
                        cap = agreed;
                        let budget = NonZeroUsize::new(cap.get() as usize)
                            .ok_or_else(|| io::Error::other("invalid response frame limit"))?;
                        answer_agreed(
                            service,
                            request.operation,
                            &bytes,
                            &mut pass,
                            mutation.as_ref(),
                            budget,
                            limits.frame_timeout,
                        )
                    }
                };
                (request.id, hello, mutation, result)
            }
            DecodedRequest::Refused { id, failure } => {
                let hello = matches!(
                    failure.code.as_str(),
                    "typed_receipt_capability_missing"
                        | "receipt_protocol_mismatch"
                        | "receipt_frame_too_small"
                );
                let mutation = failure.mutation_context();
                (id, hello, mutation, Err(failure))
            }
        };
        let close = hello && result.is_err();
        if hello {
            greeted = result.is_ok();
        }
        if let Err(error) = &result
            && deliver_receipt(&mut stream, error, &id, cap)?
        {
            if close {
                return Ok(());
            }
            continue;
        }
        let response = response_text(&id, hello, cap, result, mutation, response_limit)?;
        stream.end = deadline(limits.frame_timeout)?;
        frame::write(&mut stream, response.get().as_bytes(), cap)?;
        if close {
            return Ok(());
        }
    }
}

/// The Lys pass a connection presented, and when it expires.
struct Presented {
    token: String,
    /// Seconds since the epoch, as the service read it from the pass; `None`
    /// only when its answer carried none.
    expires: Option<u64>,
}

/// Answers one request after the hello agreement: `session.pass` is presented
/// to the service and kept as this connection's pass when it is accepted;
/// every other operation is authorized against that pass before it runs.
fn answer_agreed(
    service: &Service,
    operation: Operation,
    bytes: &[u8],
    pass: &mut Option<Presented>,
    mutation: Option<&MutationContext>,
    budget: NonZeroUsize,
    timeout: Duration,
) -> Result<Box<serde_json::value::RawValue>, ResponseFailure> {
    if let Operation::Present(presented) = operation {
        let answered = service.present(&presented.pass);
        let expires = answered
            .as_ref()
            .ok()
            .and_then(|answer| answer.get("expires"))
            .and_then(serde_json::Value::as_u64);
        let answer = answered
            .and_then(|answer| serde_json::value::to_raw_value(&answer).map_err(Failure::storage))
            .map_err(|failure| ResponseFailure::new(failure, mutation.cloned()));
        if answer.is_ok() {
            *pass = Some(Presented {
                token: presented.pass,
                expires,
            });
        }
        answer
    } else {
        let token = pass.as_ref().map(|pass| pass.token.as_str());
        let words = std::str::from_utf8(bytes).ok();
        service
            .authorize(token, &operation, words)
            .map_err(|failure| ResponseFailure::new(failure, mutation.cloned()))
            .and_then(|()| match operation {
                // The runner acts with this connection's pass, as
                // session.pass is answered by this connection.
                Operation::DraftsRun(run) => service
                    .run_drafts_waiting(token, &run, timeout)
                    .and_then(|answer| {
                        serde_json::value::to_raw_value(&answer).map_err(Failure::storage)
                    })
                    .map_err(|failure| ResponseFailure::new(failure, mutation.cloned())),
                operation => service.dispatch_text_waiting(operation, budget, timeout),
            })
    }
}

/// Whether `request` is a follow this connection answers as a stream: one
/// sent after hello. Before hello it is refused `hello_required`, as any is.
const fn follows(request: &Request, greeted: bool) -> bool {
    greeted && matches!(&request.operation, Operation::Follow(_))
}

/// Answers `head.follow` as a stream of frames, every one with the request's
/// id (serve/follow.rs): once the connection's pass allows it, the head at
/// once and one frame per change of it, then the refusal that ends it; a
/// refused follow is answered by its refusal alone and sent no head.
///
/// The follow holds this connection's thread, and so its one client slot,
/// for as long as it lasts. It only writes, each frame under a fresh frame
/// deadline, so the idle read deadline never cuts it; the next request is
/// read under a fresh one. A write that fails means the client went away and
/// ends the connection. Socket readiness also wakes it as soon as the peer closes.
fn follow_head(
    stream: &mut Deadline,
    service: &Service,
    frame_timeout: Duration,
    cap: NonZeroU32,
    request: &Request,
    bytes: &[u8],
    pass: Option<&Presented>,
) -> io::Result<()> {
    let response_limit = NonZeroUsize::new(cap.get() as usize)
        .ok_or_else(|| io::Error::other("invalid response frame limit"))?;
    let id = request.id.as_str();
    let token = pass.map(|pass| pass.token.as_str());
    let words = std::str::from_utf8(bytes).ok();
    let ending = match (
        service.authorize(token, &request.operation, words),
        &request.operation,
    ) {
        (Err(failure), _) => failure,
        (Ok(()), Operation::Follow(asked)) => service.follow_socket(
            &stream.stream.try_clone()?,
            &asked.branch,
            pass.and_then(|pass| pass.expires),
            |head| {
                let result = text(head)?;
                let response = response_text(id, false, cap, Ok(result), None, response_limit)?;
                stream.end = deadline(frame_timeout)?;
                frame::write(&mut *stream, response.get().as_bytes(), cap)
            },
        )?,
        (Ok(()), _) => Failure::new("invalid_operation", "only head.follow is followed"),
    };
    let response = response_text(
        id,
        false,
        cap,
        Err(ResponseFailure::new(ending, None)),
        None,
        response_limit,
    )?;
    stream.end = deadline(frame_timeout)?;
    frame::write(stream, response.get().as_bytes(), cap)
}

/// Writes a refusal's sealed receipt as fragment frames. Answers whether the
/// refusal carried a receipt, in which case no plain response follows it.
fn deliver_receipt(
    stream: &mut Deadline,
    error: &ResponseFailure,
    id: &str,
    cap: NonZeroU32,
) -> io::Result<bool> {
    let sealed = error.failure().seal().map_err(|cause| {
        io::Error::other(ReceiptDeliveryError {
            original: error.clone(),
            sealed: None,
            cause: Box::new(cause),
        })
    })?;
    let Some(sealed) = sealed else {
        return Ok(false);
    };
    let frames = sealed.frames(id, cap.get()).map_err(|cause| {
        io::Error::other(ReceiptDeliveryError {
            original: error.clone(),
            sealed: None,
            cause: Box::new(cause),
        })
    })?;
    for body in frames {
        if let Err(cause) = frame::write(stream, &body, cap) {
            return Err(io::Error::other(ReceiptDeliveryError {
                original: error.clone(),
                sealed: Some(sealed),
                cause: Box::new(cause),
            }));
        }
    }
    Ok(true)
}

/// The one response frame for a request: the hello answer carries the agreed
/// frame cap, and an answer over the response limit becomes a refusal that fits.
fn response_text(
    id: &str,
    hello: bool,
    cap: NonZeroU32,
    result: Result<Box<serde_json::value::RawValue>, ResponseFailure>,
    mutation: Option<MutationContext>,
    response_limit: NonZeroUsize,
) -> io::Result<Box<serde_json::value::RawValue>> {
    let mutation = result
        .as_ref()
        .err()
        .and_then(ResponseFailure::mutation_context)
        .or(mutation);
    let response = match result {
        Ok(result) if hello => {
            let mut result: serde_json::Value =
                serde_json::from_str(result.get()).map_err(io::Error::other)?;
            result["max_frame"] = json!(cap);
            text(&json!({"id":id, "result":result}))?
        }
        Ok(result) => text(&Replied {
            id,
            result: &result,
        })?,
        Err(error) => text(&json!({"id":id, "error":error}))?,
    };
    let response = if response.get().len() <= response_limit.get() {
        response
    } else {
        text(
            &json!({"id":id,"error":ResponseFailure::after_dispatch(Failure::new("response_limit", "response exceeds frame limit; use a smaller page, or reconcile a mutation by its request ID"), mutation)}),
        )?
    };
    if response.get().len() > response_limit.get() {
        return Err(io::Error::other("frame limit cannot fit refusal envelope"));
    }
    Ok(response)
}

struct Deadline {
    stream: UnixStream,
    end: Instant,
}

fn deadline(timeout: Duration) -> io::Result<Instant> {
    Instant::now()
        .checked_add(timeout)
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "frame deadline overflow"))
}
impl Deadline {
    fn remaining(&self) -> io::Result<Duration> {
        self.end
            .checked_duration_since(Instant::now())
            .filter(|value| !value.is_zero())
            .ok_or_else(|| io::Error::new(io::ErrorKind::TimedOut, "frame deadline elapsed"))
    }

    /// macOS refuses a socket timeout with EINVAL once the peer has closed. A
    /// socket in that state never blocks, so the one call is made without the
    /// deadline: a read gets what is still buffered and then end of file, a
    /// write gets the peer's close. A call that would block means the peer is
    /// still there and the deadline could not be set, which is returned.
    fn without_deadline(
        &mut self,
        refused: io::Error,
        call: impl FnOnce(&mut UnixStream) -> io::Result<usize>,
    ) -> io::Result<usize> {
        if refused.kind() != io::ErrorKind::InvalidInput {
            return Err(refused);
        }
        self.stream.set_nonblocking(true)?;
        let done = call(&mut self.stream);
        self.stream.set_nonblocking(false)?;
        match done {
            Err(error) if error.kind() == io::ErrorKind::WouldBlock => Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                format!("the frame deadline could not be set on a live connection: {refused}"),
            )),
            other => other,
        }
    }
}
impl Read for Deadline {
    fn read(&mut self, bytes: &mut [u8]) -> io::Result<usize> {
        match self.stream.set_read_timeout(Some(self.remaining()?)) {
            Ok(()) => self.stream.read(bytes),
            Err(refused) => self.without_deadline(refused, |stream| stream.read(bytes)),
        }
    }
}
impl Write for Deadline {
    fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
        match self.stream.set_write_timeout(Some(self.remaining()?)) {
            Ok(()) => self.stream.write(bytes),
            Err(refused) => self.without_deadline(refused, |stream| stream.write(bytes)),
        }
    }
    fn flush(&mut self) -> io::Result<()> {
        self.stream.flush()
    }
}

/// A response whose result is text, written as it is (NB1).
#[derive(serde::Serialize)]
struct Replied<'a> {
    id: &'a str,
    result: &'a serde_json::value::RawValue,
}

/// `value` as one line of response text.
fn text<T: serde::Serialize>(value: &T) -> io::Result<Box<serde_json::value::RawValue>> {
    serde_json::value::to_raw_value(value).map_err(io::Error::other)
}

#[cfg(test)]
#[path = "transport_tests.rs"]
mod tests;

#[cfg(test)]
#[path = "transport_follow_tests.rs"]
mod follow_tests;

#[cfg(test)]
#[path = "transport_admission_tests.rs"]
mod admission_tests;