Skip to content

Commit 3ad43a2

Browse files
committed
Add more documentation
1 parent ff133d9 commit 3ad43a2

File tree

7 files changed

+25
-15
lines changed

7 files changed

+25
-15
lines changed

runtimes/rust/lbr-prelude-derive/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ fn impl_enum(
175175
ident: &syn::Ident,
176176
variants: &syn::punctuated::Punctuated<syn::Variant, syn::token::Comma>,
177177
) -> (proc_macro2::TokenStream, proc_macro2::TokenStream) {
178-
// Arms of the pattern match over the enum variants
179-
let to_json_pattern_match = variants.iter().map(|variant| {
178+
// Arms of the pattern match over the variants of the original data
179+
let to_json_match_arms = variants.iter().map(|variant| {
180180
let variant_ident = &variant.ident;
181181
let variant_str = variant.ident.to_string();
182182

@@ -206,13 +206,13 @@ fn impl_enum(
206206
let to_json_impl = quote! {
207207
fn to_json(&self) -> Result<serde_json::Value, lbr_prelude::error::Error> {
208208
Ok(match self {
209-
#(#to_json_pattern_match)*
209+
#(#to_json_match_arms)*
210210
})
211211
}
212212
};
213213

214-
// Arms of the pattern match over tuple containinig the key-value pair
215-
let from_json_pattern_match = variants.iter().map(|variant| {
214+
// Arms of the pattern match over the JSON name and fields
215+
let from_json_match_arms = variants.iter().map(|variant| {
216216
let variant_ident = &variant.ident;
217217
let variant_str = variant.ident.to_string();
218218

@@ -264,7 +264,7 @@ fn impl_enum(
264264
let from_json_impl = quote! {
265265
fn from_json(value: serde_json::Value) -> Result<Self, lbr_prelude::error::Error> {
266266
lbr_prelude::json::sum_parser(&value).and_then(|obj| match obj {
267-
#(#from_json_pattern_match)*
267+
#(#from_json_match_arms)*
268268
_ => Err(lbr_prelude::error::Error::UnexpectedJsonInvariant {
269269
wanted: #error_msg.to_owned(),
270270
got: "unknown constructor name".to_owned(),

runtimes/rust/lbr-prelude-derive/tests/struct.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ fn main() {
1515
age: BigInt::from(89),
1616
};
1717

18-
let mut dict = serde_json::Map::new();
19-
dict.insert("name".to_owned(), Value::String("田中太郎".to_owned()));
20-
dict.insert("age".to_owned(), Value::Number(Number::from(89)));
21-
22-
let expected = Value::Object(dict);
18+
let expected = Value::Object(serde_json::Map::from_iter([
19+
("name".to_owned(), Value::String("田中太郎".to_owned())),
20+
("age".to_owned(), Value::Number(Number::from(89))),
21+
]));
2322

2423
let actual = data.to_json().unwrap();
2524

runtimes/rust/lbr-prelude/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Common error type
12
use serde_json::Value;
23
use thiserror;
34

@@ -25,6 +26,7 @@ impl From<&Value> for JsonType {
2526
}
2627
}
2728

29+
/// Error type representing Json conversion errors
2830
#[derive(thiserror::Error, Debug)]
2931
pub enum Error {
3032
#[error("Expected a JSON type: {wanted:?}, but got a JSON type: {got:?}")]

runtimes/rust/lbr-prelude/src/generators/correct.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Proptest strategies for most common types
2+
//!
3+
//! These strategies always return valid values.
4+
15
use num_bigint::{BigInt, Sign};
26
use proptest::arbitrary::{any, StrategyFor};
37
use proptest::char::CharStrategy;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
//! Proptest strategies for most common types
12
pub mod correct;

runtimes/rust/lbr-prelude/src/json.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Json serialization of Lambda Buffers types
12
pub use crate::error::{Error, JsonType};
23
use core::str::FromStr;
34
use data_encoding::BASE64;
@@ -322,8 +323,8 @@ where
322323
}
323324
}
324325

325-
/// Construct a JSON Value from a sum type
326-
/// We always encode sum types into a `{"name": string, "fields": any[]}` format in JSON
326+
/// Construct a JSON Value from a sum type.
327+
/// We always encode sum types into a `{"name": string, "fields": any[]}` format in JSON.
327328
pub fn sum_constructor(ctor_name: &str, ctor_product: Vec<Value>) -> Value {
328329
let mut obj = serde_json::Map::new();
329330
obj.insert("name".to_owned(), Value::String(ctor_name.to_owned()));
@@ -332,8 +333,8 @@ pub fn sum_constructor(ctor_name: &str, ctor_product: Vec<Value>) -> Value {
332333
Value::Object(obj)
333334
}
334335

335-
/// Parse a JSON value into an intermediary representation of a sum type
336-
/// We always encode sum types into a `{"name": string, "fields": any[]}` format in JSON
336+
/// Parse a JSON value into an intermediary representation of a sum type.
337+
/// We always encode sum types into a `{"name": string, "fields": any[]}` format in JSON.
337338
pub fn sum_parser(value: &Value) -> Result<(&str, &Vec<Value>), Error> {
338339
match value {
339340
Value::Object(obj) => {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! Lambda Buffers Runtime Prelude
2+
//!
3+
//! Common functionality for Lamba Buffer types.
14
pub mod error;
25
pub mod generators;
36
pub mod json;

0 commit comments

Comments
 (0)