Skip to content

Commit

Permalink
Standardized module structure (FuelLabs#4232)
Browse files Browse the repository at this point in the history
## Description

Implement RFC 0006, fix FuelLabs#4191.

Remove the `dep` reseved keyword in favor of `mod`.

Change path resolution for submodules to use an adjascent file at the
root and a folder named after the current module in other cases.

Remove the need for an argument to `library`, the LSP now points to
empty spans at the top of module files.

The library names are now determined by the file name, or the package
name at the top level.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
IGI-111 authored Mar 9, 2023
1 parent 0848a88 commit 0eaa3a6
Show file tree
Hide file tree
Showing 430 changed files with 994 additions and 1,171 deletions.
2 changes: 1 addition & 1 deletion docs/book/src/advanced/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ ABI supertraits are intended to make contract implementations compositional, all
Often, libraries and APIs have interfaces that are abstracted over a type that implements a certain trait. It is up to the consumer of the interface to implement that trait for the type they wish to use with the interface. For example, let's take a look at a trait and an interface built off of it.

```sway
library games;
library;
pub enum Suit {
Hearts: (),
Expand Down
48 changes: 32 additions & 16 deletions docs/book/src/sway-program-types/libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ Libraries in Sway are files used to define new common behavior. The most promine
Libraries are defined using the `library` keyword at the beginning of a file, followed by a name so that they can be imported.

```sway
library my_library;
library;
// library code
```

A good reference library to use when learning library design is the [Sway Standard Library](../introduction/standard_library.html). For example, the standard library offers an [implementation](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/option.sw) of `enum Option<T>` which is a generic type that represents either the existence of a value using the variant `Some(..)` or a value's absence using the variant `None`. The [Sway file implementing `Option<T>`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/option.sw) has the following structure:

- The `library` keyword followed by the name of the library:
- The `library` keyword:

```sway
library option;
library;
```

- A `use` statement that imports `revert` from another library _inside_ the standard library:
Expand Down Expand Up @@ -63,34 +63,48 @@ name = "my_library"

which denotes the authors, an entry file, the name by which it can be imported, and any dependencies.

For large libraries, it is recommended to have a `lib.sw` entry point re-export all other sub-libraries. For example, the `lib.sw` of the standard library looks like:
For large libraries, it is recommended to have a `lib.sw` entry point re-export all other sub-libraries.

The `mod` keyword registers a submodule, making its items (such as functions and structs) accessible from the parent library.
If used at the top level it will refer to a file in the `src` folder and in other cases in a folder named after the library in which it is defined.

For example, the `lib.sw` of the standard library looks like:

```sway
library std;
library;
dep block;
dep storage;
dep constants;
mod block;
mod storage;
mod constants;
mod vm;
// .. Other deps
```

with other libraries contained in the `src` folder, like the block library (inside of `block.sw`):
with other libraries contained in the `src` folder, like the vm library (inside of `src/vm.sw`):

```sway
library block;
library;
// Implementation of the `block` library
mod evm;
// ...
```

The `dep` keyword in the main library includes a dependency on another library, making all of its items (such as functions and structs) accessible from the main library. The `dep` keyword simply makes the library a dependency and fully accessible within the current context.
and it's own sub-library evm located in `src/vm/evm.sw`:

```sway
library;
// ...
```

## Using Libraries

There are two types of Sway libraries, based on their location and how they can be imported.

### Internal Libraries

Internal libraries are located within the project's `src` directory alongside `main.sw` as shown below:
Internal libraries are located within the project's `src` directory alongside
`main.sw` or in the appropriate folders as shown below:

```bash
$ tree
Expand All @@ -99,16 +113,18 @@ $ tree
├── Forc.toml
└── src
├── internal_lib.sw
└── main.sw
├── main.sw
└── internal_lib
└── nested_lib.sw
```

As `internal_lib` is an internal library, it can be imported into `main.sw` as follows:

- Use the `dep` keyword followed by the library name to make the internal library a dependancy
- Use the `mod` keyword followed by the library name to make the internal library a dependancy
- Use the `use` keyword with a `::` separating the name of the library and the imported item(s)

```sway
dep internal_lib; // Assuming the library name in `internal_lib.sw` is `internal_lib`
mod internal_lib; // Assuming the library name in `internal_lib.sw` is `internal_lib`
use internal_lib::mint;
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/src/code/examples/fizzbuzz/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library fizzbuzz;
library;

// ANCHOR: state
enum State {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library arrays;
library;

// ANCHOR: syntax
fn syntax() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library booleans;
library;

// ANCHOR: syntax
fn returns_true() -> bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library bytes;
library;

fn syntax() {
// ANCHOR: syntax
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library basic_enum;
library;

// ANCHOR: definition
enum Color {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library enum_of_enums;
library;

// ANCHOR: content
enum UserError {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library enum_of_structs;
library;

// ANCHOR: content
struct Item {
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/src/code/language/built-ins/enums/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library enums;
library;

dep basic_enum;
dep enum_of_structs;
mod basic_enum;
mod enum_of_structs;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library numerics;
library;

fn syntax() {
// ANCHOR: syntax
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library strings;
library;

fn explicit() {
// ANCHOR: explicit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library structs;
library;

// ANCHOR: definition
struct Foo {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library tuples;
library;

fn syntax() {
// ANCHOR: declare
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/src/code/language/comments/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library comments;
library;

fn comment() {
// ANCHOR: comment
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/src/code/language/control_flow/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library control_flow;
library;

fn conditional() {
// ANCHOR: conditional
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/src/code/language/functions/src/explicit.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library explicit;
library;

// ANCHOR: main
fn main() -> bool {
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/src/code/language/functions/src/implicit.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library implicit;
library;

// ANCHOR: main
fn main() -> bool {
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/src/code/language/functions/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library functions;
library;

dep explicit;
dep implicit;
mod explicit;
mod implicit;

// ANCHOR: definition
fn my_function(my_parameter: u64 /* ... */) -> u64 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library interface;
library;

abi Wallet {
/// When the BASE_ASSET is sent to this function the internal contract balance is incremented
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library my_library;
library;

use my_other_library::quix;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
library my_other_library;
library;

pub fn quix() {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library lib;
library;

dep my_library;
mod my_library;

use my_library::bar;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ANCHOR: library
// ANCHOR: module
library my_library;
library;
// ANCHOR_END: module

// Cannot import because the `pub` keyword is missing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library annotations;
library;

// ANCHOR: type_annotation
fn execute() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library enums;
library;

pub enum Error {
StateError: StateError,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library getters;
library;

// ANCHOR: avoid
fn get_maximum_deposit() -> u64 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ANCHOR: module
library letter_casing;
library;
// ANCHOR_END: module
// ANCHOR: const
const MAXIMUM_DEPOSIT = 10;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library struct_shorthand;
library;

// ANCHOR: struct_shorthand_definition
struct Structure {
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/src/code/language/variables/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library variables;
library;

fn mutable() {
// ANCHOR: mutable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library enums;
library;

// ANCHOR: u64_example
pub enum T {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library arrays;
library;

// ANCHOR: syntax
fn syntax() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library strings;
library;

fn single_quotes() {
// ANCHOR: single_quotes
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/src/code/operations/assertions/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library assertions;
library;

dep req;
mod req;

// ANCHOR: assert
fn subtract(a: u64, b: u64) -> u64 {
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/src/code/operations/assertions/src/req.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library req;
library;

// ANCHOR: require
fn subtract(a: u64, b: u64) -> u64 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library asset_operations;
library;

// ANCHOR: mint_import
use std::token::mint;
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/src/code/operations/call_data/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library call_data;
library;

// ANCHOR: import_sender
use std::auth::msg_sender;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library interface;
library;

abi Vault {
fn deposit();
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/src/code/operations/hashing/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library hashing;
library;

// ANCHOR: import_sha256
use std::hash::sha256;
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/src/code/operations/logging/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library logging;
library;

// ANCHOR: logging
fn log_data(number: u64) {
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/src/code/operations/namespace/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library namespace;
library;

// ANCHOR: address
pub struct Address {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A library is used to contain code that performs common operations in order to pr

## Definition

Libraries are defined using the `library` keyword at the beginning of a file followed by a name so that they can be identified and imported.
Libraries are defined using the `library` keyword at the beginning of a file.

```sway
{{#include ../../../../code/language/program-types/libraries/internal/my_library/src/my_library.sw:module}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $ tree

To be able to use our library `my_library.sw` in `lib.sw` there are two steps to take:

1. Bring our library into scope by using the `dep` keyword followed by the library name
1. Bring our library into scope by using the `mod` keyword followed by the library name
2. Use the `use` keyword to selectively import various items from the library

```sway
Expand Down
2 changes: 1 addition & 1 deletion examples/cei_analysis/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
contract;

dep other_contract;
mod other_contract;

use other_contract::*;

Expand Down
2 changes: 1 addition & 1 deletion examples/cei_analysis/src/other_contract.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library other_contract;
library;

abi OtherContract {
#[payable]
Expand Down
2 changes: 1 addition & 1 deletion examples/enums/src/basic_enum.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library basic_enum;
library;

// Declare the enum
enum Color {
Expand Down
Loading

0 comments on commit 0eaa3a6

Please sign in to comment.