Skip to content

Commit

Permalink
Format, and rewrite CI with nix
Browse files Browse the repository at this point in the history
  • Loading branch information
kiranshila committed Mar 18, 2024
1 parent 7b42b9e commit dbbc021
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 105 deletions.
120 changes: 60 additions & 60 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
on: push
name: CI
name: Check and build the library using Nix
on:
push:
branches:
- main
tags:
- "v*.*.*"
pull_request:
branches:
- main

jobs:
lint:
name: Lint
check:
name: Check/Lint
runs-on: ubuntu-latest
env:
RUSTFLAGS: -D warnings
CARGO_TERM_COLOR: always
steps:
- uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 0
- uses: actions-rs/toolchain@v1
with:
# We need nightly for fmt
toolchain: nightly
override: true
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v1
- name: Lint (clippy)
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-targets
- name: Lint (rustfmt)
uses: actions-rs/cargo@v1
- name: git checkout
uses: actions/checkout@v3
- name: Install Nix
uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
- name: Check Nixpkgs inputs
uses: DeterminateSystems/flake-checker-action@main
with:
command: fmt
args: --all --check
fail-mode: true

build:
name: Build and test
# Nix-specific logic begins here
- name: Check Rust formatting
run: |
nix develop --command cargo fmt --all --check
- name: Check Clippy lints
run: |
nix develop --command cargo clippy --all-targets
- name: Check spelling
run: |
nix develop --command \
codespell \
--skip target,.git \
--ignore-words-list crate
test_and_build:
name: Test and build library
needs: check
runs-on: ubuntu-latest
strategy:
matrix:
rust-version: [stable, 1.60, nightly]
fail-fast: false
env:
RUSTFLAGS: -D warnings
CARGO_TERM_COLOR: always
steps:
- uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 0
- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust-version }}
override: true
components: llvm-tools-preview
- uses: Swatinem/rust-cache@v1
- name: Build all targets with
uses: actions-rs/cargo@v1
with:
command: build
args: --all-targets
- name: Tests
uses: actions-rs/cargo@v1
- name: git checkout
uses: actions/checkout@v3
- name: Install Nix
uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
- name: Set up Rust cache
uses: actions/cache@v3
with:
command: test
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: psrdada-rs-${{ hashFiles('**/Cargo.lock') }}
- name: Test library
run: |
nix develop --command cargo test
- name: Build library
run: |
nix develop --command cargo build
- name: Generate code coverage
if: startsWith(matrix.rust-version, 'stable')
run: cargo llvm-cov --workspace --lcov --output-path lcov.info
run: nix develop --command cargo llvm-cov --workspace --lcov --output-path lcov.info
- name: Upload coverage to Codecov
if: startsWith(matrix.rust-version, 'stable')
uses: codecov/codecov-action@v3
with:
files: lcov.info
fail_ci_if_error: true
fail_ci_if_error: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ psrdada-sys/vendor/.ccls-cache
.vscode/
.direnv
result
lcov.info
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let mut client = DadaClientBuilder::new(key).build().unwrap();
// Split into individual clients
let (_, mut data_client) = client.split();

// Construct the writer (mutable borrow), panicing if a lock is not obtainable
// Construct the writer (mutable borrow), panicking if a lock is not obtainable
let mut writer = data_client.writer().unwrap();

// Grab the next block in the ring (assuming we can)
Expand All @@ -53,7 +53,7 @@ write_block.commit();
// Drop the writer to unlock it (this would happen also when the writer leaves scope)
drop(writer);

// Construct the reader (mutable borrow), panicing if a lock is not obtainable
// Construct the reader (mutable borrow), panicking if a lock is not obtainable
let mut reader = data_client.reader().unwrap();

// Grab the next read block in the ring
Expand Down
7 changes: 4 additions & 3 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use psrdada::prelude::*;
use std::io::{Read, Write};

use psrdada::prelude::*;

fn main() {
// Build the paired client
let key = 0xb0ba;
Expand All @@ -9,7 +10,7 @@ fn main() {
// Split into individual clients
let (_, mut data_client) = client.split();

// Construct the writer (mutable borrow), panicing if a lock is not obtainable
// Construct the writer (mutable borrow), panicking if a lock is not obtainable
let mut writer = data_client.writer().unwrap();

// Grab the next block in the ring (assuming we can)
Expand All @@ -24,7 +25,7 @@ fn main() {
// Drop the writer to unlock it (this would happen also when the writer leaves scope)
drop(writer);

// Construct the reader (mutable borrow), panicing if a lock is not obtainable
// Construct the reader (mutable borrow), panicking if a lock is not obtainable
let mut reader = data_client.reader().unwrap();

// Grab the next read block in the ring
Expand Down
2 changes: 1 addition & 1 deletion examples/read_transform_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() {
// Double every byte
*x = *y * 2;
});
// No need to lock, mark cleared, or anything like that. That's all implicit wil RAII.
// No need to lock, mark cleared, or anything like that. That's all implicit with RAII.
} else {
println!("Errored on getting the next write block, perhaps that buffer was destroyed?");
break;
Expand Down
35 changes: 31 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,40 @@
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; };

runCiLocally = pkgs.writeScriptBin "ci-local" ''
echo "Checking Rust formatting..."
cargo fmt --check
echo "Checking clippy..."
cargo clippy --all-targets
echo "Checking spelling..."
codespell \
--skip target,.git \
--ignore-words-list crate
echo "Testing Rust code..."
cargo test
echo "Generating code coverage..."
cargo llvm-cov --workspace --lcov --output-path lcov.info
'';

nativeBuildInputs = with pkgs; [ rustPlatform.bindgenHook pkg-config ];
buildInputs = with pkgs; [
(rust-bin.stable.latest.default.override {
extensions = ["rust-src" "rust-analyzer"];
buildInputs = [ runCiLocally ] ++ (with pkgs; [
# Rust stuff, some stuff dev-only
(rust-bin.nightly.latest.default.override {
extensions = ["rust-src" "rust-analyzer" "llvm-tools-preview"];
})
cargo-llvm-cov

# The C-library itself
psrdada.packages.${system}.default
];

# Linting support
codespell
]);
in with pkgs; {
devShells.default = mkShell { inherit buildInputs nativeBuildInputs; };
});
Expand Down
2 changes: 1 addition & 1 deletion psrdada-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Include generated bindings
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

// We need to include some default constructrs, as those #defines don't work in bindgen
// We need to include some default constructors, as those #defines don't work in bindgen
impl Default for ipcbuf_t {
fn default() -> Self {
Self {
Expand Down
22 changes: 12 additions & 10 deletions src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! While the header ringbuffer is just like the data ringbuffer, there is "convention" that
//! it contains key/value pairs of ASCII plaintext. Nothing about the C abstraction guarantees that,
//! so we still have a public API for dealing with the raw bytes. However, this module will introduce some
//! methods that implement the serde to and from these bytes and automaticlly read and write. These methods
//! methods that implement the serde to and from these bytes and automatically read and write. These methods
//! are of course falliable, because who knows what people put on the header buffer.
//!
//! ## Parsing
Expand Down Expand Up @@ -40,12 +40,12 @@
//!
//! Going from a `HashMap<String,String>`, we will print keys as is, separated by a single space with newlines separating pairs.

use crate::{
client::HeaderClient,
errors::{PsrdadaError, PsrdadaResult},
io::DadaClient,
iter::DadaIterator,
use std::{
collections::HashMap,
io::{Read, Write},
str,
};

use nom::{
bytes::complete::{is_not, tag},
character::complete::{line_ending, not_line_ending, space0, space1},
Expand All @@ -55,10 +55,12 @@ use nom::{
IResult,
};
use psrdada_sys::ipcbuf_get_bufsz;
use std::{
collections::HashMap,
io::{Read, Write},
str,

use crate::{
client::HeaderClient,
errors::{PsrdadaError, PsrdadaResult},
io::DadaClient,
iter::DadaIterator,
};

type RawPair<'a> = (&'a [u8], &'a [u8]);
Expand Down
18 changes: 11 additions & 7 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
//!
//! This module reimplements the functionality from `ipcio` from the original library.

use std::marker::PhantomData;

use psrdada_sys::*;
use tracing::{debug, error};

use crate::{
client::{DataClient, HeaderClient},
errors::{PsrdadaError, PsrdadaResult},
};
use psrdada_sys::*;
use std::marker::PhantomData;
use tracing::{debug, error};

mod private {
/// Private token marker to prevent library users from calling certain trait methods
Expand Down Expand Up @@ -36,7 +38,7 @@ pub trait DadaClient {
}

/// The writer associated with a ringbuffer.
/// This comes into existance locked and destructs with an unlock.
/// This comes into existence locked and destructs with an unlock.
pub struct Writer<'a> {
buf: *const ipcbuf_t,
_phantom: PhantomData<&'a ipcbuf_t>,
Expand Down Expand Up @@ -83,7 +85,7 @@ impl Drop for Writer<'_> {
}

/// The reader associated with a ringbuffer
/// This comes into existance locked and destructs with an unlock.
/// This comes into existence locked and destructs with an unlock.
pub struct Reader<'a> {
buf: *const ipcbuf_t,
_phantom: PhantomData<&'a ipcbuf_t>,
Expand Down Expand Up @@ -189,12 +191,14 @@ impl From<i32> for State {

#[cfg(test)]
mod tests {
use std::io::{Read, Write};

use test_log::test;

use crate::{
builder::DadaClientBuilder, client::HduClient, io::DadaClient, iter::DadaIterator,
tests::next_key,
};
use std::io::{Read, Write};
use test_log::test;

#[test]
fn test_read_write_many() {
Expand Down
15 changes: 9 additions & 6 deletions src/io/read.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::iter::DadaIterator;
use std::marker::PhantomData;

use super::Reader;
use psrdada_sys::*;
use std::marker::PhantomData;
use tracing::{debug, error};

use super::Reader;
use crate::iter::DadaIterator;

/// The state associated with an in-progress read. This must be dropped to perform more actions or consumed with [`done`].
///
/// This block comes into with valid data and only exists as long as it is valid
Expand All @@ -19,7 +20,7 @@ impl ReadBlock<'_> {
/// Create a [`ReadBlock`] by mutably borrowing from the [`Reader`].
/// This ensures we can only have one at a time.
///
/// Returns an option if we successfuly got a valid block.
/// Returns an option if we successfully got a valid block.
pub fn new(reader: &mut Reader) -> Option<Self> {
// Test for EOD
if unsafe { ipcbuf_eod(reader.buf as *mut _) } == 1 {
Expand Down Expand Up @@ -113,14 +114,16 @@ impl std::io::Read for ReadBlock<'_> {

#[cfg(test)]
mod tests {
use std::io::{Read, Write};

use test_log::test;

use crate::{
builder::DadaClientBuilder,
io::{read::ReadBlock, DadaClient},
iter::DadaIterator,
tests::next_key,
};
use std::io::{Read, Write};
use test_log::test;

#[test]
fn test_read_write() {
Expand Down
Loading

0 comments on commit dbbc021

Please sign in to comment.