diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml new file mode 100644 index 0000000..05b7760 --- /dev/null +++ b/.github/workflows/format.yml @@ -0,0 +1,31 @@ +name: Format + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +env: + CARGO_TERM_COLOR: always + +jobs: + format: + permissions: + # Give the default GITHUB_TOKEN write permission to commit and push the + # added or changed files to the repository. + contents: write + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly + with: + components: rustfmt + - name: Run cargo fmt + run: cargo fmt + # Commit changes + - uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: format \ No newline at end of file diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index dbea474..6313fa1 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -16,19 +16,11 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Install nightly rust - run: rustup toolchain install nightly - - name: Make nightly default - run: rustup default nightly - - name: Install build target - run: rustup target add thumbv7em-none-eabi - - name: Print installed toolchains - run: rustup show + - uses: dtolnay/rust-toolchain@nightly + with: + targets: aarch64-apple-darwin, thumbv7em-none-eabi + components: rust-src - name: Build run: cargo build --release --target thumbv7em-none-eabi - - name: Install test target - run: rustup target add aarch64-apple-darwin - - name: Add rust src - run: rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu - name: Run tests run: cargo test -Zbuild-std --target x86_64-unknown-linux-gnu diff --git a/shared/src/communication/eps.rs b/shared/src/communication/eps.rs index e834907..5a008ab 100644 --- a/shared/src/communication/eps.rs +++ b/shared/src/communication/eps.rs @@ -15,33 +15,31 @@ impl EpsCommand { pub fn from_bytes(bytes: &[u8]) -> Result { let mut words = bytes.split(|b| b == b";".iter().next().unwrap()); match words.next() { - Some(word) => { - match word { - b"pwe" => { - let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?; - let rail_num = core::str::from_utf8(arg)?.parse::()?; - Ok(EpsCommand::EnablePowerRail(rail_num)) - }, - b"pwd" => { - let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?; - let rail_num = core::str::from_utf8(arg)?.parse::()?; - Ok(EpsCommand::DisablePowerRail(rail_num)) - }, - b"soh" => return Ok(Self::StateOfHealthReq), - b"gbv" => { - let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?; - let rail_num = core::str::from_utf8(arg)?.parse::()?; - Ok(EpsCommand::GetBatteryVoltage(rail_num)) - }, - b"gprs" => { - let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?; - let rail_num = core::str::from_utf8(arg)?.parse::()?; - Ok(EpsCommand::GetPowerRailState(rail_num)) - }, - _ => Err(CommandParseError::UnknownCommand) + Some(word) => match word { + b"pwe" => { + let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?; + let rail_num = core::str::from_utf8(arg)?.parse::()?; + Ok(EpsCommand::EnablePowerRail(rail_num)) } + b"pwd" => { + let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?; + let rail_num = core::str::from_utf8(arg)?.parse::()?; + Ok(EpsCommand::DisablePowerRail(rail_num)) + } + b"soh" => return Ok(Self::StateOfHealthReq), + b"gbv" => { + let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?; + let rail_num = core::str::from_utf8(arg)?.parse::()?; + Ok(EpsCommand::GetBatteryVoltage(rail_num)) + } + b"gprs" => { + let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?; + let rail_num = core::str::from_utf8(arg)?.parse::()?; + Ok(EpsCommand::GetPowerRailState(rail_num)) + } + _ => Err(CommandParseError::UnknownCommand), }, - None => Err(CommandParseError::EmptyMessage) + None => Err(CommandParseError::EmptyMessage), } } } @@ -57,7 +55,7 @@ pub enum CommandParseError { #[error("ParseIntError {0}")] ParseIntError(#[from] ParseIntError), #[error("Utf8Error {0}")] - Utf8Error(#[from] Utf8Error) + Utf8Error(#[from] Utf8Error), } impl CommandParseError { @@ -68,7 +66,7 @@ impl CommandParseError { UnknownCommand => b"err;501", IncompleteArgs => b"err;502", ParseIntError(_) => b"err;503", - Utf8Error(_) => b"err;504" + Utf8Error(_) => b"err;504", } } } @@ -104,5 +102,4 @@ mod tests { let error = core::str::from_utf8(&[0xC0]).err().unwrap(); assert_eq!(b"err;504", Utf8Error(error).as_bytes()); } - -} \ No newline at end of file +} diff --git a/shared/src/communication/mod.rs b/shared/src/communication/mod.rs index 0226f3b..133b974 100644 --- a/shared/src/communication/mod.rs +++ b/shared/src/communication/mod.rs @@ -1 +1 @@ -pub mod eps; \ No newline at end of file +pub mod eps; diff --git a/shared/src/lib.rs b/shared/src/lib.rs index a8ec4a3..5f0bc50 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -1,3 +1,3 @@ #![no_std] -pub mod communication; \ No newline at end of file +pub mod communication;