Skip to content

Commit

Permalink
fix(rust): Address clippy warnings (#14)
Browse files Browse the repository at this point in the history
# Motivation
The code is failing the rust linter in CI.

# Changes
- Address clippy lints.

# Tests
See CI

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
bitdivine and dependabot[bot] authored Sep 18, 2024
1 parent a1277a0 commit a46910f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 24 deletions.
38 changes: 19 additions & 19 deletions scripts/did.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
#!/usr/bin/env bash

did_file_location_from_cargo() {
# Warning: Does no _- conversion.
cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path | sub("Cargo.toml";"\($v).did")'
# Warning: Does no _- conversion.
cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path | sub("Cargo.toml";"\($v).did")'
}
cargo_manifest_path() {
# Warning: Does no _- conversion.
cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path")'
# Warning: Does no _- conversion.
cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path")'
}
did_file_location_from_dfx_json() {
# Warning: Does no _- conversion.
jq -r --arg v "$1" '.canisters[$v].candid' dfx.json
# Warning: Does no _- conversion.
jq -r --arg v "$1" '.canisters[$v].candid' dfx.json
}

function generate_did() {
local canister=$1
echo "Deriving candid file from Rust for $canister"
#local manifest_path="$(cargo_manifest_path "$canister")"
#local candid_file="${manifest_path%Cargo.toml}$canister.did"
local candid_file="$(did_file_location_from_dfx_json "$canister")"
local canister=$1
echo "Deriving candid file from Rust for $canister"
#local manifest_path="$(cargo_manifest_path "$canister")"
#local candid_file="${manifest_path%Cargo.toml}$canister.did"
local candid_file="$(did_file_location_from_dfx_json "$canister")"

test -e "target/wasm32-unknown-unknown/release/$canister.wasm" ||
cargo build -p "$canister" \
--target wasm32-unknown-unknown \
--release --package "$canister"
test -e "target/wasm32-unknown-unknown/release/$canister.wasm" ||
cargo build -p "$canister" \
--target wasm32-unknown-unknown \
--release --package "$canister"

# cargo install candid-extractor
candid-extractor "target/wasm32-unknown-unknown/release/$canister.wasm" >"$candid_file"
echo "Written: $candid_file"
# cargo install candid-extractor
candid-extractor "target/wasm32-unknown-unknown/release/$canister.wasm" >"$candid_file"
echo "Written: $candid_file"
}

CANISTERS=(example_app_backend example_paid_service)

for canister in ${CANISTERS[@]}; do
generate_did "$canister"
generate_did "$canister"
done
2 changes: 1 addition & 1 deletion scripts/pic-install
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ else
exit 1
fi

if "$POCKET_IC_SERVER_PATH" --version &>/dev/null | grep -wq "$POCKET_IC_SERVER_VERSION" ; then
if "$POCKET_IC_SERVER_PATH" --version &>/dev/null | grep -wq "$POCKET_IC_SERVER_VERSION"; then
echo "PocketIC server already exists at: $POCKET_IC_SERVER_PATH"
echo "Skipping download."
else
Expand Down
9 changes: 9 additions & 0 deletions scripts/test-rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

print_help() {
echo "Runs rust unit tests."
}

[[ "${1:-}" != "--help" ]] || print_help

cargo test --lib --bins "${@}"
1 change: 1 addition & 0 deletions src/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use caller::PaymentType;
pub use error::PaymentError;
pub use vendor::Icrc2Payer;

#[must_use]
pub fn principal2account(principal: &Principal) -> ByteBuf {
// TODO: This is NOT the right way.
let mut ans = principal.as_slice().to_vec();
Expand Down
2 changes: 1 addition & 1 deletion src/example/paid_service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn init(init_args: Option<InitArgs>) {
}

#[update()]
async fn free() -> String {
fn free() -> String {
"Yes, I am free!".to_string()
}

Expand Down
2 changes: 1 addition & 1 deletion src/example/paid_service/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ where
INIT_ARGS.with(|init_args| f(init_args.borrow().as_ref().expect("No init args provided")))
}

/// Provides the canister_id of the ledger used for payments.
/// Provides the canister id of the ledger used for payments.
pub fn payment_ledger() -> Principal {
init_element(|init_args| init_args.ledger.expect("Init args specify no ledger"))
}
Expand Down
15 changes: 13 additions & 2 deletions src/guard/src/guards/icrc2_cycles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,28 @@ pub struct Icrc2CyclesPaymentGuard {
pub own_canister_id: Principal,
}
impl Icrc2CyclesPaymentGuard {
#[must_use]
pub fn default_account() -> Account {
Account {
owner: ic_cdk::caller(),
subaccount: None,
}
}
/// The normal cycles ledger canister ID.
///
/// - If the cycles ledger is listed in `dfx.json`, a normal `dfx build` will set the
/// environment variable `CANISTER_ID_CYCLES_LEDGER` and we use this to obtain the canister ID.
/// - Otherwise, we use the mainnet cycled ledger canister ID, which is `um5iw-rqaaa-aaaaq-qaaba-cai`.
///
/// # Panics
/// - If the `CANISTER_ID_CYCLES_LEDGER` environment variable is not a valid canister ID at
/// build time.
#[must_use]
pub fn default_cycles_ledger() -> Principal {
Principal::from_text(
option_env!("DFX_CYCLES_LEDGER_CANISTER_ID").unwrap_or("um5iw-rqaaa-aaaaq-qaaba-cai"),
option_env!("CANISTER_ID_CYCLES_LEDGER").unwrap_or("um5iw-rqaaa-aaaaq-qaaba-cai"),
)
.expect("Failed to parse cycles ledger canister ID")
.expect("Compile error: Failed to parse build env var 'CANISTER_ID_CYCLES_LEDGER' as a canister ID.")
}
}

Expand Down

0 comments on commit a46910f

Please sign in to comment.