Skip to content

Commit

Permalink
feat(serde): add serde integration
Browse files Browse the repository at this point in the history
  • Loading branch information
sumimakito committed Aug 22, 2023
1 parent ac64af0 commit de67e35
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
35 changes: 35 additions & 0 deletions Cargo.lock

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

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ cidr = "0.2"
lazy_static = "1.4"
uuid = "1.4"
regex = "1.9"
serde = { version = "1.0", features = ["derive"], optional = true }
serde_regex = { version = "1.1.0", optional = true }

[lib]
crate-type = ["cdylib", "staticlib"]
crate-type = ["lib", "cdylib", "staticlib"]

[features]
no-ffi = []
serde = ["cidr/serde", "dep:serde", "serde_regex"]
12 changes: 12 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@ use cidr::IpCidr;
use regex::Regex;
use std::net::IpAddr;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub enum Expression {
Logical(Box<LogicalExpression>),
Predicate(Predicate),
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub enum LogicalExpression {
And(Expression, Expression),
Or(Expression, Expression),
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Eq)]
pub enum LhsTransformations {
Lower,
Any,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Eq)]
pub enum BinaryOperator {
Equals, // ==
Expand All @@ -37,12 +44,14 @@ pub enum BinaryOperator {
Contains, // contains
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub enum Value {
String(String),
IpCidr(IpCidr),
IpAddr(IpAddr),
Int(i64),
#[cfg_attr(feature = "serde", serde(with = "serde_regex"))]
Regex(Regex),
}

Expand Down Expand Up @@ -73,6 +82,7 @@ impl Value {
}
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Eq, PartialEq)]
#[repr(C)]
pub enum Type {
Expand All @@ -83,6 +93,7 @@ pub enum Type {
Regex,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct Lhs {
pub var_name: String,
Expand All @@ -107,6 +118,7 @@ impl Lhs {
}
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct Predicate {
pub lhs: Lhs,
Expand Down
18 changes: 10 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
mod ast;
mod context;
pub mod ffi;
mod interpreter;
mod parser;
mod router;
mod schema;
mod semantics;
pub mod ast;

Check warning on line 1 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust Clippy Report

the "no-" prefix in the feature name "no-ffi" is negative

warning: the "no-" prefix in the feature name "no-ffi" is negative | = help: consider renaming the feature to "ffi", but make sure the feature adds functionality = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#negative_feature_names = note: `-W clippy::negative-feature-names` implied by `-W clippy::cargo`

Check warning on line 1 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust Clippy Report

package `atc-router` is missing `package.categories` metadata

warning: package `atc-router` is missing `package.categories` metadata | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cargo_common_metadata

Check warning on line 1 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust Clippy Report

package `atc-router` is missing `package.keywords` metadata

warning: package `atc-router` is missing `package.keywords` metadata | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cargo_common_metadata = note: `-W clippy::cargo-common-metadata` implied by `-W clippy::cargo`
pub mod context;
pub mod interpreter;
pub mod parser;
pub mod router;
pub mod schema;
pub mod semantics;
mod ast_tests;

#[cfg(not(feature = "no-ffi"))]
pub mod ffi;

#[macro_use]
extern crate pest_derive;

0 comments on commit de67e35

Please sign in to comment.