Skip to content

Commit

Permalink
chore: starknet API add lints (#1853)
Browse files Browse the repository at this point in the history
Lior banned `as` repo-wide, unless absolutely necessary.

Nearly all as uses can be replaced with [Try]From which doesn't
have implicit coercions like as (we encountered several bugs due to these coercions).

Motivation: we are standardizing lints across the repo and CI,
instead of each crate having separate sets of lints.

Notes: changes to docs are due to cargo doc now running on changed files

Co-Authored-By: Gilad Chase <[email protected]>
  • Loading branch information
giladchase and Gilad Chase authored Nov 8, 2024
1 parent 30a968f commit eba18d3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
3 changes: 3 additions & 0 deletions crates/starknet_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ rstest.workspace = true

[package.metadata.cargo-machete]
ignored = ["strum"]

[lints]
workspace = true
2 changes: 1 addition & 1 deletion crates/starknet_api/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl Nonce {
}
}

/// The selector of an [EntryPoint](`crate::deprecated_contract_class::EntryPoint`).
/// The selector of an [EntryPoint](`crate::state::EntryPoint`).
#[derive(
Debug, Copy, Clone, Default, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord,
)]
Expand Down
10 changes: 5 additions & 5 deletions crates/starknet_api/src/deprecated_contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub struct TypedParameter {
pub r#type: String,
}

/// The offset of an [EntryPoint](`crate::deprecated_contract_class::EntryPoint`).
/// The offset of an [EntryPoint](`crate::state::EntryPoint`).
#[derive(
Debug, Copy, Clone, Default, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord,
)]
Expand All @@ -212,10 +212,10 @@ impl TryFrom<String> for EntryPointOffset {

pub fn number_or_string<'de, D: Deserializer<'de>>(deserializer: D) -> Result<usize, D::Error> {
let usize_value = match Value::deserialize(deserializer)? {
Value::Number(number) => {
number.as_u64().ok_or(DeserializationError::custom("Cannot cast number to usize."))?
as usize
}
Value::Number(number) => number
.as_u64()
.and_then(|num_u64| usize::try_from(num_u64).ok())
.ok_or(DeserializationError::custom("Cannot cast number to usize."))?,
Value::String(s) => hex_string_try_into_usize(&s).map_err(DeserializationError::custom)?,
_ => return Err(DeserializationError::custom("Cannot cast value into usize.")),
};
Expand Down

0 comments on commit eba18d3

Please sign in to comment.