Skip to content

Commit

Permalink
optional serde support
Browse files Browse the repository at this point in the history
  • Loading branch information
vthriller committed Jan 29, 2024
1 parent 02004ad commit b7b6f70
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ repository = "https://github.com/vthriller/promql"
quick-error = "2.0"
nom = "7.0"
builder-pattern = "0.3"

# feature: serializable
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }

[features]
default = []
serializable = ["serde", "serde_derive"]
8 changes: 8 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use crate::utils::{

/// PromQL operators
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serializable", derive(serde_derive::Serialize))]
pub enum Op {
/** `^` */
Pow(Option<OpMod>),
Expand Down Expand Up @@ -93,12 +94,14 @@ pub enum Op {
}

#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serializable", derive(serde_derive::Serialize))]
pub enum OpModAction {
RestrictTo,
Ignore,
}
/// Vector matching operator modifier (`on (…)`/`ignoring (…)`).
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serializable", derive(serde_derive::Serialize))]
pub struct OpMod {
/// Action applied to a list of vectors; whether `on (…)` or `ignored(…)` is used after the operator.
pub action: OpModAction,
Expand All @@ -109,23 +112,27 @@ pub struct OpMod {
}

#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serializable", derive(serde_derive::Serialize))]
pub enum OpGroupSide {
Left,
Right,
}
/// Vector grouping operator modifier (`group_left(…)`/`group_right(…)`).
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serializable", derive(serde_derive::Serialize))]
pub struct OpGroupMod {
pub side: OpGroupSide,
pub labels: Vec<String>,
}

#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serializable", derive(serde_derive::Serialize))]
pub enum AggregationAction {
Without,
By,
}
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serializable", derive(serde_derive::Serialize))]
pub struct AggregationMod {
// Action applied to a list of vectors; whether `by (…)` or `without (…)` is used.
pub action: AggregationAction,
Expand All @@ -134,6 +141,7 @@ pub struct AggregationMod {

/// AST node.
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serializable", derive(serde_derive::Serialize))]
pub enum Node {
/// Chain of operators with similar mods: `a + ignoring (foo) b + ignoring (foo) c`
Operator {
Expand Down
3 changes: 3 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use crate::utils::{

/// Label filter operators.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serializable", derive(serde_derive::Serialize))]
pub enum LabelMatchOp {
/** `=` */
Eq,
Expand All @@ -71,6 +72,7 @@ pub enum LabelMatchOp {

/// Single label filter.
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serializable", derive(serde_derive::Serialize))]
pub struct LabelMatch {
pub name: String,
pub op: LabelMatchOp,
Expand Down Expand Up @@ -143,6 +145,7 @@ assert_eq!(
```
*/
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serializable", derive(serde_derive::Serialize))]
pub struct Vector {
/// Set of label filters
pub labels: Vec<LabelMatch>,
Expand Down

0 comments on commit b7b6f70

Please sign in to comment.