Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impr docs #1262

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open

Impr docs #1262

wants to merge 33 commits into from

Conversation

PolyProgrammist
Copy link
Collaborator

No description provided.

Copy link

codecov bot commented Nov 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.16%. Comparing base (ae0d1c4) to head (b1b185f).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1262      +/-   ##
==========================================
- Coverage   80.26%   80.16%   -0.11%     
==========================================
  Files         104      104              
  Lines       14855    14855              
==========================================
- Hits        11924    11909      -15     
- Misses       2931     2946      +15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -500,6 +500,8 @@ pub fn alt_bn128_pairing_check(value: &[u8]) -> bool {
// ################
/// Creates a promise that will execute a method on account with given arguments and attaches
/// the given amount and gas.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PolyProgrammist suggest adding more links, mentioned in #1265 and marked up in review of #1259,
in addition to ones, added in scope of current pr, after fixing conflicts, if you plan to finish current pr

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which links do you mean? @dj8yfo

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added half of the comments. Left the other for the future

Copy link
Contributor

@denbite denbite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job on this, I've dropped a few comments with some thoughts on how it could be even better
it's mainly focused on adding more examples

near-sdk-macros/src/lib.rs Outdated Show resolved Hide resolved
near-sdk-macros/src/lib.rs Outdated Show resolved Hide resolved
near-sdk-macros/src/lib.rs Outdated Show resolved Hide resolved
near-sdk-macros/src/lib.rs Outdated Show resolved Hide resolved
near-sdk-macros/src/lib.rs Show resolved Hide resolved
near-sdk-macros/src/lib.rs Show resolved Hide resolved
near-sdk-macros/src/lib.rs Show resolved Hide resolved
near-sdk/src/lib.rs Show resolved Hide resolved
@PolyProgrammist PolyProgrammist marked this pull request as ready for review December 20, 2024 17:15
@PolyProgrammist
Copy link
Collaborator Author

@dj8yfo @denbite thanks for the review. Please check again

@@ -159,7 +159,7 @@ pub fn register_len(register_id: u64) -> Option<u64> {
/// use near_sdk::AccountId;
/// use std::str::FromStr;
///
/// assert_eq!(current_account_id(), AccountId::from_str("alice.near").unwrap());
/// assert_eq!(current_account_id(), "alice.near".parse().unwrap());
Copy link
Collaborator

@dj8yfo dj8yfo Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding turbofishes helps in this and similar occasions helps

diff --git a/near-sdk/src/environment/env.rs b/near-sdk/src/environment/env.rs
index f04121cd..28bd0e75 100644
--- a/near-sdk/src/environment/env.rs
+++ b/near-sdk/src/environment/env.rs
@@ -152,21 +152,21 @@ pub fn register_len(register_id: u64) -> Option<u64> {
 // # Context API #
 // ###############
 /// The id of the account that owns the current contract.
 ///
 /// # Examples
 /// ```
 /// use near_sdk::env::current_account_id;
 /// use near_sdk::AccountId;
 /// use std::str::FromStr;
 ///
-/// assert_eq!(current_account_id(), "alice.near".parse().unwrap());
+/// assert_eq!(current_account_id(), "alice.near".parse::<AccountId>().unwrap());
 /// ```
 pub fn current_account_id() -> AccountId {
     assert_valid_account_id(method_into_register!(current_account_id))
 }
 
 /// The id of the account that either signed the original transaction or issued the initial
 /// cross-contract call.
 ///
 /// # Examples
 /// ```

Copy link
Collaborator

@akorchyn akorchyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall good job. However, I added a couple nits and one major point to fix (use the near macro in root macro)

/// # Parameter and result serialization
/// If the macro is used with Impl section, for parameter serialization, this macro will generate a struct with all of the parameters as
/// fields and derive deserialization for it. By default this will be JSON deserialized with `serde`
/// but can be overwritten by using `#[serializer(borsh)]`:
/// ```ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add rust tag

///
/// # Example
/// ## Example
///
/// ```ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rust tag

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is probably not possible as is,
doing

/// ```rust
/// use near_sdk_macros::near;
/// 
/// #[near(serializers=[borsh, json])]
/// struct MyStruct {
///    pub name: String,
/// }
/// ```

results in

---- near-sdk-macros/src/lib.rs - near (line 55) stdout ----
error[E0433]: failed to resolve: could not find `near_sdk` in the list of imported crates
 --> near-sdk-macros/src/lib.rs:59:1
  |
7 | #[near(serializers=[borsh, json])]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ could not find `near_sdk` in the list of imported crates
  |
  = note: this error originates in the attribute macro `near` (in Nightly builds, run with -Z macro-backtrace for more info)

error: cannot find attribute `borsh` in this scope
 --> near-sdk-macros/src/lib.rs:59:1
  |
7 | #[near(serializers=[borsh, json])]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in the attribute macro `near` (in Nightly builds, run with -Z macro-backtrace for more info)

error: cannot find attribute `serde` in this scope
 --> near-sdk-macros/src/lib.rs:59:1
  |
7 | #[near(serializers=[borsh, json])]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `serde` is in scope, but it is a crate, not an attribute
  = note: this error originates in the attribute macro `near` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors

this is why #1288 was suggested

Copy link
Collaborator

@dj8yfo dj8yfo Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively to #1288, a circular [dev-dependency] appears to allow this too:

diff --git a/near-sdk-macros/Cargo.toml b/near-sdk-macros/Cargo.toml
index 1f0388d4..31974b8d 100644
--- a/near-sdk-macros/Cargo.toml
+++ b/near-sdk-macros/Cargo.toml
@@ -21,19 +21,20 @@ strum = { version = "0.26", default-features = false }
 strum_macros = "0.26"
 quote = { version = "1.0", default-features = false }
 Inflector = { version = "0.11.4", default-features = false, features = [] }
 darling = { version = "0.20.3", default-features = false }
 serde = { version = "1", default-features = false, features = ["serde_derive"] }
 serde_json = "1"
 
 [dev-dependencies]
 insta = { version = "1.31.0", features = ["yaml"] }
 prettyplease = { version = "0.2.15" }
+near-sdk = { path = "../near-sdk" }
 
 
 [features]
 abi = []
 __abi-embed = ["abi"]
 __abi-generate = ["abi"]
 
 [package.metadata.docs.rs]
 features = ["__abi-generate"]

not sure if this entails additional problems, but this looks a one-liner compared to moving larger blocks of code.
And near-sdk kind of re-exports all of its dependencies it uses for derive macros (does it?)
Please leave a comment on #1288, if you decide to try a circular [dev-dependency] instead

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I agree that it's better to document on near sdk level. I doubt that a lot of people will go in the macro crate at all...

Copy link
Collaborator

@dj8yfo dj8yfo Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the doc will be re-exported from near-sdk-macros anyway, as it is now

image

Another example/explanation: doc at https://docs.rs/borsh/1.5.4/borsh/derive.BorshSerialize.html gets concatenated of 2 pieces: one at doc-comment at site of re-export, one at original doc-comment site:
image

the only subtlety i see , is that if one does

[dev-dependencies]
near-sdk = { path = "../near-sdk" , features = "__abi-generate" }

then near-sdk/__abi-generate => near-sdk-macros/__abi-generate feature becomes implicitly enabled
when anyone runs cargo test for near-sdk-macros crate (which wouldn't be otherwise enabled) , but i think it's kind of ok here.
so it looks like trying a one-liner is simpler, than moving a few blocks of code.
Anyway, @akorchyn should make a final call for the pr here, as he owns the review here

(i'm pro #1288 approach , doc at reexport in near-sdk should describe how to use the macros,
doc in near-sdk-macros of various non-public items might explain how the macros work internally, if someone wants to understand/extend them, the latter is only possible when documenting private items is enabled for proc-macro crates)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the #1288 approach more, to be honest.

/// function execution based on what type is returned by the function. By default, this will be
/// done through `serde` serialized as JSON, but this can be overwritten using
/// `#[result_serializer(borsh)]`:
/// ```ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rust

/// # Example
/// If you want the struct to be a contract state, you can pass in the contract_state argument.
///
/// ## Example
/// ```ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

//! use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
//! use near_sdk::{env, near_bindgen};
//!
//! #[near_bindgen]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we use near bindgen here?
It looks a bit out of scope. As we stated before it is deprecated. (even though, deprecated is the wrong word and more like low-level). We should definitely use here a new near syntax given the fact that it is the primary point for the doc.

@@ -70,6 +70,8 @@
//! - [`LazyOption<T>`](LazyOption): Lazily loaded, optional type that can be used in
//! place of a type [`Option<T>`](Option). Will only be loaded when interacted with and will
//! persist on [`Drop`].
//!
//! More information about collections can be found in [NEAR documentation](https://docs.near.org/build/smart-contracts/anatomy/collections)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was somewhere performance estimation that would be nice to link

@@ -8,7 +8,9 @@ mod primitives;
pub use self::primitives::*;

pub use near_account_id::{AccountId, AccountIdRef};
/// Same as `NearGas` from crate [`near_gas`](near_gas)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alias to near_gas::NearGas. I believe, it should derive the docs from original type, so it is not really necessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: NEW❗
Development

Successfully merging this pull request may close these issues.

4 participants