icedwright_protocol/
version.rs1pub const PROTOCOL: (u16, u16) = (1, 0);
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
8#[error(
9 "protocol mismatch: this side speaks {}.{}, the peer speaks {}.{}",
10 ours.0,
11 ours.1,
12 theirs.0,
13 theirs.1
14)]
15pub struct VersionMismatch {
16 pub ours: (u16, u16),
18 pub theirs: (u16, u16),
20}
21
22pub fn accepts(peer: (u16, u16)) -> Result<(), VersionMismatch> {
25 if peer.0 == PROTOCOL.0 {
26 return Ok(());
27 }
28 Err(VersionMismatch {
29 ours: PROTOCOL,
30 theirs: peer,
31 })
32}
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37
38 #[test]
39 fn same_major_is_accepted_whatever_the_minor() {
40 let table = [
41 ((1, 0), true),
42 ((1, 7), true),
43 ((0, 9), false),
44 ((2, 0), false),
45 ((u16::MAX, 0), false),
46 ];
47 for (peer, ok) in table {
48 assert_eq!(accepts(peer).is_ok(), ok, "{peer:?}");
49 }
50 }
51
52 #[test]
53 fn mismatch_names_both_versions() {
54 let message = accepts((2, 3)).err().map(|e| e.to_string());
55 assert_eq!(
56 message.as_deref(),
57 Some("protocol mismatch: this side speaks 1.0, the peer speaks 2.3")
58 );
59 }
60}