aws_smithy_types/
event_stream.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Types relevant to event stream serialization/deserialization
7
8use crate::str_bytes::StrBytes;
9use bytes::Bytes;
10
11mod value {
12    use crate::str_bytes::StrBytes;
13    use crate::DateTime;
14    use bytes::Bytes;
15
16    /// Event Stream frame header value.
17    #[non_exhaustive]
18    #[derive(Clone, Debug, PartialEq)]
19    pub enum HeaderValue {
20        /// Represents a boolean value.
21        Bool(bool),
22        /// Represents a byte value.
23        Byte(i8),
24        /// Represents an int16 value.
25        Int16(i16),
26        /// Represents an int32 value.
27        Int32(i32),
28        /// Represents an int64 value.
29        Int64(i64),
30        /// Represents a byte array value.
31        ByteArray(Bytes),
32        /// Represents a string value.
33        String(StrBytes),
34        /// Represents a timestamp value.
35        Timestamp(DateTime),
36        /// Represents a uuid value.
37        Uuid(u128),
38    }
39
40    impl HeaderValue {
41        /// If the `HeaderValue` is a `Bool`, returns the associated `bool`. Returns `Err` otherwise.
42        pub fn as_bool(&self) -> Result<bool, &Self> {
43            match self {
44                HeaderValue::Bool(value) => Ok(*value),
45                _ => Err(self),
46            }
47        }
48
49        /// If the `HeaderValue` is a `Byte`, returns the associated `i8`. Returns `Err` otherwise.
50        pub fn as_byte(&self) -> Result<i8, &Self> {
51            match self {
52                HeaderValue::Byte(value) => Ok(*value),
53                _ => Err(self),
54            }
55        }
56
57        /// If the `HeaderValue` is an `Int16`, returns the associated `i16`. Returns `Err` otherwise.
58        pub fn as_int16(&self) -> Result<i16, &Self> {
59            match self {
60                HeaderValue::Int16(value) => Ok(*value),
61                _ => Err(self),
62            }
63        }
64
65        /// If the `HeaderValue` is an `Int32`, returns the associated `i32`. Returns `Err` otherwise.
66        pub fn as_int32(&self) -> Result<i32, &Self> {
67            match self {
68                HeaderValue::Int32(value) => Ok(*value),
69                _ => Err(self),
70            }
71        }
72
73        /// If the `HeaderValue` is an `Int64`, returns the associated `i64`. Returns `Err` otherwise.
74        pub fn as_int64(&self) -> Result<i64, &Self> {
75            match self {
76                HeaderValue::Int64(value) => Ok(*value),
77                _ => Err(self),
78            }
79        }
80
81        /// If the `HeaderValue` is a `ByteArray`, returns the associated [`Bytes`]. Returns `Err` otherwise.
82        pub fn as_byte_array(&self) -> Result<&Bytes, &Self> {
83            match self {
84                HeaderValue::ByteArray(value) => Ok(value),
85                _ => Err(self),
86            }
87        }
88
89        /// If the `HeaderValue` is a `String`, returns the associated [`StrBytes`]. Returns `Err` otherwise.
90        pub fn as_string(&self) -> Result<&StrBytes, &Self> {
91            match self {
92                HeaderValue::String(value) => Ok(value),
93                _ => Err(self),
94            }
95        }
96
97        /// If the `HeaderValue` is a `Timestamp`, returns the associated [`DateTime`]. Returns `Err` otherwise.
98        pub fn as_timestamp(&self) -> Result<DateTime, &Self> {
99            match self {
100                HeaderValue::Timestamp(value) => Ok(*value),
101                _ => Err(self),
102            }
103        }
104
105        /// If the `HeaderValue` is a `Uuid`, returns the associated `u128`. Returns `Err` otherwise.
106        pub fn as_uuid(&self) -> Result<u128, &Self> {
107            match self {
108                HeaderValue::Uuid(value) => Ok(*value),
109                _ => Err(self),
110            }
111        }
112    }
113}
114
115pub use value::HeaderValue;
116
117/// Event Stream header.
118#[non_exhaustive]
119#[derive(Clone, Debug, PartialEq)]
120pub struct Header {
121    name: StrBytes,
122    value: HeaderValue,
123}
124
125impl Header {
126    /// Creates a new header with the given `name` and `value`.
127    pub fn new(name: impl Into<StrBytes>, value: impl Into<HeaderValue>) -> Header {
128        Header {
129            name: name.into(),
130            value: value.into(),
131        }
132    }
133
134    /// Returns the header name.
135    pub fn name(&self) -> &StrBytes {
136        &self.name
137    }
138
139    /// Returns the header value.
140    pub fn value(&self) -> &HeaderValue {
141        &self.value
142    }
143}
144
145/// Event Stream message.
146#[non_exhaustive]
147#[derive(Clone, Debug, PartialEq)]
148pub struct Message {
149    headers: Vec<Header>,
150    payload: Bytes,
151}
152
153impl Message {
154    /// Creates a new message with the given `payload`. Headers can be added later.
155    pub fn new(payload: impl Into<Bytes>) -> Message {
156        Message {
157            headers: Vec::new(),
158            payload: payload.into(),
159        }
160    }
161
162    /// Creates a message with the given `headers` and `payload`.
163    pub fn new_from_parts(headers: Vec<Header>, payload: impl Into<Bytes>) -> Self {
164        Self {
165            headers,
166            payload: payload.into(),
167        }
168    }
169
170    /// Adds a header to the message.
171    pub fn add_header(mut self, header: Header) -> Self {
172        self.headers.push(header);
173        self
174    }
175
176    /// Returns all headers.
177    pub fn headers(&self) -> &[Header] {
178        &self.headers
179    }
180
181    /// Returns the payload bytes.
182    pub fn payload(&self) -> &Bytes {
183        &self.payload
184    }
185}
186
187/// Raw message from an event stream receiver when a response error is encountered.
188#[derive(Debug)]
189#[non_exhaustive]
190pub enum RawMessage {
191    /// Message was decoded into a valid frame, but failed to unmarshall into a modeled type.
192    Decoded(Message),
193    /// Message failed to be decoded into a valid frame. The raw bytes may not be available in the
194    /// case where decoding consumed the buffer.
195    Invalid(Option<Bytes>),
196}
197
198impl RawMessage {
199    /// Creates a `RawMessage` for failure to decode a message into a valid frame.
200    pub fn invalid(bytes: Option<Bytes>) -> Self {
201        Self::Invalid(bytes)
202    }
203}