Skip to main content

grpc_webnext/
frame.rs

1//! Encode/decode a single WebSocket `Frame` (one frame per WS message).
2
3use crate::pb::Frame;
4use bytes::{Bytes, BytesMut};
5use prost::Message;
6
7#[derive(Debug, thiserror::Error)]
8pub enum FrameError {
9    #[error("failed to decode Frame: {0}")]
10    Decode(#[from] prost::DecodeError),
11}
12
13/// Decode one WebSocket binary message into a `Frame`.
14pub fn decode_frame(bytes: &[u8]) -> Result<Frame, FrameError> {
15    Ok(Frame::decode(bytes)?)
16}
17
18/// Encode a `Frame` into the bytes of one WebSocket binary message.
19pub fn encode_frame(frame: &Frame) -> Bytes {
20    let mut buf = BytesMut::with_capacity(frame.encoded_len());
21    frame.encode(&mut buf).expect("BytesMut has capacity");
22    buf.freeze()
23}