Skip to content

Commit

Permalink
Sort forc-doc links alphabetically, add std-lib docs definitions (Fue…
Browse files Browse the repository at this point in the history
…lLabs#4465)

## Description

This PR sorts forc doc links in alphabetical order and adds some missing
definitions to the std-lib docs.

Close FuelLabs#3995 

## 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).
- [ ] 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.

---------

Co-authored-by: Sophie Dankel <[email protected]>
  • Loading branch information
sarahschwartz and sdankel authored Apr 19, 2023
1 parent 51c2eaf commit 532dc44
Show file tree
Hide file tree
Showing 29 changed files with 74 additions and 17 deletions.
25 changes: 22 additions & 3 deletions forc-plugins/forc-doc/src/render/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,28 @@ pub(crate) struct DocLinks {
}
impl Renderable for DocLinks {
fn render(self, _render_plan: RenderPlan) -> Result<Box<dyn RenderBox>> {
let mut links_vec = Vec::new();
// sort the doc links alphabetically
// for the AllDoc page, sort based on the module prefix
match self.style {
DocStyle::AllDoc(_) => {
for (block_title, mut doc_link) in self.links {
doc_link.sort_by(|a, b| {
a.module_info.module_prefixes[1].cmp(&b.module_info.module_prefixes[1])
});
links_vec.push((block_title, doc_link));
}
}
_ => {
for (block_title, mut doc_link) in self.links {
doc_link.sort();
links_vec.push((block_title, doc_link));
}
}
}
let doc_links = match self.style {
DocStyle::AllDoc(_) => box_html! {
@ for (title, list_items) in self.links {
@ for (title, list_items) in links_vec {
@ if !list_items.is_empty() {
h2(id=format!("{}", title.html_title_string())) { : title.as_str(); }
div(class="item-table") {
Expand Down Expand Up @@ -56,7 +75,7 @@ impl Renderable for DocLinks {
.into_string()
.unwrap(),
DocStyle::ProjectIndex(_) => box_html! {
@ for (title, list_items) in self.links {
@ for (title, list_items) in links_vec {
@ if !list_items.is_empty() {
h2(id=format!("{}", title.html_title_string())) { : title.as_str(); }
div(class="item-table") {
Expand Down Expand Up @@ -91,7 +110,7 @@ impl Renderable for DocLinks {
.into_string()
.unwrap(),
_ => box_html! {
@ for (title, list_items) in self.links {
@ for (title, list_items) in links_vec {
@ if !list_items.is_empty() {
h2(id=format!("{}", title.html_title_string())) { : title.as_str(); }
div(class="item-table") {
Expand Down
1 change: 1 addition & 0 deletions sway-lib-std/src/assert.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Functions to assert a given condition.
library;

use ::logging::log;
Expand Down
1 change: 1 addition & 0 deletions sway-lib-std/src/auth.sw
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ::option::Option;
use ::result::Result;
use ::inputs::{Input, input_count, input_owner, input_type};

/// The error type used when an `Identity` cannot be determined.
pub enum AuthError {
InputsNotAllOwnedBySameAddress: (),
}
Expand Down
4 changes: 2 additions & 2 deletions sway-lib-std/src/block.sw
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ pub fn height() -> u64 {
}
}

/// Get the timestamp of the current block.
/// Get the TAI64 timestamp of the current block.
pub fn timestamp() -> u64 {
timestamp_of_block(height())
}

/// Get the timestamp of a block at a given `block_height`.
/// Get the TAI64 timestamp of a block at a given `block_height`.
pub fn timestamp_of_block(block_height: u64) -> u64 {
asm(timestamp, height: block_height) {
time timestamp height;
Expand Down
1 change: 1 addition & 0 deletions sway-lib-std/src/bytes.sw
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl RawBytes {
}
}

/// A type used to represent raw bytes.
pub struct Bytes {
buf: RawBytes,
len: u64,
Expand Down
4 changes: 4 additions & 0 deletions sway-lib-std/src/constants.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//! Base asset and zero address constants.
library;

use ::contract_id::ContractId;

/// The `BASE_ASSET_ID` represents the base asset of a chain.
/// This is currently hard coded as a zero address, but will be configurable in the future.
pub const BASE_ASSET_ID = ContractId::from(ZERO_B256);
/// A B256 type zero address.
pub const ZERO_B256 = 0x0000000000000000000000000000000000000000000000000000000000000000;
2 changes: 1 addition & 1 deletion sway-lib-std/src/context.sw
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn this_balance(asset_id: ContractId) -> u64 {
balance_of(asset_id, contract_id())
}

/// Get the balance of coin `asset_id` for for the contract at 'target'.
/// Get the balance of coin `asset_id` for the contract at 'target'.
pub fn balance_of(asset_id: ContractId, target: ContractId) -> u64 {
asm(balance, token: asset_id.value, id: target.value) {
bal balance token id;
Expand Down
2 changes: 2 additions & 0 deletions sway-lib-std/src/ecr.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Helper functions to verify signatures.
library;

use ::address::Address;
Expand All @@ -6,6 +7,7 @@ use ::registers::error;
use ::hash::sha256;
use ::result::Result;

/// The error type used when the `ec_recover` function fails.
pub enum EcRecoverError {
UnrecoverablePublicKey: (),
}
Expand Down
1 change: 1 addition & 0 deletions sway-lib-std/src/experimental.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Experimental types and functions.
library;

mod r#storage;
1 change: 1 addition & 0 deletions sway-lib-std/src/experimental/storage.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Experimental storage types.
library;

use ::alloc::{alloc, realloc_bytes};
Expand Down
1 change: 1 addition & 0 deletions sway-lib-std/src/external.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Functions to work with external contracts.
library;

use ::constants::ZERO_B256;
Expand Down
1 change: 1 addition & 0 deletions sway-lib-std/src/hash.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Utility functions for cryptographic hashing.
library;

/// Returns the `SHA-2-256` hash of `param`.
Expand Down
1 change: 1 addition & 0 deletions sway-lib-std/src/identity.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ library;
use ::address::Address;
use ::contract_id::ContractId;

/// The `Identity` type: either an `Address` or a `ContractId`.
// ANCHOR: docs_identity
pub enum Identity {
Address: Address,
Expand Down
1 change: 1 addition & 0 deletions sway-lib-std/src/inputs.sw
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub const GTF_INPUT_MESSAGE_DATA = 0x11D;
pub const GTF_INPUT_MESSAGE_PREDICATE = 0x11E;
pub const GTF_INPUT_MESSAGE_PREDICATE_DATA = 0x11F;

/// The input type for a transaction.
pub enum Input {
Coin: (),
Contract: (),
Expand Down
2 changes: 2 additions & 0 deletions sway-lib-std/src/low_level_call.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Utilities to help with low level calls.
library;

use ::assert::assert;
Expand All @@ -7,6 +8,7 @@ use ::option::Option;
use ::revert::require;
use ::vec::Vec;

/// A struct representing the call parameters of a function call.
pub struct CallParams {
coins: u64,
asset_id: ContractId,
Expand Down
9 changes: 7 additions & 2 deletions sway-lib-std/src/math.sw
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Utilities for common math operations.
library;

/// Calculates the square root.
pub trait Root {
fn sqrt(self) -> Self;
}
Expand Down Expand Up @@ -44,6 +46,7 @@ impl Root for u8 {
}
}

/// Calculates a number to a given power.
pub trait Power {
fn pow(self, exponent: Self) -> Self;
}
Expand Down Expand Up @@ -84,13 +87,14 @@ impl Power for u8 {
}
}

// Trait for exponential functions
// Should exist for UFP64, UFP128 and their signed versions
/// Trait for exponential functions.
/// This should exist for UFP64, UFP128 and their signed versions.
pub trait Exponent {
// exponential function: e ^ exponent
fn exp(exponent: Self) -> Self;
}

/// Calculates the log with a given base.
pub trait Logarithm {
fn log(self, base: Self) -> Self;
}
Expand Down Expand Up @@ -131,6 +135,7 @@ impl Logarithm for u8 {
}
}

/// Calculates the binary log.
pub trait BinaryLogarithm {
fn log2(self) -> Self;
}
Expand Down
1 change: 1 addition & 0 deletions sway-lib-std/src/message.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Helper functions to sign and send messages.
library;

use ::alloc::alloc_bytes;
Expand Down
4 changes: 2 additions & 2 deletions sway-lib-std/src/option.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Optional values.
//! A type for optional values.
//!
//! Type `Option` represents an optional value: every `Option`
//! is either `Some` and contains a value, or `None`, and
Expand Down Expand Up @@ -78,7 +78,7 @@ use ::result::Result;
use ::revert::revert;

// ANCHOR: docs_option
/// `Option` is a type that represents an optional value.
/// A type that represents an optional value, either `Some(val)` or `None`.
pub enum Option<T> {
/// No value.
None: (),
Expand Down
2 changes: 2 additions & 0 deletions sway-lib-std/src/outputs.sw
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub const GTF_OUTPUT_MESSAGE_AMOUNT = 0x209;

// pub const GTF_OUTPUT_CONTRACT_CREATED_CONTRACT_ID = 0x20A;
// pub const GTF_OUTPUT_CONTRACT_CREATED_STATE_ROOT = 0x20B;

/// The output type for a transaction.
pub enum Output {
Coin: (),
Contract: (),
Expand Down
7 changes: 4 additions & 3 deletions sway-lib-std/src/revert.sw
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Functions to panic or revert with a given error code.
library;

use ::logging::log;
use ::error_signals::FAILED_REQUIRE_SIGNAL;

/// Context-dependent:
/// Will panic if used in a predicate.
/// Will revert if used in a contract.
/// Will either panic or revert with a given number depending on the context.
/// If used in a predicate, it will panic.
/// If used in a contract, it will revert.
///
/// ### Arguments
///
Expand Down
3 changes: 3 additions & 0 deletions sway-lib-std/src/storage.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Contract storage utilities.
library;

use ::alloc::{alloc, alloc_bytes, realloc_bytes};
Expand Down Expand Up @@ -239,6 +240,7 @@ pub fn clear_slice(key: b256) -> bool {
__state_clear(sha256(key), number_of_slots)
}

/// A general way to persistently store heap types.
pub trait StorableSlice<T> {
#[storage(write)]
fn store(self, argument: T);
Expand Down Expand Up @@ -1046,6 +1048,7 @@ impl<V> StorageVec<V> {
}
}

/// A persistent storage type to store a collection of tightly packed bytes.
pub struct StorageBytes {}

impl StorableSlice<Bytes> for StorageBytes {
Expand Down
7 changes: 4 additions & 3 deletions sway-lib-std/src/tx.sw
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub const GTF_CREATE_WITNESS_AT_INDEX = 0x01D;
pub const GTF_WITNESS_DATA_LENGTH = 0x301;
pub const GTF_WITNESS_DATA = 0x302;

/// A transaction type.
pub enum Transaction {
Script: (),
Create: (),
Expand Down Expand Up @@ -109,7 +110,7 @@ pub fn tx_witnesses_count() -> u64 {
}
}

// Get a pointer to the witness at index `index` for either `tx_type`
/// Get a pointer to the witness at index `index` for either `tx_type`
/// (transaction-script or transaction-create).
pub fn tx_witness_pointer(_index: u64) -> u64 {
match tx_type() {
Expand All @@ -118,12 +119,12 @@ pub fn tx_witness_pointer(_index: u64) -> u64 {
}
}

// Get the length of the witness data at `index`.
/// Get the length of the witness data at `index`.
pub fn tx_witness_data_length(index: u64) -> u64 {
__gtf::<u64>(index, GTF_WITNESS_DATA_LENGTH)
}

// Get the witness data at `index`.
/// Get the witness data at `index`.
pub fn tx_witness_data<T>(index: u64) -> T {
__gtf::<raw_ptr>(index, GTF_WITNESS_DATA).read::<T>()
}
Expand Down
2 changes: 2 additions & 0 deletions sway-lib-std/src/u128.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! A 128-bit unsigned integer type.
library;

use ::assert::assert;
Expand All @@ -13,6 +14,7 @@ pub struct U128 {
lower: u64,
}

/// The error type used for `u128` type errors.
pub enum U128Error {
LossOfPrecision: (),
}
Expand Down
2 changes: 2 additions & 0 deletions sway-lib-std/src/u256.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! A 256-bit unsigned integer type.
library;

use ::assert::assert;
Expand Down Expand Up @@ -30,6 +31,7 @@ pub struct U256 {
d: u64,
}

/// The error type used for `u256` type errors.
pub enum U256Error {
LossOfPrecision: (),
}
Expand Down
1 change: 1 addition & 0 deletions sway-lib-std/src/vec.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! A vector type for dynamically sized arrays outside of storage.
library;

use ::alloc::{alloc, realloc};
Expand Down
1 change: 1 addition & 0 deletions sway-lib-std/src/vm.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! VM-specific utilities.
library;

mod evm;
1 change: 1 addition & 0 deletions sway-lib-std/src/vm/evm.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! EVM-specific utilities.
library;

mod evm_address;
Expand Down
1 change: 1 addition & 0 deletions sway-lib-std/src/vm/evm/ecr.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Helper functions to verify EVM signatures.
library;

use ::b512::B512;
Expand Down
2 changes: 1 addition & 1 deletion sway-lsp/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ async fn go_to_definition_for_paths() {
req_uri: &uri,
req_line: 24,
req_char: 31,
def_line: 5,
def_line: 9,
def_start_char: 10,
def_end_char: 19,
def_path: "sway-lib-std/src/constants.sw",
Expand Down

0 comments on commit 532dc44

Please sign in to comment.