Skip to content

Commit

Permalink
refactor(capi): Move CAPI generation to its own crate (#234)
Browse files Browse the repository at this point in the history
* refactor(capi): Move CAPI generation to its own crate

* reduce Biscuit visibility

* remove capi.rs

* add `print_block_source` c binding

* add test for `biscuit_print_block_source` + use `inttypes.h` macros for handling cross-platform `uint64_t` type

* remove `block()` usage and C-API usage
  • Loading branch information
ptondereau authored Nov 3, 2024
1 parent bf2f22a commit 4e6b2c8
Show file tree
Hide file tree
Showing 13 changed files with 425 additions and 357 deletions.
67 changes: 29 additions & 38 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Release
on:
push:
tags:
- '*'
- "*"

permissions:
contents: write
Expand All @@ -12,43 +12,34 @@ env:
CARGO_TERM_COLOR: always

jobs:

build:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: macos-latest-large
target_arch: x86_64-apple-darwin
- os: macos-latest
target_arch: aarch64-apple-darwin
- os: ubuntu-latest
target_arch: x86_64-unknown-linux-gnu

steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Cache
uses: actions/cache@v2
with:
path: |
~/.cargo
target/
key: ${{ runner.os }}-cargoc-${{ hashFiles('Cargo.toml') }}
restore-keys: |
${{ runner.os }}-cargoc-${{ hashFiles('Cargo.toml') }}
${{ runner.os }}-cargoc-
${{ runner.os }}-${{ hashFiles('Cargo.toml') }}
${{ runner.os }}-
- name: Install cargo-c
uses: actions-rs/cargo@v1
with:
command: install
args: cargo-c
- name: Run cargo-c tests
uses: actions-rs/cargo@v1
with:
command: ctest
args: --release
- name: Build
uses: actions-rs/cargo@v1
with:
command: cinstall
args: --release --prefix=/usr --destdir=./build
- name: Compress
run: tar cvzf biscuit_c-${{github.ref_name}}-x86_64.tar.gz -C build/ .
- name: Release
uses: softprops/action-gh-release@v1
with:
files: biscuit_c-${{github.ref_name}}-x86_64.tar.gz
- name: Checkout sources
uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
target: ${{ matrix.target_arch }}
- name: Install cargo-c
run: cargo install cargo-c --version 0.10.5+cargo-0.83.0
- name: Run cargo-c tests
run: cargo ctest --release --features="capi"
if: matrix.os == 'ubuntu-latest' && matrix.target_arch == 'x86_64-unknown-linux-gnu'
- name: Build C-API
run: cargo cinstall --release --prefix=/usr --destdir=./build --features="capi"
- name: Compress
run: tar cvzf biscuit_capi-${{github.ref_name}}-${{matrix.target_arch}}.tar.gz -C build/ .
- name: Release
uses: softprops/action-gh-release@v1
with:
files: biscuit_capi-${{github.ref_name}}-${{matrix.target_arch}}.tar.gz
79 changes: 30 additions & 49 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,50 @@ name: Rust

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main, v5 ]
branches: [main, v5]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Cache
uses: actions/cache@v2
with:
path: |
~/.cargo
target/
key: ${{ runner.os }}-${{ hashFiles('Cargo.toml') }}
restore-keys: |
${{ runner.os }}-${{ hashFiles('Cargo.toml') }}
${{ runner.os }}-
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --features="serde-error,bwk" --verbose
- name: Check samples
run: |
cd biscuit-auth
cargo run --release --example testcases --features serde-error -- ./samples --json > ./samples/samples.json
git diff --exit-code
- uses: actions/checkout@v2
- name: Cache
uses: actions/cache@v2
with:
path: |
~/.cargo
target/
key: ${{ runner.os }}-${{ hashFiles('Cargo.toml') }}
restore-keys: |
${{ runner.os }}-${{ hashFiles('Cargo.toml') }}
${{ runner.os }}-
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --features="serde-error,bwk" --verbose
- name: Check samples
run: |
cd biscuit-auth
cargo run --release --example testcases --features serde-error -- ./samples --json > ./samples/samples.json
git diff --exit-code
capi:
runs-on: ubuntu-latest

steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Cache
uses: actions/cache@v2
with:
path: |
~/.cargo
target/
key: ${{ runner.os }}-cargoc-${{ hashFiles('Cargo.toml') }}
restore-keys: |
${{ runner.os }}-cargoc-${{ hashFiles('Cargo.toml') }}
${{ runner.os }}-cargoc-
${{ runner.os }}-${{ hashFiles('Cargo.toml') }}
${{ runner.os }}-
- name: Install cargo-c
uses: actions-rs/cargo@v1
with:
command: install
args: cargo-c --version 0.9.14+cargo-0.66
- name: Run cargo-c tests
uses: actions-rs/cargo@v1
with:
command: ctest
args: --release
- name: Checkout sources
uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Install cargo-c
run: cargo install cargo-c --version 0.10.5+cargo-0.83.0
- name: Run cargo-c tests
run: cargo ctest --features="capi" --release

coverage:
name: Coverage
Expand Down Expand Up @@ -94,4 +75,4 @@ jobs:
uses: codecov/codecov-action@v4.0.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: biscuit-auth/biscuit-rust
slug: biscuit-auth/biscuit-rust
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["biscuit-auth", "biscuit-quote", "biscuit-parser"]
members = ["biscuit-auth", "biscuit-quote", "biscuit-parser", "biscuit-capi"]
14 changes: 1 addition & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,7 @@ biscuit implementations come with a default symbol table to avoid transmitting f

# C bindings

This project can generate C bindings with [cargo-c](https://crates.io/crates/cargo-c).

compile it with:

```
cargo cinstall --prefix=/usr --destdir=./build
```

Run C integration tests with:

```
cargo ctest
```
You can find the C bindings and documentation in the [`biscuit-capi`](./biscuit-capi/README.md) crate.

## License

Expand Down
28 changes: 5 additions & 23 deletions biscuit-auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ repository = "https://github.com/biscuit-auth/biscuit-rust"

[features]
default = ["regex-full", "datalog-macro"]
regex-full = [ "regex/perf", "regex/unicode"]
# used by cargo-c to signal the compilation of C bindings
capi = ["inline-c"]
regex-full = ["regex/perf", "regex/unicode"]
wasm = ["wasm-bindgen", "getrandom/wasm-bindgen"]
# used by biscuit-wasm to serialize errors to JSON
serde-error = ["serde", "biscuit-parser/serde-error"]
Expand All @@ -33,12 +31,11 @@ sha2 = "^0.9"
prost = "0.10"
prost-types = "0.10"
regex = { version = "1.5", default-features = false, features = ["std"] }
nom = {version = "7", default-features = false, features = ["std"] }
nom = { version = "7", default-features = false, features = ["std"] }
hex = "0.4"
zeroize = { version = "1", default-features = false }
thiserror = "1"
rand = { version = "0.8" }
inline-c = { version = "0.1", optional = true }
wasm-bindgen = { version = "0.2", optional = true }
base64 = "0.13.0"
ed25519-dalek = { version = "2.0.0", features = ["rand_core", "zeroize"] }
Expand All @@ -48,7 +45,9 @@ time = { version = "0.3.7", features = ["formatting", "parsing"] }
uuid = { version = "1", optional = true }
biscuit-parser = { version = "0.1.2", path = "../biscuit-parser" }
biscuit-quote = { version = "0.2.2", optional = true, path = "../biscuit-quote" }
chrono = { version = "0.4.26", optional = true, default-features = false, features = ["serde"] }
chrono = { version = "0.4.26", optional = true, default-features = false, features = [
"serde",
] }


[dev-dependencies]
Expand All @@ -64,23 +63,6 @@ codspeed-bencher-compat = "2.6.0"
#[build-dependencies]
#prost-build = "0.10"

[package.metadata.capi.library]
# Used as the library name and defaults to the crate name. This might get
# prefixed with `lib` depending on the target platform.
name = "biscuit_auth"

include = [
"Cargo.toml",
"cbindgen.toml",
"build.rs",
"examples/*.rs",
"LICENSE",
"README.md",
"src/*.rs",
"src/*/*.rs",
"tests/*.rs"
]

[[example]]
name = "testcases"
required-features = ["serde-error"]
Expand Down
1 change: 1 addition & 0 deletions biscuit-auth/src/datalog/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ impl SymbolTable {
}
}
}

pub fn print_fact(&self, f: &Fact) -> String {
self.print_predicate(&f.predicate)
}
Expand Down
6 changes: 0 additions & 6 deletions biscuit-auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,6 @@ pub use token::Biscuit;
pub use token::RootKeyProvider;
pub use token::{ThirdPartyBlock, ThirdPartyRequest};

#[cfg(cargo_c)]
mod capi;

#[cfg(cargo_c)]
pub use capi::*;

#[cfg(feature = "bwk")]
mod bwk;
#[cfg(feature = "bwk")]
Expand Down
30 changes: 30 additions & 0 deletions biscuit-capi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "biscuit-capi"
version = "5.0.0" # Should keep the same version as biscuit-auth
description = "C API for Biscuit"
authors = ["Geoffroy Couprie <contact@geoffroycouprie.com>"]
edition = "2018"
license = "Apache-2.0"
documentation = "https://docs.rs/biscuit-auth"
homepage = "https://github.com/biscuit-auth/biscuit"
repository = "https://github.com/biscuit-auth/biscuit-rust"

[dependencies]
biscuit-auth = { version = "5.0.0", path = "../biscuit-auth", features = [
"datalog-macro",
"serde-error",
] }
inline-c = { version = "0.1", optional = true }
rand = "0.8"

[features]
default = []
capi = ["inline-c"]

[package.metadata.capi.library]
# Used as the library name and defaults to the crate name. This might get
# prefixed with `lib` depending on the target platform.
name = "biscuit_auth"

[package.metadata.capi.header]
name = "biscuit_auth"
Loading

0 comments on commit 4e6b2c8

Please sign in to comment.