Skip to content

Commit 905d3b4

Browse files
Merge pull request #627 from swimos/docs
Tidy up of crate/module API: Stage 1
2 parents f1929ef + b9bb0c1 commit 905d3b4

File tree

260 files changed

+2761
-1795
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

260 files changed

+2761
-1795
lines changed

api/formats/swimos_msgpack/src/lib.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,43 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
//! MessagePack support for Swim serialization.
16+
//!
17+
//! Provides a MessagesPack backend for the Swim serialization system. This consists of two parts:
18+
//!
19+
//! - A function [`read_from_msg_pack`] that will attempt to deserialize any type that implements
20+
//! [`swimos_form::structural::read::StructuralReadable`] from a buffer containing MessagePack data.
21+
//! - The type [`MsgPackInterpreter`] that implements [`swimos_form::structural::write::StructuralWriter`]
22+
//! allowing any type that implements [`swimos_form::structural::write::StructuralWritable`] to be
23+
//! serialized as MessagePack.
24+
//!
25+
//! # Examples
26+
//!
27+
//! ```
28+
//! use bytes::{BufMut, BytesMut};
29+
//! use swimos_form::structural::write::StructuralWritable;
30+
//! use swimos_msgpack::{read_from_msg_pack, MsgPackInterpreter};
31+
//!
32+
//! let mut buffer = BytesMut::with_capacity(128);
33+
//! let data = vec!["first".to_owned(), "second".to_owned(), "third".to_owned()];
34+
//! let mut writer = (&mut buffer).writer();
35+
//!
36+
//! let interpreter = MsgPackInterpreter::new(&mut writer);
37+
//! assert!(data.write_with(interpreter).is_ok());
38+
//!
39+
//! let mut bytes = buffer.split().freeze();
40+
//! let restored = read_from_msg_pack::<Vec<String>, _>(&mut bytes);
41+
//!
42+
//! assert_eq!(restored, Ok(data));
43+
//! ```
44+
1545
mod reader;
1646
#[cfg(test)]
1747
mod tests;
1848
mod writer;
1949

2050
pub use reader::{read_from_msg_pack, MsgPackReadError};
21-
pub use writer::{MsgPackBodyInterpreter, MsgPackInterpreter, MsgPackWriteError};
51+
pub use writer::{MsgPackInterpreter, MsgPackWriteError};
2252

2353
const BIG_INT_EXT: i8 = 0;
2454
const BIG_UINT_EXT: i8 = 1;

api/formats/swimos_msgpack/src/reader/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ fn feed<T, E: Into<MsgPackReadError>>(maybe: Option<Result<T, E>>) -> Result<(),
5252
}
5353

5454
/// Attempt to read a [`StructuralReadable`] type from MessagePack data in a buffer.
55+
///
56+
/// # Arguments
57+
/// * `input` - The buffer containing the MessagePack data.
5558
pub fn read_from_msg_pack<T: StructuralReadable, R: Buf>(
5659
input: &mut R,
5760
) -> Result<T, MsgPackReadError> {
@@ -179,9 +182,11 @@ where
179182
})
180183
}
181184

185+
/// Reading MessagePack data can fail if the bytes do not constitute valid MessagePack or the buffer contains
186+
/// an incomplete record.
182187
#[derive(Debug, PartialEq)]
183188
pub enum MsgPackReadError {
184-
/// The parsed strucuture was not valid for the target type.
189+
/// The parsed structure was not valid for the target type.
185190
Structure(ReadError),
186191
/// The MessagePack data contained invalid UTF8 in a string.
187192
StringDecode(Utf8Error),

api/formats/swimos_recon/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ edition = "2021"
66

77
[features]
88
default = []
9-
async_parser = ["futures", "tokio", "tokio-util", "bytes"]
109

1110
[dependencies]
1211
base64 = { workspace = true }
1312
either = { workspace = true }
1413
swimos_form = { path = "../../swimos_form" }
1514
swimos_model = { path = "../../swimos_model" }
15+
swimos_utilities = { path = "../../../swimos_utilities", features = ["encoding"] }
1616
nom = { workspace = true }
1717
nom_locate = { workspace = true }
1818
num-traits = { workspace = true }
1919
ryu = "1.0"
20-
futures = { workspace = true, optional = true }
21-
tokio = { workspace = true, features = ["io-util"], optional = true }
22-
tokio-util = { workspace = true, optional = true, features = ["codec"] }
23-
bytes = { workspace = true, optional = true }
20+
futures = { workspace = true }
21+
tokio = { workspace = true, features = ["io-util"] }
22+
tokio-util = { workspace = true, features = ["codec"] }
23+
bytes = { workspace = true }
2424
smallvec = { workspace = true }
2525
thiserror = { workspace = true }
2626

api/formats/swimos_recon/src/comparator/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::parser::Span;
15+
use crate::recon_parser::{record::ParseIterator, Span};
1616
use smallvec::SmallVec;
1717
use std::iter::Peekable;
1818
use swimos_form::structural::read::event::ReadEvent;
@@ -24,10 +24,10 @@ mod tests;
2424
///
2525
/// * `first` - The first recon value.
2626
/// * `second` - The second recon value.
27-
pub fn compare_values(first: &str, second: &str) -> bool {
27+
pub fn compare_recon_values(first: &str, second: &str) -> bool {
2828
match incremental_compare(
29-
&mut crate::parser::ParseIterator::new(Span::new(first), false).peekable(),
30-
&mut crate::parser::ParseIterator::new(Span::new(second), false).peekable(),
29+
&mut ParseIterator::new(Span::new(first), false).peekable(),
30+
&mut ParseIterator::new(Span::new(second), false).peekable(),
3131
) {
3232
Some(eq) => eq,
3333
None => first == second,

api/formats/swimos_recon/src/comparator/tests.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::comparator::{compare_values, incremental_compare};
16-
use crate::parser::{ParseError, ParseIterator, Span};
15+
use crate::comparator::{compare_recon_values, incremental_compare};
16+
use crate::recon_parser::record::ParseIterator;
17+
use crate::recon_parser::{parse_recognize, ParseError, Span};
1718
use nom::error::ErrorKind;
1819
use std::borrow::Cow;
1920
use swimos_form::structural::read::event::{NumericValue, ReadEvent};
2021
use swimos_model::Value;
2122

2223
fn value_from_string(rep: &str) -> Result<Value, ParseError> {
23-
let span = Span::new(rep);
24-
crate::parser::parse_recognize(span, false)
24+
parse_recognize(rep, false)
2525
}
2626

2727
#[test]
2828
fn cmp_simple() {
2929
let first = "@name(a: 1, b: 2)";
3030
let second = "@name(a: 1, b: 2)";
3131

32-
assert!(compare_values(first, second));
32+
assert!(compare_recon_values(first, second));
3333

3434
let result_1 = value_from_string(first).unwrap();
3535
let result_2 = value_from_string(second).unwrap();
@@ -38,7 +38,7 @@ fn cmp_simple() {
3838
let first = "\"test\"";
3939
let second = "test";
4040

41-
assert!(compare_values(first, second));
41+
assert!(compare_recon_values(first, second));
4242

4343
let result_1 = value_from_string(first).unwrap();
4444
let result_2 = value_from_string(second).unwrap();
@@ -50,7 +50,7 @@ fn cmp_complex() {
5050
let first = "{a:2}";
5151
let second = "{ a: 2 }";
5252

53-
assert!(compare_values(first, second));
53+
assert!(compare_recon_values(first, second));
5454

5555
let result_1 = value_from_string(first).unwrap();
5656
let result_2 = value_from_string(second).unwrap();
@@ -59,7 +59,7 @@ fn cmp_complex() {
5959
let first = "@tag(){}:1";
6060
let second = "@tag{}:1";
6161

62-
assert!(compare_values(first, second));
62+
assert!(compare_recon_values(first, second));
6363

6464
let result_1 = value_from_string(first).unwrap();
6565
let result_2 = value_from_string(second).unwrap();
@@ -68,7 +68,7 @@ fn cmp_complex() {
6868
let first = "@name(a: 1, b: 2)";
6969
let second = "@name({a: 1, b: 2})";
7070

71-
assert!(compare_values(first, second));
71+
assert!(compare_recon_values(first, second));
7272

7373
let result_1 = value_from_string(first).unwrap();
7474
let result_2 = value_from_string(second).unwrap();
@@ -77,7 +77,7 @@ fn cmp_complex() {
7777
let first = "@first(1)@second(2)";
7878
let second = "@first(1)@second(2) {}";
7979

80-
assert!(compare_values(first, second));
80+
assert!(compare_recon_values(first, second));
8181

8282
let result_1 = value_from_string(first).unwrap();
8383
let result_2 = value_from_string(second).unwrap();
@@ -86,7 +86,7 @@ fn cmp_complex() {
8686
let first = "{ @inner(0), after }";
8787
let second = "{ @inner(0) {}, after }";
8888

89-
assert!(compare_values(first, second));
89+
assert!(compare_recon_values(first, second));
9090

9191
let result_1 = value_from_string(first).unwrap();
9292
let result_2 = value_from_string(second).unwrap();
@@ -95,7 +95,7 @@ fn cmp_complex() {
9595
let first = "@outer(@inner)";
9696
let second = "@outer(@inner {})";
9797

98-
assert!(compare_values(first, second));
98+
assert!(compare_recon_values(first, second));
9999

100100
let result_1 = value_from_string(first).unwrap();
101101
let result_2 = value_from_string(second).unwrap();
@@ -104,7 +104,7 @@ fn cmp_complex() {
104104
let first = "@foo({one: 1, two: @bar(1,2,3), three: 3, four: {@baz({1,2})}})";
105105
let second = "@foo(one: 1, two: @bar({1,2,3}), three: 3, four: {@baz(1,2)})";
106106

107-
assert!(compare_values(first, second));
107+
assert!(compare_recon_values(first, second));
108108

109109
let result_1 = value_from_string(first).unwrap();
110110
let result_2 = value_from_string(second).unwrap();
@@ -113,7 +113,7 @@ fn cmp_complex() {
113113
let first = "@foo(1,2)";
114114
let second = "@foo({1,2})";
115115

116-
assert!(compare_values(first, second));
116+
assert!(compare_recon_values(first, second));
117117

118118
let result_1 = value_from_string(first).unwrap();
119119
let result_2 = value_from_string(second).unwrap();
@@ -122,7 +122,7 @@ fn cmp_complex() {
122122
let first = "@foo({one: 1, two: @bar(1,2,3)})";
123123
let second = "@foo(one: 1, two: @bar({1,2,3}))";
124124

125-
assert!(compare_values(first, second));
125+
assert!(compare_recon_values(first, second));
126126

127127
let result_1 = value_from_string(first).unwrap();
128128
let result_2 = value_from_string(second).unwrap();
@@ -131,7 +131,7 @@ fn cmp_complex() {
131131
let first = "@name(@foo,@bar)";
132132
let second = "@name({@foo, @bar})";
133133

134-
assert!(compare_values(first, second));
134+
assert!(compare_recon_values(first, second));
135135

136136
let result_1 = value_from_string(first).unwrap();
137137
let result_2 = value_from_string(second).unwrap();
@@ -140,7 +140,7 @@ fn cmp_complex() {
140140
let first = "@name(1, @foo@bar)";
141141
let second = "@name({1, @foo@bar})";
142142

143-
assert!(compare_values(first, second));
143+
assert!(compare_recon_values(first, second));
144144

145145
let result_1 = value_from_string(first).unwrap();
146146
let result_2 = value_from_string(second).unwrap();
@@ -149,7 +149,7 @@ fn cmp_complex() {
149149
let first = "@name(@foo@bar({@bar, 1, @baz()}), @bar@foo()@baz)";
150150
let second = "@name({@foo@bar(@bar, 1, @baz()), @bar@foo()@baz})";
151151

152-
assert!(compare_values(first, second));
152+
assert!(compare_recon_values(first, second));
153153

154154
let result_1 = value_from_string(first).unwrap();
155155
let result_2 = value_from_string(second).unwrap();
@@ -158,7 +158,7 @@ fn cmp_complex() {
158158
let first = "@foo(1, @bar(2,3), 4, 5)";
159159
let second = "@foo(1, @bar({2,3}), 4, 5)";
160160

161-
assert!(compare_values(first, second));
161+
assert!(compare_recon_values(first, second));
162162

163163
let result_1 = value_from_string(first).unwrap();
164164
let result_2 = value_from_string(second).unwrap();
@@ -167,7 +167,7 @@ fn cmp_complex() {
167167
let first = "@name(a: @foo)";
168168
let second = "@name({a: @foo})";
169169

170-
assert!(compare_values(first, second));
170+
assert!(compare_recon_values(first, second));
171171

172172
let result_1 = value_from_string(first).unwrap();
173173
let result_2 = value_from_string(second).unwrap();
@@ -176,7 +176,7 @@ fn cmp_complex() {
176176
let first = "@name(b: @foo@bar@baz, @foo(1,2), @bar@baz))";
177177
let second = "@name({b: @foo@bar@baz, @foo({1,2}), @bar@baz})";
178178

179-
assert!(compare_values(first, second));
179+
assert!(compare_recon_values(first, second));
180180

181181
let result_1 = value_from_string(first).unwrap();
182182
let result_2 = value_from_string(second).unwrap();
@@ -185,7 +185,7 @@ fn cmp_complex() {
185185
let first = "@name(b: )";
186186
let second = "@name({b: })";
187187

188-
assert!(compare_values(first, second));
188+
assert!(compare_recon_values(first, second));
189189

190190
let result_1 = value_from_string(first).unwrap();
191191
let result_2 = value_from_string(second).unwrap();
@@ -326,34 +326,34 @@ fn cmp_early_termination_complex() {
326326
fn cmp_invalid_eq() {
327327
let first = ":5";
328328
let second = ":5";
329-
assert!(compare_values(first, second));
329+
assert!(compare_recon_values(first, second));
330330

331331
let first = ")10";
332332
let second = ")10";
333-
assert!(compare_values(first, second));
333+
assert!(compare_recon_values(first, second));
334334

335335
let first = "@foo}20";
336336
let second = "@foo}20";
337-
assert!(compare_values(first, second));
337+
assert!(compare_recon_values(first, second));
338338
}
339339

340340
#[test]
341341
fn cmp_invalid_not_eq() {
342342
let first = ":5, 30";
343343
let second = ":10, 30";
344-
assert!(!compare_values(first, second));
344+
assert!(!compare_recon_values(first, second));
345345

346346
let first = ")15";
347347
let second = ")20";
348-
assert!(!compare_values(first, second));
348+
assert!(!compare_recon_values(first, second));
349349

350350
let first = "@foo-30";
351351
let second = "@foo-35";
352-
assert!(!compare_values(first, second));
352+
assert!(!compare_recon_values(first, second));
353353

354354
let first = "@foo(1, 2, 3){-}";
355355
let second = "@foo({1, 2, 3}){-}";
356-
assert!(!compare_values(first, second));
356+
assert!(!compare_recon_values(first, second));
357357
}
358358

359359
#[test]

0 commit comments

Comments
 (0)