Skip to content
Open
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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- run: cargo fmt --all --check
- run: cargo run -p codegen --locked -- --check
- run: cargo clippy --workspace --all-targets --all-features --locked
- run: cargo build -p openfga-model --target thumbv7em-none-eabihf --locked
- run: cargo build -p openfga-model -p openfga-language --target thumbv7em-none-eabihf --locked
- run: cargo doc --no-deps --all-features --locked
env:
RUSTDOCFLAGS: -D warnings
Expand All @@ -37,7 +37,7 @@ jobs:
with:
persist-credentials: false
- run: rustup toolchain install 1.88 --profile minimal && rustup default 1.88
- run: cargo check -p openfga-model --lib --locked
- run: cargo check -p openfga-model -p openfga-language --lib --locked

coverage:
runs-on: ubuntu-latest
Expand All @@ -56,8 +56,8 @@ jobs:
with:
tool: cargo-llvm-cov
- run: mkdir -p target/coverage
- run: cargo llvm-cov -p openfga-model --all-targets --all-features --locked --no-report
- run: cargo llvm-cov -p openfga-model --doc --all-features --locked --no-clean --lcov --output-path target/coverage/coverage.lcov
- run: cargo llvm-cov -p openfga-model -p openfga-language --all-targets --all-features --locked --no-report
- run: cargo llvm-cov -p openfga-model -p openfga-language --doc --all-features --locked --no-clean --lcov --output-path target/coverage/coverage.lcov
- uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1
if: github.actor != 'dependabot[bot]' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository)
with:
Expand Down
161 changes: 161 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "3"
members = ["codegen", "crates/openfga-model"]
members = ["codegen", "crates/openfga-model", "crates/openfga-language"]

[workspace.package]
edition = "2024"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
| Crate | What it holds |
|---|---|
| [`openfga-model`](crates/openfga-model) | The model types generated from `authzmodel.proto`, with protobuf binary and JSON encodings |
| [`openfga-language`](crates/openfga-language) | The OpenFGA DSL parser and model lowering |

Conformance tests read the pinned [openfga/language](https://github.com/openfga/language) corpus, so clone with `--recurse-submodules`.
27 changes: 27 additions & 0 deletions crates/openfga-language/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "openfga-language"
version = "0.1.0"
edition.workspace = true
rust-version.workspace = true
license.workspace = true
repository.workspace = true
authors.workspace = true
description = "OpenFGA DSL parser and model lowering for no_std"
readme = "README.md"
documentation = "https://docs.rs/openfga-language"
keywords = ["openfga", "authorization", "zanzibar", "dsl", "no-std"]
categories = ["authentication", "parser-implementations", "no-std"]

[dependencies]
openfga-model = { version = "0.1.0", path = "../openfga-model" }
pest = { version = "2.8", default-features = false }
pest_derive = { version = "2.8", default-features = false }
thiserror = { version = "2", default-features = false }

[dev-dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_norway = "0.9"

[lints]
workspace = true
18 changes: 18 additions & 0 deletions crates/openfga-language/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# openfga-language

[![CI](https://github.com/LucaCappelletti94/openfga-rs/actions/workflows/ci.yml/badge.svg)](https://github.com/LucaCappelletti94/openfga-rs/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/LucaCappelletti94/openfga-rs/graph/badge.svg)](https://codecov.io/gh/LucaCappelletti94/openfga-rs)
[![crates.io](https://img.shields.io/crates/v/openfga-language.svg)](https://crates.io/crates/openfga-language)
[![docs.rs](https://docs.rs/openfga-language/badge.svg)](https://docs.rs/openfga-language)
[![License: Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://github.com/LucaCappelletti94/openfga-rs/blob/main/LICENSE)

Not published yet, so the crates.io and docs.rs badges resolve after the first release.

Parse [OpenFGA](https://openfga.dev) DSL into `openfga-model` types with `no_std` and `alloc`.

```rust
let model = openfga_language::parse("model\n schema 1.1\n\ntype user")?;
assert_eq!(model.schema_version, "1.1");
assert_eq!(model.type_definitions[0].r#type, "user");
# Ok::<(), openfga_language::ParseError>(())
```
63 changes: 63 additions & 0 deletions crates/openfga-language/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#![no_std]
#![doc = include_str!("../README.md")]

extern crate alloc;

mod lowering;

use alloc::string::{String, ToString};
use openfga_model::AuthorizationModel;
use pest::{Parser, error::LineColLocation};

mod grammar {
#[derive(pest_derive::Parser)]
#[grammar = "openfga.pest"]
pub(super) struct DslParser;
}

use grammar::{DslParser, Rule};

/// A DSL syntax or transformation error.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("line {line}, column {column}: {message}")]
pub struct ParseError {
/// One-based source line.
pub line: usize,
/// One-based source column.
pub column: usize,
/// Diagnostic description.
pub message: String,
}

/// Parses an `OpenFGA` DSL model or module into protobuf model types.
///
/// # Errors
/// Returns an error for invalid syntax or duplicate relation, condition, parameter, or extension declarations.
pub fn parse(dsl: &str) -> Result<AuthorizationModel, ParseError> {
let mut cleaned = String::with_capacity(dsl.len());
for (index, line) in dsl.split('\n').enumerate() {
if index != 0 {
cleaned.push('\n');
}
if !line.trim_start_matches(' ').starts_with('#') {
cleaned.push_str(
line.split(" #")
.next()
.unwrap_or_default()
.trim_end_matches(' '),
);
}
}
let source = cleaned.trim_end_matches('\n');
let pairs = DslParser::parse(Rule::main, source).map_err(|error| {
let (line, column) = match error.line_col {
LineColLocation::Pos(position) | LineColLocation::Span(position, _) => position,
};
ParseError {
line,
column,
message: error.variant.message().to_string(),
}
})?;
lowering::lower(pairs)
}
Loading