Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CI #2

Merged
merged 2 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Check and Lint
on:
pull_request:
push:
branches:
- main

jobs:
check:
name: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo run -p ci -- check

wasm-check:
name: wasm-check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
targets: wasm32-unknown-unknown
- run: cargo run -p ci -- wasm-check

example-check:
name: example-check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo run -p ci -- example-check

fmt:
name: fmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo run -p ci -- fmt

test:
name: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo run -p ci -- test

doc-test:
name: doc-test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo run -p ci -- doc-test

doc-check:
name: doc-check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo run -p ci -- doc-check

clippy:
name: clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo run -p ci -- clippy
38 changes: 37 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ edition = "2021"
[dependencies]
thiserror = "1.0.50"
wasm-timer = "0.2.5"

[workspace]
resolver = "2"
members = [
"ci"
]
9 changes: 9 additions & 0 deletions ci/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "ci"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.70"
xshell = "0.2"
bitflags = "2.0"
119 changes: 119 additions & 0 deletions ci/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use anyhow::bail;
use bitflags::bitflags;
use xshell::{cmd, Shell};

mod utils;
use utils::*;

bitflags! {
#[derive(Clone, Copy)]
struct Check: u32 {
const CHECK = 0b00000001;
const WASM_CHECK = 0b00000010;
const EXAMPLE_CHECK = 0b00000100;
const FMT = 0b00001000;
const TEST = 0b00010000;
const DOC_TEST = 0b00100000;
const DOC_CHECK = 0b01000000;
const CLIPPY = 0b10000000;
}
}

fn main() -> anyhow::Result<()> {
std::env::set_var("RUSTFLAGS", "-D warnings");

let arguments = [
("check", Check::CHECK),
("wasm-check", Check::WASM_CHECK),
("example-check", Check::EXAMPLE_CHECK),
("fmt", Check::FMT),
("test", Check::TEST),
("doc-test", Check::DOC_TEST),
("doc-check", Check::DOC_CHECK),
("clippy", Check::CLIPPY),
];

let what_to_run = if let Some(arg) = std::env::args().nth(1).as_deref() {
if let Some((_, check)) = arguments.iter().find(|(str, _)| *str == arg) {
*check
} else {
bail!(
"Invalid argument: {arg:?}.\nEnter one of: {}.",
arguments[1..]
.iter()
.map(|(s, _)| s)
.fold(arguments[0].0.to_owned(), |c, v| c + ", " + v)
);
}
} else {
Check::all()
};

let sh = Shell::new()?;
if what_to_run.contains(Check::CHECK) {
check(&sh, Target::Default)?;
}
if what_to_run.contains(Check::WASM_CHECK) {
check(&sh, Target::Wasm)?;
}
if what_to_run.contains(Check::EXAMPLE_CHECK) {
example_check(&sh)?;
}
if what_to_run.contains(Check::FMT) {
fmt(&sh)?;
}
if what_to_run.contains(Check::TEST) {
test(&sh)?;
}
if what_to_run.contains(Check::DOC_TEST) {
doc_test(&sh)?;
}
if what_to_run.contains(Check::DOC_CHECK) {
doc_check(&sh)?;
}
if what_to_run.contains(Check::CLIPPY) {
clippy(&sh)?;
}
Ok(())
}

fn check(sh: &Shell, target: Target) -> anyhow::Result<()> {
let target_flags = &target.flags();
cmd!(sh, "cargo check {target_flags...}").run()?;
Ok(())
}

fn example_check(sh: &Shell) -> anyhow::Result<()> {
cmd!(sh, "cargo check --examples").run()?;
Ok(())
}

fn fmt(sh: &Shell) -> anyhow::Result<()> {
cmd!(sh, "cargo fmt --all -- --check").run()?;
Ok(())
}

fn test(sh: &Shell) -> anyhow::Result<()> {
cmd!(sh, "cargo test --workspace --lib --bins --tests").run()?;
Ok(())
}

fn doc_test(sh: &Shell) -> anyhow::Result<()> {
cmd!(sh, "cargo test --workspace --doc").run()?;
Ok(())
}

fn doc_check(sh: &Shell) -> anyhow::Result<()> {
cmd!(
sh,
"cargo doc --workspace --all-features --no-deps --document-private-items"
)
.env("RUSTDOCFLAGS", "-Dwarnings")
.run()?;
Ok(())
}

fn clippy(sh: &Shell) -> anyhow::Result<()> {
cmd!(sh, "cargo clippy --workspace --all-targets").run()?;
Ok(())
}
13 changes: 13 additions & 0 deletions ci/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub enum Target {
Default,
Wasm,
}

impl Target {
pub fn flags(&self) -> Vec<String> {
match self {
Target::Default => vec![],
Target::Wasm => vec!["--target".to_owned(), "wasm32-unknown-unknown".to_owned()],
}
}
}
2 changes: 1 addition & 1 deletion src/crc32.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{enet_crc32, os::c_void, ENetBuffer};

/// ENet implementation of CRC32 checksum, for use with
/// [`HostSettings::checksum_fn`](`crate::HostSettings::checksum_fn`).
/// [`HostSettings::checksum_fn`](`crate::HostSettings::checksum`).
pub fn crc32(in_buffers: Vec<&[u8]>) -> u32 {
let mut buffers = vec![];
for in_buffer in in_buffers {
Expand Down
20 changes: 19 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,27 @@
non_snake_case,
non_upper_case_globals,
unused_assignments,
unused_mut
unused_mut,
clippy::zero_ptr,
clippy::needless_return,
clippy::unnecessary_cast,
clippy::toplevel_ref_arg,
clippy::ptr_offset_with_cast,
clippy::nonminimal_bool,
clippy::single_match,
clippy::unnecessary_mut_passed,
clippy::comparison_chain,
clippy::unnecessary_literal_unwrap,
clippy::let_and_return,
clippy::type_complexity,
clippy::new_without_default,
clippy::precedence,
clippy::collapsible_if,
clippy::collapsible_else_if
)]
#![warn(missing_docs)]
// https://github.com/rust-lang/rust-clippy/issues/11382
#![allow(clippy::arc_with_non_send_sync)]

use std::{mem::MaybeUninit, time::Duration};

Expand Down