Skip to content

Commit c9c19de

Browse files
authored
Merge pull request #1 from JadKHaddad/docs
docs: documented everything
2 parents c0c2bf8 + 0477e19 commit c9c19de

13 files changed

Lines changed: 801 additions & 6 deletions

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "websocketz"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
edition = "2024"
55
rust-version = "1.85.1"
66
authors = ["Jad K. Haddad <[email protected]>"]

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
`zerocopy`, `async`, `no_std` and [`autobahn`](https://github.com/crossbario/autobahn-testsuite) compliant `websockets` implementation.
1212

13+
Please refer to the [Documentation](https://docs.rs/websocketz) for more information.
14+
1315
## License
1416

1517
Licensed under either of

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: 71 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,10 @@ pub enum WriteError<I> {
104146
),
105147
}
106148

149+
/// Error establishing a WebSocket handshake.
150+
///
151+
/// # Generic Parameters
152+
/// `E`: User-defined error type for custom errors during the handshake.
107153
#[derive(Debug, thiserror::Error)]
108154
pub enum HandshakeError<E = Infallible> {
109155
/// Use of the wrong HTTP method (the WebSocket protocol requires the GET method to be used).
@@ -112,52 +158,75 @@ pub enum HandshakeError<E = Infallible> {
112158
/// Wrong HTTP version used (the WebSocket protocol requires version 1.1 or higher).
113159
#[error("HTTP version must be 1.1 or higher")]
114160
WrongHttpVersion,
161+
/// Connection was closed during the handshake.
115162
#[error("Connection closed during handshake")]
116163
ConnectionClosed,
164+
/// Invalid status code. (Should be 101 for switching protocols.)
117165
#[error("Invalid status code")]
118166
InvalidStatusCode,
167+
/// Missing or invalid (`Upgrade`: `websocket`) header.
119168
#[error("Missing or invalid upgrade header")]
120169
MissingOrInvalidUpgrade,
170+
/// Missing or invalid (`Connection`: `upgrade`) header.
121171
#[error("Missing or invalid connection header")]
122172
MissingOrInvalidConnection,
173+
/// Missing or invalid (`Sec-WebSocket-Accept`) header.
123174
#[error("Missing or invalid sec websocket accept header")]
124175
MissingOrInvalidAccept,
176+
/// Missing or invalid (`Sec-WebSocket-Version`) header.
125177
#[error("Missing or invalid sec websocket version header")]
126178
MissingOrInvalidSecVersion,
179+
/// Missing (`Sec-WebSocket-Key`) header.
127180
#[error("Missing sec websocket key header")]
128181
MissingSecKey,
182+
/// Other error.
183+
///
184+
/// User-defined error type.
129185
#[error("Other: {0}")]
130186
Other(E),
131187
}
132188

189+
/// Fragmentation error.
133190
#[derive(Debug, thiserror::Error)]
134191
pub enum FragmentationError {
192+
/// Fragment size is zero.
135193
#[error("Fragment size must be greater than 0")]
136194
InvalidFragmentSize,
195+
/// Error indicating that a message type that cannot be fragmented was attempted to be fragmented.
196+
///
197+
/// Only text and binary messages can be fragmented.
137198
#[error("Only text and binary messages can be fragmented")]
138199
CanNotBeFragmented,
139200
}
140201

202+
/// General WebSocket error type.
203+
///
204+
/// # Generic Parameters
205+
/// `E`: User-defined error type for custom errors during the handshake.
141206
#[derive(Debug, thiserror::Error)]
142207
pub enum Error<I, E = Infallible> {
208+
/// Error reading from the WebSocket connection.
143209
#[error("Read error: {0}")]
144210
Read(
145211
#[from]
146212
#[source]
147213
ReadError<I>,
148214
),
215+
/// Error writing to the WebSocket connection.
149216
#[error("Write error: {0}")]
150217
Write(
151218
#[from]
152219
#[source]
153220
WriteError<I>,
154221
),
222+
/// Handshake error.
155223
#[error("Handshake error: {0}")]
156224
Handshake(
157225
#[from]
158226
#[source]
159227
HandshakeError<E>,
160228
),
229+
/// Fragmentation error.
161230
#[error("Fragment error: {0}")]
162231
Fragmentation(
163232
#[from]

0 commit comments

Comments
 (0)