Standardized module structure (#4232)

## Description

Implement RFC 0006, fix #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.
This commit is contained in:
IGI-111 2023-03-09 09:14:52 +01:00 committed by GitHub
parent 0848a887b7
commit 0eaa3a6da9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
430 changed files with 994 additions and 1171 deletions

View file

@ -1,4 +1,4 @@
library fizzbuzz;
library;
// ANCHOR: state
enum State {

View file

@ -1,4 +1,4 @@
library arrays;
library;
// ANCHOR: syntax
fn syntax() {

View file

@ -1,4 +1,4 @@
library booleans;
library;
// ANCHOR: syntax
fn returns_true() -> bool {

View file

@ -1,4 +1,4 @@
library bytes;
library;
fn syntax() {
// ANCHOR: syntax

View file

@ -1,4 +1,4 @@
library basic_enum;
library;
// ANCHOR: definition
enum Color {

View file

@ -1,4 +1,4 @@
library enum_of_enums;
library;
// ANCHOR: content
enum UserError {

View file

@ -1,4 +1,4 @@
library enum_of_structs;
library;
// ANCHOR: content
struct Item {

View file

@ -1,4 +1,4 @@
library enums;
library;
dep basic_enum;
dep enum_of_structs;
mod basic_enum;
mod enum_of_structs;

View file

@ -1,4 +1,4 @@
library numerics;
library;
fn syntax() {
// ANCHOR: syntax

View file

@ -1,4 +1,4 @@
library strings;
library;
fn explicit() {
// ANCHOR: explicit

View file

@ -1,4 +1,4 @@
library structs;
library;
// ANCHOR: definition
struct Foo {

View file

@ -1,4 +1,4 @@
library tuples;
library;
fn syntax() {
// ANCHOR: declare

View file

@ -1,4 +1,4 @@
library comments;
library;
fn comment() {
// ANCHOR: comment

View file

@ -1,4 +1,4 @@
library control_flow;
library;
fn conditional() {
// ANCHOR: conditional

View file

@ -1,4 +1,4 @@
library explicit;
library;
// ANCHOR: main
fn main() -> bool {

View file

@ -1,4 +1,4 @@
library implicit;
library;
// ANCHOR: main
fn main() -> bool {

View file

@ -1,7 +1,7 @@
library functions;
library;
dep explicit;
dep implicit;
mod explicit;
mod implicit;
// ANCHOR: definition
fn my_function(my_parameter: u64 /* ... */) -> u64 {

View file

@ -1,4 +1,4 @@
library interface;
library;
abi Wallet {
/// When the BASE_ASSET is sent to this function the internal contract balance is incremented

View file

@ -1,4 +1,4 @@
library my_library;
library;
use my_other_library::quix;

View file

@ -1,3 +1,3 @@
library my_other_library;
library;
pub fn quix() {}

View file

@ -1,6 +1,6 @@
library lib;
library;
dep my_library;
mod my_library;
use my_library::bar;

View file

@ -1,6 +1,6 @@
// ANCHOR: library
// ANCHOR: module
library my_library;
library;
// ANCHOR_END: module
// Cannot import because the `pub` keyword is missing

View file

@ -1,4 +1,4 @@
library annotations;
library;
// ANCHOR: type_annotation
fn execute() {

View file

@ -1,4 +1,4 @@
library enums;
library;
pub enum Error {
StateError: StateError,

View file

@ -1,4 +1,4 @@
library getters;
library;
// ANCHOR: avoid
fn get_maximum_deposit() -> u64 {

View file

@ -1,5 +1,5 @@
// ANCHOR: module
library letter_casing;
library;
// ANCHOR_END: module
// ANCHOR: const
const MAXIMUM_DEPOSIT = 10;

View file

@ -1,4 +1,4 @@
library struct_shorthand;
library;
// ANCHOR: struct_shorthand_definition
struct Structure {

View file

@ -1,4 +1,4 @@
library variables;
library;
fn mutable() {
// ANCHOR: mutable

View file

@ -1,4 +1,4 @@
library enums;
library;
// ANCHOR: u64_example
pub enum T {

View file

@ -1,4 +1,4 @@
library arrays;
library;
// ANCHOR: syntax
fn syntax() {

View file

@ -1,4 +1,4 @@
library strings;
library;
fn single_quotes() {
// ANCHOR: single_quotes

View file

@ -1,6 +1,6 @@
library assertions;
library;
dep req;
mod req;
// ANCHOR: assert
fn subtract(a: u64, b: u64) -> u64 {

View file

@ -1,4 +1,4 @@
library req;
library;
// ANCHOR: require
fn subtract(a: u64, b: u64) -> u64 {

View file

@ -1,4 +1,4 @@
library asset_operations;
library;
// ANCHOR: mint_import
use std::token::mint;

View file

@ -1,4 +1,4 @@
library call_data;
library;
// ANCHOR: import_sender
use std::auth::msg_sender;

View file

@ -1,4 +1,4 @@
library interface;
library;
abi Vault {
fn deposit();

View file

@ -1,4 +1,4 @@
library hashing;
library;
// ANCHOR: import_sha256
use std::hash::sha256;

View file

@ -1,4 +1,4 @@
library logging;
library;
// ANCHOR: logging
fn log_data(number: u64) {

View file

@ -1,4 +1,4 @@
library namespace;
library;
// ANCHOR: address
pub struct Address {

View file

@ -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}}

View file

@ -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