satx-core 0.11.0

HP 48/49 calculator layer of satx: Kermit and XModem driver loops, calculator operations, HP objects; blocking or sans-I/O (wasm32)
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
//! Blocking links to a calculator: serial port and TCP (emulator) with the
//! `native` feature (default), the saturnus emulator in-process (HP 48SX,
//! 48GX or 49G, chosen in the `saturnus://MODEL@ROM` address) with the
//! `saturnus` feature, and in-memory (tests).
//!
//! A [`Transport`] writes whole packets and reads with a timeout. Each packet
//! goes out in one write: the HP's receiver overruns on inter-byte gaps. A
//! host without blocking reads (a browser) uses
//! [`machine::Machine`](crate::machine::Machine) instead.

#[cfg(not(target_arch = "wasm32"))]
use std::collections::VecDeque;
use std::io;
#[cfg(not(target_arch = "wasm32"))]
use std::io::ErrorKind;
#[cfg(feature = "native")]
use std::io::{Read, Write};
#[cfg(feature = "native")]
use std::net::TcpStream;

use crate::time::Duration;
#[cfg(not(target_arch = "wasm32"))]
use crate::time::Instant;

#[cfg(not(target_arch = "wasm32"))]
use kermit_proto::trace;
#[cfg(not(target_arch = "wasm32"))]
use kermit_proto::trace::Direction;

use crate::{Error, Result};

#[cfg(feature = "saturnus")]
mod saturnus;
#[cfg(feature = "saturnus")]
pub use saturnus::SaturnusTransport;

/// A byte link to a calculator.
pub trait Transport: Send {
    /// Write one whole packet with a single write (then flush).
    fn write_packet(&mut self, packet: &[u8]) -> io::Result<()>;
    /// Read what is available, waiting at most `timeout`. `Ok(0)` = timeout
    /// passed without data. A closed link is `Err(ErrorKind::UnexpectedEof)`.
    fn read(&mut self, buf: &mut [u8], timeout: Duration) -> io::Result<usize>;
}

/// Serial line speed: the HP default (IOPAR) and what the emulator expects.
pub const BAUD: u32 = 9600;

/// Shortest timeout handed to the OS; zero would mean "block forever".
#[cfg(feature = "native")]
const MIN_TIMEOUT: Duration = Duration::from_millis(1);

/// Open a link: `tcp://host:port` for an emulator, `saturnus://[MODEL@]ROM`
/// for the in-process saturnus emulator booted from the ROM image at the
/// path `ROM` (needs the `saturnus` feature; see [`parse_saturnus`]), e.g.
/// `saturnus:///abs/sxrom-j` (an HP 48SX) or
/// `saturnus://49g@/abs/rom.49g`; anything else without a `scheme://`
/// prefix is a serial device path (`/dev/ttyUSB0`, `COM3`). Serial and
/// TCP need the `native` feature: without it they are [`Error::Address`].
pub fn open(addr: &str) -> Result<Box<dyn Transport>> {
    open_at(addr, BAUD)
}

/// [`open`] with the serial line at `baud` instead of [`BAUD`] (the
/// HP 100LX/200LX Filer: 19200 by default); TCP and in-process links
/// ignore it.
pub fn open_at(addr: &str, baud: u32) -> Result<Box<dyn Transport>> {
    if addr.starts_with("saturnus://") {
        let (model, rom) = parse_saturnus(addr)?;
        return open_saturnus(model, rom);
    }
    if let Some(host_port) = addr.strip_prefix("tcp://") {
        if host_port.is_empty() {
            return Err(Error::Address(addr.to_string()));
        }
        return open_tcp(addr, host_port);
    }
    if addr.is_empty() || addr.contains("://") {
        return Err(Error::Address(addr.to_string()));
    }
    open_serial(addr, baud)
}

#[cfg(feature = "native")]
fn open_tcp(_addr: &str, host_port: &str) -> Result<Box<dyn Transport>> {
    Ok(Box::new(TcpTransport::connect(host_port)?))
}

#[cfg(not(feature = "native"))]
fn open_tcp(addr: &str, _host_port: &str) -> Result<Box<dyn Transport>> {
    Err(Error::Address(format!(
        "{addr} (satx-core was built without the `native` feature)"
    )))
}

#[cfg(feature = "native")]
fn open_serial(addr: &str, baud: u32) -> Result<Box<dyn Transport>> {
    Ok(Box::new(SerialTransport::open_at(addr, baud)?))
}

#[cfg(not(feature = "native"))]
fn open_serial(addr: &str, _baud: u32) -> Result<Box<dyn Transport>> {
    Err(Error::Address(format!(
        "{addr} (satx-core was built without the `native` feature)"
    )))
}

/// A calculator model the in-process emulator can be asked to run, by its
/// name in a `saturnus://MODEL@ROM` address. Only the 48SX, 48GX and 49G
/// boot; the others are named so that they can be refused with a clear
/// [`Error::Emulator`]: the 42S, 27S, 17BII, 17B, 28S and 19BII have no serial port,
/// the 38G, 39G and 40G
/// have no Kermit server command.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EmulatorModel {
    /// HP 48SX (the default without a `MODEL@` part).
    Hp48sx,
    /// HP 48GX.
    Hp48gx,
    /// HP 49G.
    Hp49g,
    /// HP 42S: refused, no serial port.
    Hp42s,
    /// HP 38G: refused, no Kermit server.
    Hp38g,
    /// HP 39G: refused, no Kermit server.
    Hp39g,
    /// HP 40G: refused, no Kermit server.
    Hp40g,
    /// HP 27S: refused, no serial port.
    Hp27s,
    /// HP 17BII: refused, no serial port.
    Hp17bii,
    /// HP 17B: refused, no serial port.
    Hp17b,
    /// HP 28S: refused, no serial port (only the IR printer).
    Hp28s,
    /// HP 19BII: refused, no serial port (only the IR printer).
    Hp19bii,
}

impl EmulatorModel {
    /// Every model with its address name.
    const ALL: [(EmulatorModel, &'static str); 12] = [
        (EmulatorModel::Hp48sx, "48sx"),
        (EmulatorModel::Hp48gx, "48gx"),
        (EmulatorModel::Hp49g, "49g"),
        (EmulatorModel::Hp42s, "42s"),
        (EmulatorModel::Hp38g, "38g"),
        (EmulatorModel::Hp39g, "39g"),
        (EmulatorModel::Hp40g, "40g"),
        (EmulatorModel::Hp27s, "27s"),
        (EmulatorModel::Hp17bii, "17bii"),
        (EmulatorModel::Hp17b, "17b"),
        (EmulatorModel::Hp28s, "28s"),
        (EmulatorModel::Hp19bii, "19bii"),
    ];

    /// The model's name in addresses (`48sx`).
    pub fn name(self) -> &'static str {
        Self::ALL
            .iter()
            .find(|(m, _)| *m == self)
            .map_or("?", |(_, n)| n)
    }

    /// The model named `name` (`48sx`, `HP48SX`; any case, optional `hp`).
    pub fn from_name(name: &str) -> Option<Self> {
        let lower = name.to_ascii_lowercase();
        let bare = lower.strip_prefix("hp").unwrap_or(&lower);
        Self::ALL.iter().find(|(_, n)| *n == bare).map(|(m, _)| *m)
    }
}

/// Whether `token` looks like a model name: ASCII letters and digits,
/// starting with a digit or with `hp` and a digit (`48sx`, `HP49G`).
fn model_token(token: &str) -> bool {
    let lower = token.to_ascii_lowercase();
    let bare = lower.strip_prefix("hp").unwrap_or(&lower);
    bare.starts_with(|c: char| c.is_ascii_digit())
        && bare.chars().all(|c| c.is_ascii_alphanumeric())
}

/// Split a `saturnus://[MODEL@]ROM` address into the model and the ROM
/// path. Without `MODEL@` the model is the 48SX. A ROM path may itself
/// contain `@`, so only a prefix before the first `@` that looks like a
/// model name (letters and digits, starting with a digit or `hp` and a
/// digit) counts as one. An empty ROM path or model is
/// [`Error::Address`]; an unknown model name is [`Error::Emulator`] listing
/// the accepted ones.
pub fn parse_saturnus(addr: &str) -> Result<(EmulatorModel, &std::path::Path)> {
    let bad = || Error::Address(addr.to_string());
    let spec = addr.strip_prefix("saturnus://").ok_or_else(bad)?;
    let (model, rom) = match spec.split_once('@') {
        Some(("", _)) => return Err(bad()),
        Some((token, rom)) if model_token(token) => {
            let model = EmulatorModel::from_name(token).ok_or_else(|| {
                Error::Emulator(format!(
                    "unknown model {token:?} in {addr}: saturnus:// boots 48sx, 48gx or 49g"
                ))
            })?;
            (model, rom)
        }
        _ => (EmulatorModel::Hp48sx, spec),
    };
    if rom.is_empty() {
        return Err(bad());
    }
    Ok((model, std::path::Path::new(rom)))
}

/// Boot the saturnus emulator in-process as `model` from the ROM image at
/// `rom` and start its Kermit server (48SX, 48GX and 49G; other models are
/// [`Error::Emulator`]).
#[cfg(feature = "saturnus")]
pub fn open_saturnus(model: EmulatorModel, rom: &std::path::Path) -> Result<Box<dyn Transport>> {
    Ok(Box::new(SaturnusTransport::open(model, rom)?))
}

/// Without the `saturnus` feature there is no in-process emulator.
#[cfg(not(feature = "saturnus"))]
pub fn open_saturnus(model: EmulatorModel, rom: &std::path::Path) -> Result<Box<dyn Transport>> {
    Err(Error::Emulator(format!(
        "cannot boot {} as the {}: satx-core was built without the `saturnus` feature",
        rom.display(),
        model.name()
    )))
}

/// Read and discard everything that arrives within `period` from now; returns
/// the number of bytes discarded. The idle HP server NAKs once, a server
/// timeout after its last packet, and then stays silent (wiki:
/// protocols/server-commands); that NAK sits in the buffers until we read it
/// (wiki: protocols/kermit-hp, tools/saturnng-oracle/README.md "Stale NAK on
/// connect").
#[cfg(not(target_arch = "wasm32"))]
pub fn drain(transport: &mut dyn Transport, period: Duration) -> io::Result<usize> {
    let end = Instant::now() + period;
    let mut buf = [0u8; 256];
    let mut discarded = 0;
    loop {
        let left = end.saturating_duration_since(Instant::now());
        if left.is_zero() {
            return Ok(discarded);
        }
        discarded += transport.read(&mut buf, left)?;
    }
}

/// TCP link, e.g. to the emulator's serial bridge (feature `native`).
#[cfg(feature = "native")]
#[derive(Debug)]
pub struct TcpTransport {
    stream: TcpStream,
}

#[cfg(feature = "native")]
impl TcpTransport {
    /// Connect to `host:port` with Nagle disabled.
    pub fn connect(host_port: &str) -> Result<Self> {
        let stream = TcpStream::connect(host_port)?;
        stream.set_nodelay(true)?;
        Ok(TcpTransport { stream })
    }
}

#[cfg(feature = "native")]
impl Transport for TcpTransport {
    fn write_packet(&mut self, packet: &[u8]) -> io::Result<()> {
        self.stream.write_all(packet)?;
        self.stream.flush()
    }

    fn read(&mut self, buf: &mut [u8], timeout: Duration) -> io::Result<usize> {
        self.stream
            .set_read_timeout(Some(timeout.max(MIN_TIMEOUT)))?;
        match self.stream.read(buf) {
            Ok(0) => Err(io::Error::from(ErrorKind::UnexpectedEof)),
            Ok(n) => Ok(n),
            Err(e) if matches!(e.kind(), ErrorKind::WouldBlock | ErrorKind::TimedOut) => Ok(0),
            Err(e) => Err(e),
        }
    }
}

/// Serial port at [`BAUD`], 8N1, no flow control (feature `native`).
#[cfg(feature = "native")]
pub struct SerialTransport {
    port: Box<dyn serialport::SerialPort>,
}

#[cfg(feature = "native")]
impl std::fmt::Debug for SerialTransport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SerialTransport")
            .field("port", &self.port.name())
            .finish()
    }
}

#[cfg(feature = "native")]
impl SerialTransport {
    /// Open and configure the serial device at `path`, at [`BAUD`].
    pub fn open(path: &str) -> Result<Self> {
        Self::open_at(path, BAUD)
    }

    /// Open and configure the serial device at `path`, at `baud`.
    pub fn open_at(path: &str, baud: u32) -> Result<Self> {
        let mut port = serialport::new(path, baud)
            .data_bits(serialport::DataBits::Eight)
            .parity(serialport::Parity::None)
            .stop_bits(serialport::StopBits::One)
            .flow_control(serialport::FlowControl::None)
            .open()?;
        // Early HP 49G units drive TX weakly and need a level buffer powered
        // from DTR/RTS (wiki: hardware/uart).
        port.write_data_terminal_ready(true)?;
        port.write_request_to_send(true)?;
        Ok(SerialTransport { port })
    }
}

#[cfg(feature = "native")]
impl Transport for SerialTransport {
    fn write_packet(&mut self, packet: &[u8]) -> io::Result<()> {
        self.port.write_all(packet)?;
        self.port.flush()
    }

    fn read(&mut self, buf: &mut [u8], timeout: Duration) -> io::Result<usize> {
        self.port.set_timeout(timeout.max(MIN_TIMEOUT))?;
        match self.port.read(buf) {
            Ok(n) => Ok(n),
            Err(e) if e.kind() == ErrorKind::TimedOut => Ok(0),
            Err(e) => Err(e),
        }
    }
}

#[cfg(not(target_arch = "wasm32"))]
type Peer = Box<dyn FnMut(&[u8]) -> Vec<Vec<u8>> + Send>;

#[cfg(not(target_arch = "wasm32"))]
enum Script {
    Peer(Peer),
    /// Remaining trace chunks; the front is the next expected `>` chunk.
    Trace(VecDeque<(Direction, Vec<u8>)>),
}

/// In-memory link for tests: a closure or a recorded trace plays the
/// calculator.
#[cfg(not(target_arch = "wasm32"))]
pub struct MemoryTransport {
    script: Script,
    readable: VecDeque<Vec<u8>>,
}

// The script may be a closure, which has no `Debug`.
#[cfg(not(target_arch = "wasm32"))]
impl std::fmt::Debug for MemoryTransport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MemoryTransport")
            .field("readable", &self.readable)
            .finish_non_exhaustive()
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl MemoryTransport {
    /// Each written packet is passed to `peer`; the chunks it returns become
    /// readable in order, one chunk per [`Transport::read`].
    pub fn new(peer: impl FnMut(&[u8]) -> Vec<Vec<u8>> + Send + 'static) -> Self {
        MemoryTransport {
            script: Script::Peer(Box::new(peer)),
            readable: VecDeque::new(),
        }
    }

    /// Replay a [`kermit_proto::trace`] text. `<` chunks before the first `>`
    /// are readable at once (stale input). Each write must equal the next `>`
    /// chunk (else `InvalidData`, and the chunk stays expected); after a
    /// matching write the `<` chunks up to the next `>` become readable.
    pub fn from_trace(text: &str) -> Result<Self> {
        let chunks: VecDeque<_> = trace::parse(text).map_err(Error::Reply)?.into();
        let mut transport = MemoryTransport {
            script: Script::Trace(chunks),
            readable: VecDeque::new(),
        };
        transport.release_input();
        Ok(transport)
    }

    /// Move leading `<` chunks of a trace to the readable queue.
    fn release_input(&mut self) {
        if let Script::Trace(chunks) = &mut self.script {
            while let Some((Direction::In, _)) = chunks.front() {
                if let Some((_, bytes)) = chunks.pop_front() {
                    self.readable.push_back(bytes);
                }
            }
        }
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl Transport for MemoryTransport {
    fn write_packet(&mut self, packet: &[u8]) -> io::Result<()> {
        match &mut self.script {
            Script::Peer(peer) => {
                let replies = peer(packet);
                self.readable.extend(replies);
            }
            Script::Trace(chunks) => {
                match chunks.front() {
                    Some((Direction::Out, expected)) if expected == packet => {}
                    Some((_, expected)) => {
                        return Err(io::Error::new(
                            ErrorKind::InvalidData,
                            format!(
                                "trace mismatch: wrote {}, expected {}",
                                trace::escape(packet),
                                trace::escape(expected)
                            ),
                        ));
                    }
                    None => {
                        return Err(io::Error::new(
                            ErrorKind::InvalidData,
                            format!("trace exhausted: wrote {}", trace::escape(packet)),
                        ));
                    }
                }
                chunks.pop_front();
                self.release_input();
            }
        }
        Ok(())
    }

    fn read(&mut self, buf: &mut [u8], timeout: Duration) -> io::Result<usize> {
        let Some(mut chunk) = self.readable.pop_front() else {
            // Behave like an idle link so the Kermit timeouts fire.
            std::thread::sleep(timeout);
            return Ok(0);
        };
        if chunk.len() > buf.len() {
            let rest = chunk.split_off(buf.len());
            self.readable.push_front(rest);
        }
        buf[..chunk.len()].copy_from_slice(&chunk);
        Ok(chunk.len())
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    #[cfg(feature = "native")]
    use std::net::TcpListener;
    #[cfg(feature = "native")]
    use std::sync::mpsc;

    const SHORT: Duration = Duration::from_millis(20);

    #[test]
    fn memory_peer_splits_large_chunks() {
        let mut t = MemoryTransport::new(|p| vec![p.to_vec(), b"x".to_vec()]);
        t.write_packet(b"abcde").unwrap();
        let mut buf = [0u8; 3];
        assert_eq!(t.read(&mut buf, SHORT).unwrap(), 3);
        assert_eq!(&buf, b"abc");
        assert_eq!(t.read(&mut buf, SHORT).unwrap(), 2);
        assert_eq!(&buf[..2], b"de");
        assert_eq!(t.read(&mut buf, SHORT).unwrap(), 1);
        assert_eq!(t.read(&mut buf, Duration::ZERO).unwrap(), 0);
    }

    #[test]
    fn memory_trace_replay() {
        let mut t = MemoryTransport::from_trace("< stale\n> out\n< in1\n< in2\n> end\n").unwrap();
        let mut buf = [0u8; 16];
        assert_eq!(t.read(&mut buf, SHORT).unwrap(), 5);
        assert_eq!(t.read(&mut buf, Duration::ZERO).unwrap(), 0);
        let err = t.write_packet(b"wrong").unwrap_err();
        assert_eq!(err.kind(), ErrorKind::InvalidData);
        assert!(err.to_string().contains("expected out"), "{err}");
        t.write_packet(b"out").unwrap();
        assert_eq!(t.read(&mut buf, SHORT).unwrap(), 3);
        assert_eq!(&buf[..3], b"in1");
        assert_eq!(t.read(&mut buf, SHORT).unwrap(), 3);
        t.write_packet(b"end").unwrap();
        let err = t.write_packet(b"more").unwrap_err();
        assert_eq!(err.kind(), ErrorKind::InvalidData);
        assert!(MemoryTransport::from_trace("? bad\n").is_err());
    }

    #[test]
    fn drain_discards_stale_input() {
        let mut t = MemoryTransport::from_trace("< \\x01# N3\\r\n> x\n").unwrap();
        assert_eq!(drain(&mut t, Duration::ZERO).unwrap(), 0);
        assert_eq!(drain(&mut t, SHORT).unwrap(), 6);
        let mut buf = [0u8; 8];
        assert_eq!(t.read(&mut buf, Duration::ZERO).unwrap(), 0);
    }

    #[cfg(feature = "native")]
    #[test]
    fn tcp_transport() {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        let (close_tx, close_rx) = mpsc::channel::<()>();
        let server = std::thread::spawn(move || {
            let (mut s, _) = listener.accept().unwrap();
            let mut packet = [0u8; 7];
            s.read_exact(&mut packet).unwrap();
            s.write_all(b"reply").unwrap();
            close_rx.recv().unwrap();
            packet
        });

        let mut t = TcpTransport::connect(&format!("127.0.0.1:{port}")).unwrap();
        t.write_packet(b"\x01packet").unwrap();
        let mut got = Vec::new();
        let mut buf = [0u8; 16];
        while got.len() < 5 {
            let n = t.read(&mut buf, Duration::from_secs(1)).unwrap();
            assert!(n > 0, "no reply");
            got.extend_from_slice(&buf[..n]);
        }
        assert_eq!(got, b"reply");

        let start = Instant::now();
        assert_eq!(t.read(&mut buf, SHORT).unwrap(), 0);
        assert!(start.elapsed() >= Duration::from_millis(15));

        close_tx.send(()).unwrap();
        assert_eq!(&server.join().unwrap(), b"\x01packet");
        let err = t.read(&mut buf, Duration::from_secs(1)).unwrap_err();
        assert_eq!(err.kind(), ErrorKind::UnexpectedEof);
    }

    #[cfg(feature = "native")]
    #[test]
    fn open_addresses() {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        assert!(open(&format!("tcp://127.0.0.1:{port}")).is_ok());
        assert!(matches!(open(""), Err(Error::Address(a)) if a.is_empty()));
        assert!(matches!(open("udp://x:1"), Err(Error::Address(a)) if a == "udp://x:1"));
        assert!(matches!(open("tcp://"), Err(Error::Address(_))));
        assert!(matches!(open("saturnus://"), Err(Error::Address(_))));
        assert!(matches!(open("saturnus://49g@"), Err(Error::Address(_))));
        assert!(matches!(
            open("saturnus:///nonexistent/rom"),
            Err(Error::Emulator(_))
        ));
        assert!(matches!(
            open("saturnus://49g@/nonexistent/rom"),
            Err(Error::Emulator(_))
        ));
    }

    #[test]
    fn saturnus_addresses() {
        fn parse(a: &str) -> Result<(EmulatorModel, String)> {
            parse_saturnus(a).map(|(m, p)| (m, p.to_string_lossy().into_owned()))
        }
        let ok = |m, p: &str| (m, p.to_string());
        assert_eq!(
            parse("saturnus:///r/sxrom-j").unwrap(),
            ok(EmulatorModel::Hp48sx, "/r/sxrom-j")
        );
        assert_eq!(
            parse("saturnus://49g@/r/rom.49g").unwrap(),
            ok(EmulatorModel::Hp49g, "/r/rom.49g")
        );
        assert_eq!(
            parse("saturnus://HP48GX@/r/gxrom-r").unwrap(),
            ok(EmulatorModel::Hp48gx, "/r/gxrom-r")
        );
        assert_eq!(
            parse("saturnus://48sx@roms/sx@j").unwrap(),
            ok(EmulatorModel::Hp48sx, "roms/sx@j")
        );
        // An `@` inside the path is not a model separator.
        assert_eq!(
            parse("saturnus:///r/a@b/rom").unwrap(),
            ok(EmulatorModel::Hp48sx, "/r/a@b/rom")
        );
        assert_eq!(
            parse("saturnus://roms@2/rom").unwrap(),
            ok(EmulatorModel::Hp48sx, "roms@2/rom")
        );
        assert_eq!(
            parse(r"saturnus://C:\r@1\rom").unwrap(),
            ok(EmulatorModel::Hp48sx, r"C:\r@1\rom")
        );
        for (name, model) in [
            ("42s", EmulatorModel::Hp42s),
            ("38G", EmulatorModel::Hp38g),
            ("39g", EmulatorModel::Hp39g),
            ("hp40g", EmulatorModel::Hp40g),
            ("17bii", EmulatorModel::Hp17bii),
            ("27S", EmulatorModel::Hp27s),
            ("17b", EmulatorModel::Hp17b),
            ("HP28S", EmulatorModel::Hp28s),
            ("19BII", EmulatorModel::Hp19bii),
        ] {
            let addr = format!("saturnus://{name}@/r/rom");
            assert_eq!(parse(&addr).unwrap().0, model);
        }
        assert!(matches!(
            parse("saturnus://@/r/rom"),
            Err(Error::Address(_))
        ));
        assert!(matches!(parse("saturnus://48sx@"), Err(Error::Address(_))));
        assert!(matches!(parse("tcp://h:1"), Err(Error::Address(_))));
        match parse("saturnus://48s@/r/rom") {
            Err(Error::Emulator(msg)) => assert!(msg.contains("48sx, 48gx or 49g"), "{msg}"),
            other => panic!("expected Error::Emulator, got {other:?}"),
        }
        for (model, name) in EmulatorModel::ALL {
            assert_eq!(model.name(), name);
            assert_eq!(EmulatorModel::from_name(name), Some(model));
        }
    }
}