From c6d8d1bd70262950cf9245cdb62bb57317141d9e Mon Sep 17 00:00:00 2001 From: Serkyo Date: Mon, 13 Jul 2026 19:56:19 +0200 Subject: [PATCH] refactor(net): move NetError into its own error module --- crates/net/src/codec.rs | 40 +-------------------------------- crates/net/src/endpoint.rs | 2 +- crates/net/src/error.rs | 45 ++++++++++++++++++++++++++++++++++++++ crates/net/src/lib.rs | 1 + 4 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 crates/net/src/error.rs diff --git a/crates/net/src/codec.rs b/crates/net/src/codec.rs index 2ee541a..b38d8f3 100644 --- a/crates/net/src/codec.rs +++ b/crates/net/src/codec.rs @@ -4,45 +4,7 @@ //! //! Encodes and decodes one logical protocol message per record on a QUIC stream, using a length prefix so a reader can recover record boundaries from a byte stream. -/// Errors produced by the framing codec and its stream I/O helpers. -#[derive(Debug, thiserror::Error)] -pub enum NetError { - /// A `postcard` serialization or deserialization operation failed. - #[error("postcard codec error: {0}")] - Postcard(#[from] postcard::Error), - /// An underlying byte-stream I/O operation failed. - #[error("i/o error: {0}")] - Io(#[from] std::io::Error), - /// A declared frame length exceeded the caller-supplied maximum, indicating a malicious or corrupt peer. - #[error("frame length {len} exceeds maximum {max}")] - FrameTooLarge { - /// The frame length declared by the length prefix, in bytes. - len: u64, - /// The maximum payload length accepted by the reader, in bytes. - max: usize, - }, - /// The stream ended before a complete frame (prefix or payload) had been read. - #[error("unexpected end of stream while reading a frame")] - UnexpectedEof, - /// A varint length prefix was malformed: either overlong or otherwise invalid. - #[error("malformed varint length prefix")] - MalformedVarint, - /// Writing bytes to a quinn send stream failed. - #[error("quinn write error: {0}")] - Write(#[from] quinn::WriteError), - /// Reading an exact number of bytes from a quinn recv stream failed. - #[error("quinn read error: {0}")] - Read(#[from] quinn::ReadExactError), - /// Generation of the self-signed server certificate failed. - #[error("certificate generation error: {0}")] - Rcgen(#[from] rcgen::Error), - /// Construction of the `rustls` TLS configuration failed. - #[error("rustls configuration error: {0}")] - Rustls(#[from] rustls::Error), - /// The `rustls` configuration lacked a TLS 1.3 cipher suite, which QUIC requires. - #[error("no initial cipher suite for quic: {0}")] - NoInitialCipherSuite(#[from] quinn::crypto::rustls::NoInitialCipherSuite), -} +use crate::error::NetError; /// The maximum payload length, in bytes, accepted on the control stream (64 KiB), matching the mod-payload cap. Higher-bandwidth tiers such as chunk streaming define their own caps. pub const MAX_CONTROL_FRAME_LEN: usize = 64 * 1024; diff --git a/crates/net/src/endpoint.rs b/crates/net/src/endpoint.rs index a1e684f..fe865e7 100644 --- a/crates/net/src/endpoint.rs +++ b/crates/net/src/endpoint.rs @@ -15,7 +15,7 @@ use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, Server use rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer, ServerName, UnixTime}; use tracing::debug; -use crate::codec::NetError; +use crate::error::NetError; /// The Application-Layer Protocol Negotiation identifier for the Synvael protocol. pub const ALPN: &[u8] = b"synvael"; diff --git a/crates/net/src/error.rs b/crates/net/src/error.rs new file mode 100644 index 0000000..00e2e4e --- /dev/null +++ b/crates/net/src/error.rs @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! Error types for the net crate. + +use thiserror::Error; + +/// Errors produced by the framing codec, endpoint construction, and stream I/O helpers. +#[derive(Debug, Error)] +pub enum NetError { + /// A `postcard` serialization or deserialization operation failed. + #[error("postcard codec error: {0}")] + Postcard(#[from] postcard::Error), + /// An underlying byte-stream I/O operation failed. + #[error("i/o error: {0}")] + Io(#[from] std::io::Error), + /// A declared frame length exceeded the caller-supplied maximum, indicating a malicious or corrupt peer. + #[error("frame length {len} exceeds maximum {max}")] + FrameTooLarge { + /// The frame length declared by the length prefix, in bytes. + len: u64, + /// The maximum payload length accepted by the reader, in bytes. + max: usize, + }, + /// The stream ended before a complete frame (prefix or payload) had been read. + #[error("unexpected end of stream while reading a frame")] + UnexpectedEof, + /// A varint length prefix was malformed: either overlong or otherwise invalid. + #[error("malformed varint length prefix")] + MalformedVarint, + /// Writing bytes to a quinn send stream failed. + #[error("quinn write error: {0}")] + Write(#[from] quinn::WriteError), + /// Reading an exact number of bytes from a quinn recv stream failed. + #[error("quinn read error: {0}")] + Read(#[from] quinn::ReadExactError), + /// Generation of the self-signed server certificate failed. + #[error("certificate generation error: {0}")] + Rcgen(#[from] rcgen::Error), + /// Construction of the `rustls` TLS configuration failed. + #[error("rustls configuration error: {0}")] + Rustls(#[from] rustls::Error), + /// The `rustls` configuration lacked a TLS 1.3 cipher suite, which QUIC requires. + #[error("no initial cipher suite for quic: {0}")] + NoInitialCipherSuite(#[from] quinn::crypto::rustls::NoInitialCipherSuite), +} diff --git a/crates/net/src/lib.rs b/crates/net/src/lib.rs index 40cae1d..4cea45d 100644 --- a/crates/net/src/lib.rs +++ b/crates/net/src/lib.rs @@ -8,5 +8,6 @@ pub mod codec; pub mod endpoint; +pub mod error; pub mod handshake; pub mod runtime;