Skip to content

Commit

Permalink
chore(ci,linter): lint some code and add clippy to ci pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Jun 7, 2023
1 parent 2c834c1 commit 818fed9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 49 deletions.
13 changes: 6 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ jobs:
override: true
default: true

# Uncomment once linting issues have been fixed
# Enable this for clippy linting.
# - name: Check and Lint Code
# run: cargo clippy -- -D warnings
- name: Run cargo fmt
run: cargo fmt --check

- name: Check and Lint Code
run: cargo clippy
#run: cargo clippy -- -D warnings

- name: Run cargo check
run: cargo check --locked
Expand All @@ -44,6 +46,3 @@ jobs:

- name: Run cargo test
run: cargo test

- name: Run cargo fmt
run: cargo fmt --check
2 changes: 1 addition & 1 deletion tezos-core/src/types/encoded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub trait Encoded: Sized {
/// Returns an error of the provided value is not a valid base58 string for the type being created.
fn new(value: String) -> Result<Self>;
/// Returns the base58 string value.
fn into_string(&self) -> String {
fn into_string(self) -> String {
self.value().into()
}
/// Encodes the value to its bytes representation
Expand Down
6 changes: 5 additions & 1 deletion tezos-core/src/types/hex_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ impl HexString {
Err(Error::InvalidHexString)
}

pub fn len(&self) -> usize {
pub fn len(self) -> usize {
if self.0.starts_with(Self::PREFIX) {
return self.0.len() - Self::PREFIX.len();
}
self.0.len()
}

pub fn is_empty(self) -> bool {
self.len() == 0
}

pub fn len_with_prefix(&self) -> usize {
if self.0.starts_with(Self::PREFIX) {
return self.0.len();
Expand Down
81 changes: 41 additions & 40 deletions tezos-core/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
/// see https://tezos.gitlab.io/alpha/michelson.html#constants (needs clarification)
pub fn is_str(value: &str) -> bool {
let mut chars = value.chars();
!chars.any(|c| match c {
' '..='~' => false,
_ => true,
})
!chars.any(|c| !matches!(c, ' '..='~'))
}

/// Validates that `value` matches equivalent regex `^(0x)?([0-9a-fA-F]{2})*$`.
Expand All @@ -19,29 +16,15 @@ pub fn is_hex_str(value: &str) -> bool {
return false;
}
let mut chars = value.chars().peekable();
match chars.peek() {
Some('0') => {

if let Some('0') = chars.peek() {
chars.next();
if let Some('x') = chars.peek() {
chars.next();
match chars.peek() {
// once seen a '0', it must be followed by 'x' to build '0x' prefix
Some('x') => {
chars.next();
}
_ => {
// not advancing cursor -> gets validated below
}
}
}
_ => {
// not advancing cursor -> gets validated below or is an empty string (valid)
}
}
!chars.any(|c| match c {
'0'..='9' => false,
'A'..='F' => false,
'a'..='f' => false,
_ => true,
})

!chars.any(|c| !matches!(c, '0'..='9' | 'A'..='F' | 'a'..='f'))
}

/// Validates that `value` matches equivalent regex `^-?[0-9]+$`.
Expand All @@ -59,26 +42,44 @@ pub fn is_int(value: &str) -> bool {
// not advancing cursor -> gets validated below
}
}
!chars.any(|c| match c {
'0'..='9' => false,
_ => true,
})
chars.all(|c| c.is_ascii_digit())
}

/// Validates that `value` matches equivalent regex `^[0-9]+$`.
pub fn is_uint(value: &str) -> bool {
let mut chars = value.chars().peekable();
match chars.peek() {
None => {
// empty string is invalid!
return false;
}
_ => {
// not advancing cursor -> gets validated below
}
if chars.peek().is_none() {
// empty string is invalid!
return false;
}
chars.all(|c| c.is_ascii_digit())
}

#[cfg(test)]
mod test {
use crate::validation::{is_int, is_str, is_uint};

#[test]
fn test_is_7bit_ascii() {
assert!(is_str("123456789"));
assert!(is_str("ABCabc!"));
assert!(is_str("-12"));
assert!(!is_str("\n"));
}

#[test]
fn test_is_int() {
assert!(is_int("123456789"));
assert!(is_int("0"));
assert!(is_int("-12"));
assert!(!is_int("A"));
}

#[test]
fn test_is_uint() {
assert!(is_uint("123456789"));
assert!(is_uint("0"));
assert!(!is_uint("-12"));
assert!(!is_uint("A"));
}
!chars.any(|c| match c {
'0'..='9' => false,
_ => true,
})
}

0 comments on commit 818fed9

Please sign in to comment.