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

feat: nep330 build info field of contract metadata #1178

Merged
merged 29 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
79eaf7c
feat: add `CARGO_NEAR_...` vars into `ContractMetadata::populate`
May 1, 2024
2b74dd7
chore: extract `BuildInfo` as `ContractMetadata` field
May 2, 2024
be7bbbd
chore: better `std::fmt::Display` of errors of missing fields
May 3, 2024
d0820ca
chore: replace two `BuildInfo` fields with one from `CARGO_NEAR_SOURC…
May 3, 2024
71fdbb7
chore: add `CARGO_NEAR_REPO_LINK_HINT` as more precise alt to `CARGO_…
May 3, 2024
22109b1
ci: fix new 1.78 lints
May 7, 2024
64efdcf
test: `schemars` version update, `TRYBUILD=overwrite`
May 7, 2024
afe79e8
Merge branch 'ci_1_78'
May 8, 2024
bf81221
ci: fix pr specific clippy lints
May 8, 2024
b3bd35c
chore: replace `Option<Strin>` -> `String` of `BuildInfo.contract_path`
May 8, 2024
148133c
Merge branch 'master'
Jun 6, 2024
d4a44dd
chore: bump MSRV to 1.73.0
Jun 6, 2024
cd6c5fc
chore: bump MSRV to 1.73.0
Jun 6, 2024
cb1d28b
chore: bump MSRV to 1.73.0
Jun 6, 2024
97df1a1
Merge branch 'bump_msrv_1_73'
Jun 6, 2024
b2823cb
review: address https://github.com/near/near-sdk-rs/pull/1178#discuss…
Jun 6, 2024
3b56d6e
review: address https://github.com/near/near-sdk-rs/pull/1178#discuss…
Jun 6, 2024
9ff44ca
review: addresses https://github.com/near/near-sdk-rs/pull/1178#discu…
Jun 6, 2024
20f8c7e
Merge branch 'bump_msrv_1_73'
Jun 6, 2024
5156a4d
Merge branch 'bump_msrv_1_73'
Jun 6, 2024
b2755ea
Merge branch 'master'
Jun 10, 2024
ce5c116
review: addresses https://github.com/near/near-sdk-rs/pull/1178#discu…
Jun 10, 2024
223917b
review: addresses https://github.com/near/near-sdk-rs/pull/1178#discu…
Jun 10, 2024
824eb80
Merge branch 'master'
Jun 12, 2024
74e4848
review: addresses https://github.com/near/near-sdk-rs/pull/1178#discu…
Jun 12, 2024
8d0c65f
review: addresses https://github.com/near/near-sdk-rs/pull/1178#discu…
Jun 13, 2024
fb84219
review: addresses https://github.com/near/near-sdk-rs/pull/1178#discu…
Jun 13, 2024
75c8a2a
Merge branch 'master'
Jun 19, 2024
39baf19
clippy: silence `manual_unwrap_or_default` lint detection bug
Jun 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/fungible-token/test-contract-defi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct DeFi {
}

// Have to repeat the same trait for our own implementation.
#[allow(dead_code)]
trait ValueReturnTrait {
fn value_please(&self, amount_to_return: String) -> PromiseOrValue<U128>;
}
Expand Down
84 changes: 84 additions & 0 deletions near-sdk-macros/src/core_impl/contract_metadata/build_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#[derive(serde::Serialize)]
pub(crate) struct BuildInfo {
build_environment: String,
build_command: Vec<String>,
contract_path: String,
source_code_snapshot: String,
}

/// basic parsing errors
#[derive(Debug)]
pub(super) enum FieldError {
EmptyBuildEnvironment,
UnsetOrEmptyBuildCommand,
/// `None` value should be used instead for `contract_path`
UnsetContractPath,
UnsetOrEmptySourceSnapshot,
}

#[allow(clippy::write_literal)]
impl std::fmt::Display for FieldError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::EmptyBuildEnvironment => {
write!(f, "`CARGO_NEAR_BUILD_ENVIRONMENT` is set, but it's set to empty string!")
}
Self::UnsetOrEmptyBuildCommand => {
write!(f, "{}{}",
"`CARGO_NEAR_BUILD_COMMAND` is required, when `CARGO_NEAR_BUILD_ENVIRONMENT` is set, ",
"but it's either not set or empty!"
)
}
Self::UnsetContractPath => {
write!(f, "{}{}", "`CARGO_NEAR_CONTRACT_PATH` was provided, ", "but it's not set!")
}
Self::UnsetOrEmptySourceSnapshot => {
write!(f, "{}{}",
"`CARGO_NEAR_SOURCE_CODE_GIT_URL` is required, when `CARGO_NEAR_BUILD_ENVIRONMENT` is set, ",
"but it's either not set or empty!"
)
}
}
}
}
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved

impl BuildInfo {
pub(super) fn from_env() -> Result<Self, FieldError> {
macro_rules! env_field {
($field: ident, $env_key: expr, $error: expr) => {
let $field = std::env::var($env_key).map_err(|_| $error)?;
if $field.is_empty() {
return Err($error);
}
};
}

env_field!(
build_environment,
"CARGO_NEAR_BUILD_ENVIRONMENT",
FieldError::EmptyBuildEnvironment
);
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
let build_command = {
env_field!(
build_command,
"CARGO_NEAR_BUILD_COMMAND",
FieldError::UnsetOrEmptyBuildCommand
);
let build_command =
build_command.split_whitespace().map(|st| st.to_string()).collect::<Vec<_>>();
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
if build_command.is_empty() {
return Err(FieldError::UnsetOrEmptyBuildCommand);
}
build_command
};
env_field!(
source_code_snapshot,
"CARGO_NEAR_SOURCE_CODE_SNAPSHOT",
FieldError::UnsetOrEmptySourceSnapshot
);
let contract_path =
std::env::var("CARGO_NEAR_CONTRACT_PATH").map_err(|_| FieldError::UnsetContractPath)?;

Ok(Self { build_environment, build_command, contract_path, source_code_snapshot })
}
}
41 changes: 29 additions & 12 deletions near-sdk-macros/src/core_impl/contract_metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use darling::{ast::NestedMeta, Error, FromMeta};
use proc_macro2::TokenStream;
use quote::quote;

mod build_info;

#[derive(FromMeta)]
struct MacroConfig {
contract_metadata: Option<ContractMetadata>,
Expand All @@ -11,8 +13,12 @@ struct MacroConfig {
pub(crate) struct ContractMetadata {
version: Option<String>,
link: Option<String>,

#[darling(multiple, rename = "standard")]
standards: Vec<Standard>,

#[darling(skip)]
build_info: Option<build_info::BuildInfo>,
}

impl quote::ToTokens for ContractMetadata {
Expand Down Expand Up @@ -47,26 +53,37 @@ struct Standard {

impl ContractMetadata {
fn populate(mut self) -> Self {
if self.version.is_none() {
let version = std::env::var("CARGO_PKG_VERSION").unwrap_or(String::from(""));
if !version.is_empty() {
self.version = Some(version);
}
macro_rules! env_field {
($field: expr, $env_result: expr) => {
if $field.is_none() {
let field_val = $env_result.unwrap_or(String::from(""));
if !field_val.is_empty() {
$field = Some(field_val);
}
}
};
}

if self.link.is_none() {
let repo = std::env::var("CARGO_PKG_REPOSITORY").unwrap_or(String::from(""));
if !repo.is_empty() {
self.link = Some(repo);
}
}
env_field!(
self.link,
std::env::var("CARGO_NEAR_REPO_LINK_HINT").or(std::env::var("CARGO_PKG_REPOSITORY"))
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
);
env_field!(self.version, std::env::var("CARGO_PKG_VERSION"));
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved

// adding nep330 if it is not present
if self.standards.is_empty()
|| self.standards.iter().all(|s| s.standard.to_ascii_lowercase() != "nep330")
{
self.standards
.push(Standard { standard: "nep330".to_string(), version: "1.1.0".to_string() });
.push(Standard { standard: "nep330".to_string(), version: "1.2.0".to_string() });
}

if std::env::var("CARGO_NEAR_BUILD_ENVIRONMENT").is_ok() {
self.build_info = Some(
build_info::BuildInfo::from_env()
.map_err(|err| err.to_string())
.expect("Build Details Extension field not provided"),
);
}

self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl AttrSigInfo {

self_occurrences.extend(args.iter().flat_map(|arg| arg.self_occurrences.clone()));

*original_attrs = non_bindgen_attrs.clone();
original_attrs.clone_from(&non_bindgen_attrs);

if matches!(method_kind, MethodKind::Call(_) | MethodKind::View(_)) {
report_spans(
Expand Down
2 changes: 1 addition & 1 deletion near-sdk/compilation_tests/schema_derive_invalids.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ error[E0277]: the trait bound `Inner: JsonSchema` is not satisfied
i128
and $N others
note: required by a bound in `SchemaGenerator::subschema_for`
--> $CARGO/schemars-0.8.17/src/gen.rs
--> $CARGO/schemars-0.8.19/src/gen.rs
|
| pub fn subschema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema {
| ^^^^^^^^^^ required by this bound in `SchemaGenerator::subschema_for`
1 change: 1 addition & 0 deletions near-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![allow(clippy::redundant_closure)]
// We want to enable all clippy lints, but some of them generate false positives.
#![allow(clippy::missing_const_for_fn, clippy::redundant_pub_crate)]
#![allow(clippy::multiple_bound_locations)]

#[cfg(test)]
extern crate quickcheck;
Expand Down
Loading