Skip to main content

open_net/api/ws/
request.rs

1use super::{Message, RequestId};
2use crate::Metadata;
3use std::fmt;
4
5/// Business request to send, including an identifier, body, and metadata.
6///
7/// `Debug` summarizes the identifier, body, and metadata size without exposing their contents.
8#[derive(Clone)]
9pub struct Request {
10    /// Identifier used to correlate the business response.
11    id: RequestId,
12    /// Text or binary body sent on the wire.
13    message: Message,
14    /// Additional metadata; size validation occurs during send admission.
15    metadata: Metadata,
16}
17
18impl fmt::Debug for Request {
19    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
20        formatter
21            .debug_struct("Request")
22            .field("id", &self.id)
23            .field("message", &self.message)
24            .field("metadata_entries", &self.metadata.len())
25            .finish()
26    }
27}
28
29impl Request {
30    /// Creates a request with empty metadata.
31    pub fn new(id: RequestId, message: Message) -> Self {
32        Self {
33            id,
34            message,
35            metadata: Metadata::new(),
36        }
37    }
38
39    /// Replaces the request metadata.
40    pub fn with_metadata(mut self, metadata: Metadata) -> Self {
41        self.metadata = metadata;
42        self
43    }
44
45    /// Borrows the request identifier.
46    pub fn id(&self) -> &RequestId {
47        &self.id
48    }
49
50    /// Borrows the request body.
51    pub fn message(&self) -> &Message {
52        &self.message
53    }
54
55    /// Borrows request metadata.
56    pub fn metadata(&self) -> &Metadata {
57        &self.metadata
58    }
59
60    /// Consumes the request and returns its components.
61    pub fn into_parts(self) -> (RequestId, Message, Metadata) {
62        (self.id, self.message, self.metadata)
63    }
64}