Skip to content

Commit

Permalink
Merge pull request #4 from thombles/ci-improvements
Browse files Browse the repository at this point in the history
Fix clippy errors and improve CI lints
  • Loading branch information
thombles authored Nov 1, 2020
2 parents f69c69d + 66b9a95 commit 5a13c34
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ jobs:
run: cargo check --all-targets
- name: Run tests
run: cargo test
- name: Clean
run: cargo clean
- name: Check formatting
run: cargo fmt -- --check
- name: Clippy
run: cargo clippy -- -Dwarnings

Expand All @@ -22,6 +26,9 @@ jobs:
run: cargo check --all-targets
- name: Run tests
run: cargo test
- name: Clean
run: cargo clean
- name: Check formatting
run: cargo fmt -- --check
- name: Clippy
run: cargo clippy -- -Dwarnings

26 changes: 13 additions & 13 deletions src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,24 +362,24 @@ impl FromStr for Address {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<&str> = s.split('-').collect();
if parts.len() != 2 {
Err(AddressParseError::InvalidFormat)?;
return Err(AddressParseError::InvalidFormat);
}

let callsign = parts[0].to_uppercase();
if callsign.is_empty() || callsign.len() > 6 {
Err(AddressParseError::InvalidFormat)?;
return Err(AddressParseError::InvalidFormat);
}
for c in callsign.chars() {
if !c.is_alphanumeric() {
Err(AddressParseError::InvalidFormat)?;
return Err(AddressParseError::InvalidFormat);
}
}

let ssid = parts[1]
.parse::<u8>()
.map_err(|e| AddressParseError::InvalidSsid { source: e })?;
if ssid > 15 {
Err(AddressParseError::SsidOutOfRange)?;
return Err(AddressParseError::SsidOutOfRange);
}

// c_bit will be set on transmit
Expand Down Expand Up @@ -446,13 +446,13 @@ impl Ax25Frame {
let control = addr_end + 1;
// +1 because the "terminator" is actually within the last byte
if addr_end - addr_start + 1 < 14 {
Err(FrameParseError::AddressFieldTooShort {
return Err(FrameParseError::AddressFieldTooShort {
start: addr_start,
end: addr_end,
})?;
});
}
if control >= bytes.len() {
Err(FrameParseError::FrameTooShort { len: bytes.len() })?;
return Err(FrameParseError::FrameTooShort { len: bytes.len() });
}

let dest = parse_address(&bytes[addr_start..addr_start + 7])?;
Expand Down Expand Up @@ -542,7 +542,7 @@ fn parse_address(bytes: &[u8]) -> Result<Address, FrameParseError> {

fn parse_i_frame(bytes: &[u8]) -> Result<FrameContent, FrameParseError> {
if bytes.len() < 2 {
Err(FrameParseError::MissingPidField)?;
return Err(FrameParseError::MissingPidField);
}
let c = bytes[0]; // control octet
Ok(FrameContent::Information(Information {
Expand Down Expand Up @@ -574,7 +574,7 @@ fn parse_s_frame(bytes: &[u8]) -> Result<FrameContent, FrameParseError> {
receive_sequence: n_r,
poll_or_final,
})),
_ => Err(FrameParseError::UnrecognisedSFieldType)?,
_ => Err(FrameParseError::UnrecognisedSFieldType),
}
}

Expand Down Expand Up @@ -604,13 +604,13 @@ fn parse_u_frame(bytes: &[u8]) -> Result<FrameContent, FrameParseError> {
})),
0b1000_0111 => parse_frmr_frame(bytes),
0b0000_0011 => parse_ui_frame(bytes),
_ => Err(FrameParseError::UnrecognisedUFieldType)?,
_ => Err(FrameParseError::UnrecognisedUFieldType),
}
}

fn parse_ui_frame(bytes: &[u8]) -> Result<FrameContent, FrameParseError> {
if bytes.len() < 2 {
Err(FrameParseError::MissingPidField)?;
return Err(FrameParseError::MissingPidField);
}
// Control, then PID, then Info
Ok(FrameContent::UnnumberedInformation(UnnumberedInformation {
Expand All @@ -623,7 +623,7 @@ fn parse_ui_frame(bytes: &[u8]) -> Result<FrameContent, FrameParseError> {
fn parse_frmr_frame(bytes: &[u8]) -> Result<FrameContent, FrameParseError> {
// Expect 24 bits following the control
if bytes.len() != 4 {
Err(FrameParseError::WrongSizeFrmrInfo)?;
return Err(FrameParseError::WrongSizeFrmrInfo);
}
Ok(FrameContent::FrameReject(FrameReject {
final_bit: bytes[0] & 0b0001_0000 > 0,
Expand All @@ -645,7 +645,7 @@ fn parse_frmr_frame(bytes: &[u8]) -> Result<FrameContent, FrameParseError> {
/// Parse the content of the frame starting from the control field
fn parse_content(bytes: &[u8]) -> Result<FrameContent, FrameParseError> {
if bytes.is_empty() {
Err(FrameParseError::ContentZeroLength)?;
return Err(FrameParseError::ContentZeroLength);
}
match bytes[0] {
c if c & 0x01 == 0x00 => parse_i_frame(bytes),
Expand Down
28 changes: 16 additions & 12 deletions src/tnc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ impl FromStr for TncAddress {

fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.starts_with("tnc:") {
Err(ParseError::NoTncPrefix {
return Err(ParseError::NoTncPrefix {
string: s.to_string(),
})?;
});
}
let components: Vec<&str> = s.split(':').collect();
let len = components.len();
Ok(match components[1] {
"tcpkiss" => {
if len != 4 {
Err(ParseError::WrongParameterCount {
return Err(ParseError::WrongParameterCount {
tnc_type: components[1].to_string(),
expected: 2usize,
actual: len - 2,
})?;
});
}
TncAddress {
config: ConnectConfig::TcpKiss(TcpKissConfig {
Expand All @@ -124,21 +124,23 @@ impl FromStr for TncAddress {
}
"linuxif" => {
if len != 3 {
Err(ParseError::WrongParameterCount {
return Err(ParseError::WrongParameterCount {
tnc_type: components[1].to_string(),
expected: 1usize,
actual: len - 2,
})?;
});
}
TncAddress {
config: ConnectConfig::LinuxIf(LinuxIfConfig {
callsign: components[2].to_string(),
}),
}
}
unknown => Err(ParseError::UnknownType {
tnc_type: unknown.to_string(),
})?,
unknown => {
return Err(ParseError::UnknownType {
tnc_type: unknown.to_string(),
})
}
})
}
}
Expand Down Expand Up @@ -200,9 +202,11 @@ impl LinuxIfTnc {
.find(|nd| nd.name.to_uppercase() == config.callsign.to_uppercase())
{
Some(nd) => nd.ifindex,
None => Err(TncError::InterfaceNotFound {
callsign: config.callsign.clone(),
})?,
None => {
return Err(TncError::InterfaceNotFound {
callsign: config.callsign.clone(),
})
}
};
Ok(Self {
socket: Arc::new(socket),
Expand Down

0 comments on commit 5a13c34

Please sign in to comment.