Skip to content

Commit

Permalink
review: addresses #1178 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Jun 6, 2024
1 parent 3b56d6e commit a34e7ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 36 deletions.
57 changes: 22 additions & 35 deletions near-sdk-macros/src/core_impl/contract_metadata/build_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,47 @@ pub(super) enum FieldError {
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!"
)
}
}
}
}
const ERR_EMPTY_BUILD_ENVIRONMENT: &str = "`CARGO_NEAR_BUILD_ENVIRONMENT` is set, \
but it's set to empty string!";
const ERR_EMPTY_BUILD_COMMAND: &str = "`CARGO_NEAR_BUILD_COMMAND` is required, \
when `CARGO_NEAR_BUILD_ENVIRONMENT` is set, \
but it's either not set or empty!";

const ERR_UNSET_CONTRACT_PATH: &str = "`CARGO_NEAR_CONTRACT_PATH` was provided, \
but it's not set!";

const ERR_UNSET_OR_EMPTY_SOURCE_SNAPSHOT: &str = "`CARGO_NEAR_SOURCE_CODE_GIT_URL` is \
required, when `CARGO_NEAR_BUILD_ENVIRONMENT` \
is set, but it's either not set or empty!";

impl BuildInfo {
pub(super) fn from_env() -> Result<Self, FieldError> {
pub(super) fn from_env() -> Result<Self, String> {
let build_environment = std::env::var("CARGO_NEAR_BUILD_ENVIRONMENT")
.map_err(|_| FieldError::EmptyBuildEnvironment)?;
.map_err(|_| ERR_EMPTY_BUILD_ENVIRONMENT.to_string())?;
if build_environment.is_empty() {
return Err(FieldError::EmptyBuildEnvironment);
return Err(ERR_EMPTY_BUILD_ENVIRONMENT.to_string());
}

let build_command = {
let build_command = std::env::var("CARGO_NEAR_BUILD_COMMAND")
.map_err(|_| FieldError::UnsetOrEmptyBuildCommand)?;
.map_err(|_| ERR_EMPTY_BUILD_COMMAND.to_string())?;
if build_command.is_empty() {
return Err(FieldError::UnsetOrEmptyBuildCommand);
return Err(ERR_EMPTY_BUILD_COMMAND.to_string());
}
let build_command =
build_command.split_whitespace().map(|st| st.to_string()).collect::<Vec<_>>();
if build_command.is_empty() {
return Err(FieldError::UnsetOrEmptyBuildCommand);
return Err(ERR_EMPTY_BUILD_COMMAND.to_string());
}
build_command
};
let source_code_snapshot = std::env::var("CARGO_NEAR_SOURCE_CODE_SNAPSHOT")
.map_err(|_| FieldError::UnsetOrEmptySourceSnapshot)?;
.map_err(|_| ERR_UNSET_OR_EMPTY_SOURCE_SNAPSHOT.to_string())?;
if source_code_snapshot.is_empty() {
return Err(FieldError::UnsetOrEmptySourceSnapshot);
return Err(ERR_UNSET_OR_EMPTY_SOURCE_SNAPSHOT.to_string());
}
let contract_path =
std::env::var("CARGO_NEAR_CONTRACT_PATH").map_err(|_| FieldError::UnsetContractPath)?;
let contract_path = std::env::var("CARGO_NEAR_CONTRACT_PATH")
.map_err(|_| ERR_UNSET_CONTRACT_PATH.to_string())?;

Ok(Self { build_environment, build_command, contract_path, source_code_snapshot })
}
Expand Down
1 change: 0 additions & 1 deletion near-sdk-macros/src/core_impl/contract_metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ impl ContractMetadata {
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"),
);
}
Expand Down

0 comments on commit a34e7ef

Please sign in to comment.