Skip to content

Commit eaa2be1

Browse files
committed
docs: documented a lot of structs and modules
Signed-off-by: Jad K. Haddad <jadkhaddad@gmail.com>
1 parent c0c2bf8 commit eaa2be1

9 files changed

Lines changed: 129 additions & 4 deletions

File tree

src/close_code.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
/// A WebSocket Close code.
2+
///
3+
/// Indicate why an endpoint is closing the WebSocket connection.
14
#[repr(u16)]
5+
#[non_exhaustive]
26
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
37
pub enum CloseCode {
48
/// Indicates a normal closure, meaning that the purpose for
@@ -59,10 +63,15 @@ pub enum CloseCode {
5963
/// to a different IP (when multiple targets exist), or reconnect to the same IP
6064
/// when a user has performed an action.
6165
Again = 1013,
66+
#[doc(hidden)]
6267
Tls = 1015,
68+
#[doc(hidden)]
6369
Reserved(u16),
70+
#[doc(hidden)]
6471
Iana(u16),
72+
#[doc(hidden)]
6573
Library(u16),
74+
#[doc(hidden)]
6675
Bad(u16),
6776
}
6877

src/close_frame.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::CloseCode;
22

3+
/// A WebSocket Close frame.
34
#[derive(Debug)]
45
pub struct CloseFrame<'a> {
56
/// The reason as a code.

src/error.rs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,56 @@
1+
//! Crate's error module.
2+
//!
3+
//! Contains all error types used throughout the crate.
4+
15
use core::convert::Infallible;
26

7+
/// Error decoding a WebSocket frame.
38
#[derive(Debug, thiserror::Error)]
49
pub enum FrameDecodeError {
10+
/// Reserved bits are not zero.
511
#[error("Reserved bits must be zero")]
612
ReservedBitsNotZero,
13+
/// Unmasked frame received from client.
14+
///
715
/// The server must close the connection when an unmasked frame is received.
816
#[error("Received an unmasked frame from client")]
917
UnmaskedFrameFromClient,
18+
/// Masked frame received from server.
19+
///
1020
/// The client must close the connection when a masked frame is received.
1121
#[error("Received a masked frame from server")]
1222
MaskedFrameFromServer,
23+
/// Invalid opcode.
1324
#[error("Invalid opcode")]
1425
InvalidOpCode,
15-
// The payload length comes as a u64, converting it to usize might fail on 32-bit systems
26+
/// Payload length is too large.
27+
// XXX: The payload length comes as a u64, converting it to usize might fail on 32-bit systems
1628
#[error("Payload too large")]
1729
PayloadTooLarge,
30+
/// Control frame fragmented.
31+
///
32+
/// Control frames must not be fragmented.
1833
#[error("Control frame fragmented")]
1934
ControlFrameFragmented,
35+
/// Control frame too large.
36+
///
37+
/// Control frames must have a payload length of 125 bytes or less.
2038
#[error("Control frame too large")]
2139
ControlFrameTooLarge,
2240
}
2341

42+
/// Error encoding a WebSocket frame.
2443
#[derive(Debug, thiserror::Error)]
2544
pub enum FrameEncodeError {
45+
/// Write buffer is too small to hold the encoded frame.
2646
#[error("Buffer too small")]
2747
BufferTooSmall,
2848
}
2949

50+
/// Error decoding an HTTP request/response.
3051
#[derive(Debug, thiserror::Error)]
3152
pub enum HttpDecodeError {
53+
/// Error parsing the HTTP request/response.
3254
#[error("Parse error: {0}")]
3355
Parse(httparse::Error),
3456
}
@@ -39,63 +61,83 @@ impl From<httparse::Error> for HttpDecodeError {
3961
}
4062
}
4163

64+
/// Error encoding an HTTP request/response.
4265
#[derive(Debug, thiserror::Error)]
4366
pub enum HttpEncodeError {
67+
/// Write buffer is too small to hold the encoded HTTP request/response.
4468
#[error("Buffer too small")]
4569
BufferTooSmall,
4670
}
4771

72+
/// Protocol specific errors/violations.
4873
#[derive(Debug, thiserror::Error)]
4974
pub enum ProtocolError {
75+
/// Close frame is invalid.
5076
#[error("Invalid close frame")]
5177
InvalidCloseFrame,
78+
/// Close code is invalid.
5279
#[error("Invalid close code")]
5380
InvalidCloseCode,
81+
/// Text message contains invalid UTF-8.
5482
#[error("Invalid UTF-8")]
5583
InvalidUTF8,
84+
/// Fragment is invalid.
85+
///
86+
/// This happens when a final fragment is received without any prior fragments.
5687
#[error("Invalid fragment")]
5788
InvalidFragment,
89+
/// Continuation frame is invalid.
90+
///
91+
/// This happens when a continuation frame is received without an ongoing fragmented message.
5892
#[error("Invalid continuation frame")]
5993
InvalidContinuationFrame,
6094
}
6195

96+
/// Error reading from a WebSocket connection.
6297
#[derive(Debug, thiserror::Error)]
6398
pub enum ReadError<I> {
99+
/// Error reading a WebSocket frame from the underlying I/O.
64100
#[error("Read frame error: {0}")]
65101
ReadFrame(
66102
#[source]
67103
#[from]
68104
framez::ReadError<I, FrameDecodeError>,
69105
),
106+
/// Error reading an HTTP request/response from the underlying I/O.
70107
#[error("Read http error: {0}")]
71108
ReadHttp(
72109
#[source]
73110
#[from]
74111
framez::ReadError<I, HttpDecodeError>,
75112
),
113+
/// Protocol error.
76114
#[error("Protocol error: {0}")]
77115
Protocol(
78116
#[source]
79117
#[from]
80118
ProtocolError,
81119
),
120+
/// Fragments buffer is too small to read a frame.
82121
#[error("Fragments buffer too small to read a frame")]
83122
FragmentsBufferTooSmall,
84123
}
85124

125+
/// Error writing to a WebSocket connection.
86126
#[derive(Debug, thiserror::Error)]
87127
pub enum WriteError<I> {
88128
/// Websocket connection is closed.
89129
///
90-
/// To close the TCP connection, you should drop the [`WebSocket`](crate::WebSocket) instance.
130+
/// To close the TCP connection, you should drop/close the underlying I/O instance.
91131
#[error("Connection closed")]
92132
ConnectionClosed,
133+
/// Error writing a WebSocket frame to the underlying I/O.
93134
#[error("Write frame error: {0}")]
94135
WriteFrame(
95136
#[source]
96137
#[from]
97138
framez::WriteError<I, FrameEncodeError>,
98139
),
140+
/// Error writing an HTTP request/response to the underlying I/O.
99141
#[error("Write http error: {0}")]
100142
WriteHttp(
101143
#[source]
@@ -104,6 +146,11 @@ pub enum WriteError<I> {
104146
),
105147
}
106148

149+
/// Error establishing a WebSocket handshake.
150+
///
151+
/// # Generic Parameter
152+
///
153+
/// `E`: User-defined error type for custom errors during the handshake.
107154
#[derive(Debug, thiserror::Error)]
108155
pub enum HandshakeError<E = Infallible> {
109156
/// Use of the wrong HTTP method (the WebSocket protocol requires the GET method to be used).
@@ -112,52 +159,72 @@ pub enum HandshakeError<E = Infallible> {
112159
/// Wrong HTTP version used (the WebSocket protocol requires version 1.1 or higher).
113160
#[error("HTTP version must be 1.1 or higher")]
114161
WrongHttpVersion,
162+
/// Connection was closed during the handshake.
115163
#[error("Connection closed during handshake")]
116164
ConnectionClosed,
165+
/// Invalid status code. (Should be 101 for switching protocols.)
117166
#[error("Invalid status code")]
118167
InvalidStatusCode,
168+
/// Missing or invalid (`Upgrade`: `websocket`) header.
119169
#[error("Missing or invalid upgrade header")]
120170
MissingOrInvalidUpgrade,
171+
/// Missing or invalid (`Connection`: `upgrade`) header.
121172
#[error("Missing or invalid connection header")]
122173
MissingOrInvalidConnection,
174+
/// Missing or invalid (`Sec-WebSocket-Accept`) header.
123175
#[error("Missing or invalid sec websocket accept header")]
124176
MissingOrInvalidAccept,
177+
/// Missing or invalid (`Sec-WebSocket-Version`) header.
125178
#[error("Missing or invalid sec websocket version header")]
126179
MissingOrInvalidSecVersion,
180+
/// Missing (`Sec-WebSocket-Key`) header.
127181
#[error("Missing sec websocket key header")]
128182
MissingSecKey,
183+
/// Other error.
184+
///
185+
/// User-defined error type.
129186
#[error("Other: {0}")]
130187
Other(E),
131188
}
132189

190+
/// Fragmentation error.
133191
#[derive(Debug, thiserror::Error)]
134192
pub enum FragmentationError {
193+
/// Fragment size is zero.
135194
#[error("Fragment size must be greater than 0")]
136195
InvalidFragmentSize,
196+
/// Error indicating that a message type that cannot be fragmented was attempted to be fragmented.
197+
///
198+
/// Only text and binary messages can be fragmented.
137199
#[error("Only text and binary messages can be fragmented")]
138200
CanNotBeFragmented,
139201
}
140202

203+
/// General WebSocket error type.
141204
#[derive(Debug, thiserror::Error)]
142205
pub enum Error<I, E = Infallible> {
206+
/// Error reading from the WebSocket connection.
143207
#[error("Read error: {0}")]
144208
Read(
145209
#[from]
146210
#[source]
147211
ReadError<I>,
148212
),
213+
/// Error writing to the WebSocket connection.
149214
#[error("Write error: {0}")]
150215
Write(
151216
#[from]
152217
#[source]
153218
WriteError<I>,
154219
),
220+
/// Handshake error.
155221
#[error("Handshake error: {0}")]
156222
Handshake(
157223
#[from]
158224
#[source]
159225
HandshakeError<E>,
160226
),
227+
/// Fragmentation error.
161228
#[error("Fragment error: {0}")]
162229
Fragmentation(
163230
#[from]

src/http.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! HTTP request and response types.
2+
//!
3+
//! The [`Header`] type is re-exported from the [`httparse`] crate.
4+
15
use framez::{decode::Decoder, encode::Encoder};
26
pub use httparse::Header;
37
use httparse::Status;
@@ -95,6 +99,7 @@ impl Encoder<OutResponse<'_, '_>> for OutResponseCodec {
9599
}
96100
}
97101

102+
/// An HTTP response.
98103
#[derive(Debug)]
99104
pub struct Response<'buf, const N: usize> {
100105
/// The response minor version, such as `1` for `HTTP/1.1`.
@@ -110,6 +115,7 @@ pub struct Response<'buf, const N: usize> {
110115
}
111116

112117
impl<'buf, const N: usize> Response<'buf, N> {
118+
/// Creates a new [`Response`].
113119
pub const fn new(
114120
version: u8,
115121
code: u16,
@@ -124,18 +130,22 @@ impl<'buf, const N: usize> Response<'buf, N> {
124130
}
125131
}
126132

133+
/// Returns the HTTP version.
127134
pub const fn version(&self) -> u8 {
128135
self.version
129136
}
130137

138+
/// Returns the response code.
131139
pub const fn code(&self) -> u16 {
132140
self.code
133141
}
134142

143+
/// Returns the reason-phrase.
135144
pub const fn reason(&self) -> &'buf str {
136145
self.reason
137146
}
138147

148+
/// Returns the headers.
139149
pub const fn headers(&self) -> &[Header<'buf>] {
140150
&self.headers
141151
}
@@ -252,6 +262,7 @@ impl Encoder<OutRequest<'_, '_>> for OutRequestCodec {
252262
}
253263
}
254264

265+
/// An HTTP request.
255266
#[derive(Debug)]
256267
pub struct Request<'buf, const N: usize> {
257268
/// The request method, such as `GET`.
@@ -265,6 +276,7 @@ pub struct Request<'buf, const N: usize> {
265276
}
266277

267278
impl<'buf, const N: usize> Request<'buf, N> {
279+
/// Creates a new [`Request`].
268280
pub const fn new(
269281
method: &'buf str,
270282
path: &'buf str,
@@ -279,18 +291,22 @@ impl<'buf, const N: usize> Request<'buf, N> {
279291
}
280292
}
281293

294+
/// Returns the request method.
282295
pub const fn method(&self) -> &'buf str {
283296
self.method
284297
}
285298

299+
/// Returns the request path.
286300
pub const fn path(&self) -> &'buf str {
287301
self.path
288302
}
289303

304+
/// Returns the HTTP version.
290305
pub const fn version(&self) -> u8 {
291306
self.version
292307
}
293308

309+
/// Returns the headers.
294310
pub const fn headers(&self) -> &[Header<'buf>] {
295311
&self.headers
296312
}

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! `zerocopy`, `async`, `no_std` and [`autobahn`](https://github.com/crossbario/autobahn-testsuite) compliant `websockets` implementation.
22
3+
// TODO: examples
4+
35
#![no_std]
46
#![deny(missing_debug_implementations)]
5-
// #![deny(missing_docs)]
7+
#![deny(missing_docs)]
68
#![cfg_attr(docsrs, feature(doc_cfg))]
79

810
mod close_code;

src/macros.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// TODO: Add documentation for this macro and examples and why to use it.
2+
3+
// TODO: remove this after adding docs.
4+
#![allow(missing_docs)]
5+
16
#[macro_export]
27
macro_rules! next {
38
($websocketz:expr) => {{

src/message.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::{CloseFrame, Frame, OpCode, error::FragmentationError, fragments::FragmentsIterator};
22

3+
/// A WebSocket message.
34
#[derive(Debug)]
45
pub enum Message<'a> {
6+
/// A text WebSocket message
57
Text(&'a str),
68
/// A binary WebSocket message
79
Binary(&'a [u8]),

0 commit comments

Comments
 (0)