Skip to content

Commit

Permalink
Annotate ItemStorage fields (FuelLabs#2588)
Browse files Browse the repository at this point in the history
Co-authored-by: JC <[email protected]>
  • Loading branch information
AlicanC and AlicanC authored Aug 19, 2022
1 parent 9907c16 commit 75be099
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 46 deletions.
2 changes: 1 addition & 1 deletion sway-ast/src/item/item_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::priv_prelude::*;
#[derive(Clone, Debug)]
pub struct ItemStorage {
pub storage_token: StorageToken,
pub fields: Braces<Punctuated<StorageField, CommaToken>>,
pub fields: Braces<Punctuated<Annotated<StorageField>, CommaToken>>,
}

impl Spanned for ItemStorage {
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/convert_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ fn item_storage_to_storage_declaration(
.fields
.into_inner()
.into_iter()
.map(|storage_field| storage_field_to_storage_field(ec, storage_field))
.map(|storage_field| storage_field_to_storage_field(ec, storage_field.value))
.collect::<Result<_, _>>()?;

// Make sure each storage field is declared once
Expand Down
7 changes: 6 additions & 1 deletion sway-fmt-v2/src/items/item_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ impl Format for ItemStorage {
match formatter.config.structures.field_alignment {
FieldAlignment::AlignFields(storage_field_align_threshold) => {
writeln!(formatted_code)?;
let value_pairs = &fields.value_separator_pairs;
let value_pairs = &fields
.value_separator_pairs
.iter()
// TODO: Handle annotations instead of stripping them
.map(|pair| (&pair.0.value, &pair.1))
.collect::<Vec<_>>();
// In first iteration we are going to be collecting the lengths of the struct fields.
let field_length: Vec<usize> = value_pairs
.iter()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[package]]
name = 'core'
source = 'path+from-root-68219769D077ACAE'
dependencies = []

[[package]]
name = 'doc_comments'
source = 'root'
dependencies = ['std']

[[package]]
name = 'std'
source = 'path+from-root-68219769D077ACAE'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "doc_comments"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
contract;

/// Enum representing either a number or a string
///
/// # Examples
///
/// `NumberOrString::Number(42)`
/// `NumberOrString::String("foo")`
enum NumberOrString {
/// The `Number` variant in `NumberOrString`
Number: u64,
/// The `String` variant in `NumberOrString`
String: str[4],
}

/// Struct holding:
///
/// 1. A `value` of type `NumberOrString`
/// 2. An `address` of type `u64`
struct Data {
/// The `value` field in `Data`
value: NumberOrString,
/// The `address` field in `Data`
address: u64,
}

/// This is the `FooABI` abi
abi FooABI {
/// This is the `main` method on the `FooABI` abi
fn main() -> u64;
}

/// Storage fields for the contract
storage {
/// A `u64` field
field_a: u64 = 0,
/// An `str` field
field_b: str[4] = "aaaa",
}

/// The implementation of the `FooABI` abi
impl FooABI for Contract {
/// The main function that does all the things!
fn main() -> u64 {
let mut data = Data {
value: NumberOrString::Number(20),
address: 1337,
};

return 20;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
category = "compile"
validate_abi = false

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 75be099

Please sign in to comment.