Update bindgen to use new formatting

This commit is contained in:
Richard Feldman 2022-05-22 22:11:36 -04:00
parent 0456ea4f19
commit 096b738f1e
No known key found for this signature in database
GPG key ID: 7E4127D1E4241798
3 changed files with 24 additions and 24 deletions

View file

@ -287,7 +287,7 @@ fn add_tag_union(
return match payload_vars.len() { return match payload_vars.len() {
0 => { 0 => {
// This is a single-tag union with no payload, e.g. `[ Foo ]` // This is a single-tag union with no payload, e.g. `[Foo]`
// so just generate an empty record // so just generate an empty record
types.add(RocType::Struct { types.add(RocType::Struct {
name, name,
@ -295,7 +295,7 @@ fn add_tag_union(
}) })
} }
1 => { 1 => {
// This is a single-tag union with 1 payload field, e.g.`[ Foo Str ]`. // This is a single-tag union with 1 payload field, e.g.`[Foo Str]`.
// We'll just wrap that. // We'll just wrap that.
let var = *payload_vars.get(0).unwrap(); let var = *payload_vars.get(0).unwrap();
let content = add_type(env, var, types); let content = add_type(env, var, types);
@ -303,7 +303,7 @@ fn add_tag_union(
types.add(RocType::TransparentWrapper { name, content }) types.add(RocType::TransparentWrapper { name, content })
} }
_ => { _ => {
// This is a single-tag union with multiple payload field, e.g.`[ Foo Str U32 ]`. // This is a single-tag union with multiple payload field, e.g.`[Foo Str U32]`.
// Generate a record. // Generate a record.
let fields = payload_vars.iter().enumerate().map(|(index, payload_var)| { let fields = payload_vars.iter().enumerate().map(|(index, payload_var)| {
let field_name = format!("f{}", index).into(); let field_name = format!("f{}", index).into();
@ -335,7 +335,7 @@ fn add_tag_union(
} }
1 => { 1 => {
// there's 1 payload item, so it doesn't need its own // there's 1 payload item, so it doesn't need its own
// struct - e.g. for `[ Foo Str, Bar Str ]` both of them // struct - e.g. for `[Foo Str, Bar Str]` both of them
// can have payloads of plain old Str, no struct wrapper needed. // can have payloads of plain old Str, no struct wrapper needed.
let payload_var = payload_vars.get(0).unwrap(); let payload_var = payload_vars.get(0).unwrap();
let payload_id = add_type(env, *payload_var, types); let payload_id = add_type(env, *payload_var, types);
@ -376,30 +376,30 @@ fn add_tag_union(
match union_layout { match union_layout {
// A non-recursive tag union // A non-recursive tag union
// e.g. `Result ok err : [ Ok ok, Err err ]` // e.g. `Result ok err : [Ok ok, Err err]`
NonRecursive(_) => RocType::TagUnion(RocTagUnion::NonRecursive { name, tags }), NonRecursive(_) => RocType::TagUnion(RocTagUnion::NonRecursive { name, tags }),
// A recursive tag union (general case) // A recursive tag union (general case)
// e.g. `Expr : [ Sym Str, Add Expr Expr ]` // e.g. `Expr : [Sym Str, Add Expr Expr]`
Recursive(_) => { Recursive(_) => {
todo!() todo!()
} }
// A recursive tag union with just one constructor // A recursive tag union with just one constructor
// Optimization: No need to store a tag ID (the payload is "unwrapped") // Optimization: No need to store a tag ID (the payload is "unwrapped")
// e.g. `RoseTree a : [ Tree a (List (RoseTree a)) ]` // e.g. `RoseTree a : [Tree a (List (RoseTree a))]`
NonNullableUnwrapped(_) => { NonNullableUnwrapped(_) => {
todo!() todo!()
} }
// A recursive tag union that has an empty variant // A recursive tag union that has an empty variant
// Optimization: Represent the empty variant as null pointer => no memory usage & fast comparison // Optimization: Represent the empty variant as null pointer => no memory usage & fast comparison
// It has more than one other variant, so they need tag IDs (payloads are "wrapped") // It has more than one other variant, so they need tag IDs (payloads are "wrapped")
// e.g. `FingerTree a : [ Empty, Single a, More (Some a) (FingerTree (Tuple a)) (Some a) ]` // e.g. `FingerTree a : [Empty, Single a, More (Some a) (FingerTree (Tuple a)) (Some a)]`
// see also: https://youtu.be/ip92VMpf_-A?t=164 // see also: https://youtu.be/ip92VMpf_-A?t=164
NullableWrapped { .. } => { NullableWrapped { .. } => {
todo!() todo!()
} }
// A recursive tag union with only two variants, where one is empty. // A recursive tag union with only two variants, where one is empty.
// Optimizations: Use null for the empty variant AND don't store a tag ID for the other variant. // Optimizations: Use null for the empty variant AND don't store a tag ID for the other variant.
// e.g. `ConsList a : [ Nil, Cons a (ConsList a) ]` // e.g. `ConsList a : [Nil, Cons a (ConsList a)]`
NullableUnwrapped { nullable_id, .. } => { NullableUnwrapped { nullable_id, .. } => {
// NullableUnwrapped tag unions should always have exactly 2 tags. // NullableUnwrapped tag unions should always have exactly 2 tags.
debug_assert_eq!(tags.len(), 2); debug_assert_eq!(tags.len(), 2);

View file

@ -486,20 +486,20 @@ pub enum RocTagUnion {
tags: Vec<String>, tags: Vec<String>,
}, },
/// A non-recursive tag union /// A non-recursive tag union
/// e.g. `Result a e : [ Ok a, Err e ]` /// e.g. `Result a e : [Ok a, Err e]`
NonRecursive { NonRecursive {
name: String, name: String,
tags: Vec<(String, Option<TypeId>)>, tags: Vec<(String, Option<TypeId>)>,
}, },
/// A recursive tag union (general case) /// A recursive tag union (general case)
/// e.g. `Expr : [ Sym Str, Add Expr Expr ]` /// e.g. `Expr : [Sym Str, Add Expr Expr]`
Recursive { Recursive {
name: String, name: String,
tags: Vec<(String, Option<TypeId>)>, tags: Vec<(String, Option<TypeId>)>,
}, },
/// A recursive tag union with just one constructor /// A recursive tag union with just one constructor
/// Optimization: No need to store a tag ID (the payload is "unwrapped") /// Optimization: No need to store a tag ID (the payload is "unwrapped")
/// e.g. `RoseTree a : [ Tree a (List (RoseTree a)) ]` /// e.g. `RoseTree a : [Tree a (List (RoseTree a))]`
NonNullableUnwrapped { NonNullableUnwrapped {
name: String, name: String,
content: TypeId, content: TypeId,
@ -508,7 +508,7 @@ pub enum RocTagUnion {
/// A recursive tag union that has an empty variant /// A recursive tag union that has an empty variant
/// Optimization: Represent the empty variant as null pointer => no memory usage & fast comparison /// Optimization: Represent the empty variant as null pointer => no memory usage & fast comparison
/// It has more than one other variant, so they need tag IDs (payloads are "wrapped") /// It has more than one other variant, so they need tag IDs (payloads are "wrapped")
/// e.g. `FingerTree a : [ Empty, Single a, More (Some a) (FingerTree (Tuple a)) (Some a) ]` /// e.g. `FingerTree a : [Empty, Single a, More (Some a) (FingerTree (Tuple a)) (Some a)]`
/// see also: https://youtu.be/ip92VMpf_-A?t=164 /// see also: https://youtu.be/ip92VMpf_-A?t=164
NullableWrapped { NullableWrapped {
name: String, name: String,
@ -518,12 +518,12 @@ pub enum RocTagUnion {
/// A recursive tag union with only two variants, where one is empty. /// A recursive tag union with only two variants, where one is empty.
/// Optimizations: Use null for the empty variant AND don't store a tag ID for the other variant. /// Optimizations: Use null for the empty variant AND don't store a tag ID for the other variant.
/// e.g. `ConsList a : [ Nil, Cons a (ConsList a) ]` /// e.g. `ConsList a : [Nil, Cons a (ConsList a)]`
NullableUnwrapped { NullableUnwrapped {
name: String, name: String,
/// e.g. Nil in `StrConsList : [ Nil, Cons Str (ConsList Str) ]` /// e.g. Nil in `StrConsList : [Nil, Cons Str (ConsList Str)]`
null_tag: String, null_tag: String,
/// e.g. Cons in `StrConsList : [ Nil, Cons Str (ConsList Str) ]` /// e.g. Cons in `StrConsList : [Nil, Cons Str (ConsList Str)]`
non_null_tag: String, non_null_tag: String,
/// There must be a payload associated with the non-null tag. /// There must be a payload associated with the non-null tag.
/// Otherwise, this would have been an Enumeration! /// Otherwise, this would have been an Enumeration!

View file

@ -47,7 +47,7 @@ mod test_gen_rs {
Inner : { a : U16, b : F32 } Inner : { a : U16, b : F32 }
main : Outer main : Outer
main = { x: { a: 5, b: 24 }, y: "foo", z: [ 1, 2 ] } main = { x: { a: 5, b: 24 }, y: "foo", z: [1, 2] }
"# "#
); );
@ -99,7 +99,7 @@ mod test_gen_rs {
#[test] #[test]
fn nested_record_anonymous() { fn nested_record_anonymous() {
let module = r#"main = { x: { a: 5u16, b: 24f32 }, y: "foo", z: [ 1u8, 2 ] }"#; let module = r#"main = { x: { a: 5u16, b: 24f32 }, y: "foo", z: [1u8, 2] }"#;
assert_eq!( assert_eq!(
generate_bindings(module) generate_bindings(module)
@ -130,7 +130,7 @@ mod test_gen_rs {
fn tag_union_aliased() { fn tag_union_aliased() {
let module = indoc!( let module = indoc!(
r#" r#"
NonRecursive : [ Foo Str, Bar U128, Blah I32, Baz ] NonRecursive : [Foo Str, Bar U128, Blah I32, Baz]
main : NonRecursive main : NonRecursive
main = Foo "blah" main = Foo "blah"
@ -429,7 +429,7 @@ mod test_gen_rs {
fn tag_union_enumeration() { fn tag_union_enumeration() {
let module = indoc!( let module = indoc!(
r#" r#"
Enumeration : [ Blah, Foo, Bar, ] Enumeration : [Blah, Foo, Bar,]
main : Enumeration main : Enumeration
main = Foo main = Foo
@ -468,7 +468,7 @@ mod test_gen_rs {
fn single_tag_union_with_payloads() { fn single_tag_union_with_payloads() {
let module = indoc!( let module = indoc!(
r#" r#"
UserId : [ Id U32 Str ] UserId : [Id U32 Str]
main : UserId main : UserId
main = Id 42 "blah" main = Id 42 "blah"
@ -496,7 +496,7 @@ mod test_gen_rs {
fn single_tag_union_with_one_payload_field() { fn single_tag_union_with_one_payload_field() {
let module = indoc!( let module = indoc!(
r#" r#"
UserId : [ Id Str ] UserId : [Id Str]
main : UserId main : UserId
main = Id "blah" main = Id "blah"
@ -521,7 +521,7 @@ mod test_gen_rs {
fn cons_list_of_strings() { fn cons_list_of_strings() {
let module = indoc!( let module = indoc!(
r#" r#"
StrConsList : [ Nil, Cons Str StrConsList ] StrConsList : [Nil, Cons Str StrConsList]
main : StrConsList main : StrConsList
main = Cons "Hello, " (Cons "World!" Nil) main = Cons "Hello, " (Cons "World!" Nil)
@ -657,7 +657,7 @@ mod test_gen_rs {
fn cons_list_of_ints() { fn cons_list_of_ints() {
let module = indoc!( let module = indoc!(
r#" r#"
IntConsList : [ Empty, Prepend U16 IntConsList ] IntConsList : [Empty, Prepend U16 IntConsList]
main : IntConsList main : IntConsList
main = Prepend 42 (Prepend 26 Empty) main = Prepend 42 (Prepend 26 Empty)