pub mod ast;
pub mod function;
pub mod lex;
pub mod parse;
pub(crate) mod production;
pub mod token;
pub mod value;
pub use ast::{
AggregateExpr, AtModifier, BinModifier, BinaryExpr, Call, EvalStmt, Expr, Extension,
LabelModifier, MatrixSelector, NumberLiteral, Offset, ParenExpr, StringLiteral, SubqueryExpr,
UnaryExpr, VectorMatchCardinality, VectorMatchFillValues, VectorSelector,
};
pub use function::{Function, FunctionArgs};
pub use lex::lexer;
pub use parse::parse;
const INVALID_QUERY_INFO: &str = "invalid promql query";
const INDENT_STR: &str = " ";
const MAX_CHARACTERS_PER_LINE: usize = 100;
pub trait Prettier: std::fmt::Display {
fn pretty(&self, level: usize, max: usize) -> String {
if self.needs_split(max) {
self.format(level, max)
} else {
format!("{}{self}", indent(level))
}
}
fn format(&self, level: usize, _max: usize) -> String {
format!("{}{self}", indent(level))
}
fn needs_split(&self, max: usize) -> bool {
self.to_string().len() > max
}
}
fn indent(n: usize) -> String {
INDENT_STR.repeat(n)
}
#[cfg(test)]
mod tests {
use super::*;
struct Pretty(String);
impl std::fmt::Display for Pretty {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Prettier for Pretty {}
#[test]
fn test_prettier_trait() {
let max = 10;
let level = 1;
let p = Pretty("demo".into());
assert!(!p.needs_split(max));
assert_eq!(p.format(level, max), p.pretty(level, max));
let p = Pretty("demo_again.".into());
assert!(p.needs_split(max));
assert_eq!(p.format(level, max), p.pretty(level, max));
}
}