nimble_protocol/
lib.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/nimble-rust/nimble
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5use std::fmt;
6use std::io::Result;
7
8use flood_rs::{Deserialize, ReadOctetStream, Serialize, WriteOctetStream};
9
10pub mod client_to_host;
11pub mod host_to_client;
12pub mod prelude;
13pub mod serialize;
14
15#[derive(Debug, Copy, Clone, PartialEq, Eq)]
16pub struct ClientRequestId(pub u8);
17
18impl fmt::Display for ClientRequestId {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        write!(f, "RequestId({:X})", self.0)
21    }
22}
23
24impl ClientRequestId {
25    #[must_use]
26
27    pub const fn new(value: u8) -> Self {
28        Self(value)
29    }
30}
31
32impl Serialize for ClientRequestId {
33    fn serialize(&self, stream: &mut impl WriteOctetStream) -> Result<()>
34    where
35        Self: Sized,
36    {
37        stream.write_u8(self.0)
38    }
39}
40
41impl Deserialize for ClientRequestId {
42    fn deserialize(stream: &mut impl ReadOctetStream) -> Result<Self>
43    where
44        Self: Sized,
45    {
46        Ok(Self(stream.read_u8()?))
47    }
48}
49
50#[derive(Debug, Copy, Clone, PartialEq, Eq)]
51pub struct Version {
52    pub major: u16,
53    pub minor: u16,
54    pub patch: u16,
55}
56
57impl Version {
58    #[must_use]
59
60    pub const fn new(major: u16, minor: u16, patch: u16) -> Self {
61        Self {
62            major,
63            minor,
64            patch,
65        }
66    }
67
68    /// # Errors
69    ///
70    /// `io::Error` // TODO:
71    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> Result<()> {
72        stream.write_u16(self.major)?;
73        stream.write_u16(self.minor)?;
74        stream.write_u16(self.patch)?;
75
76        Ok(())
77    }
78
79    /// # Errors
80    ///
81    /// `io::Error` // TODO:
82    pub fn from_stream(stream: &mut impl ReadOctetStream) -> Result<Self> {
83        Ok(Self {
84            major: stream.read_u16()?,
85            minor: stream.read_u16()?,
86            patch: stream.read_u16()?,
87        })
88    }
89}
90
91pub const NIMBLE_PROTOCOL_VERSION: Version = Version::new(0, 0, 5);
92
93#[derive(PartialEq, Copy, Clone, Eq)]
94pub struct SessionConnectionSecret {
95    pub value: u64,
96}
97
98impl SessionConnectionSecret {
99    /// # Errors
100    ///
101    /// `io::Error` // TODO:
102    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> Result<()> {
103        stream.write_u64(self.value)
104    }
105    /// # Errors
106    ///
107    /// `io::Error` // TODO:
108    pub fn from_stream(stream: &mut impl ReadOctetStream) -> Result<Self> {
109        Ok(Self {
110            value: stream.read_u64()?,
111        })
112    }
113}
114
115impl fmt::Display for SessionConnectionSecret {
116    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117        write!(f, "session_secret: {:X}", self.value)
118    }
119}
120
121impl fmt::Debug for SessionConnectionSecret {
122    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123        write!(f, "session_secret: {:X}", self.value)
124    }
125}