Skip to content

Commit

Permalink
misc: omit the manual max_duration, use the rust Duration::MAX
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanbohan committed Dec 12, 2022
1 parent b890260 commit 5c66a88
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::parser::TokenType;

/// EvalStmt holds an expression and information on the range it should
/// be evaluated on.
// TODO:
pub struct EvalStmt {
expr: Expr, // Expression to be evaluated.

Expand Down
1 change: 0 additions & 1 deletion src/parser/promql.y
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ use std::time::{Duration, Instant};

use crate::parser::{lexeme_to_string, span_to_string, lexeme_to_token};
use crate::parser::{Expr, Token};
use crate::parser::value::STALE_STR;

use crate::label::{self, Label, Labels, MatchOp, Matcher, Matchers, METRIC_NAME, new_matcher};

Expand Down
2 changes: 0 additions & 2 deletions src/parser/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::fmt::{self, Display};

pub const STALE_STR: &'static str = "stale";

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ValueType {
Vector,
Expand Down
17 changes: 4 additions & 13 deletions src/util/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ use lazy_static::lazy_static;
use regex::Regex;
use std::time::Duration;

/// 290 years
const MAX_DURATION: Duration = Duration::from_secs(60 * 60 * 24 * 365 * 290);

lazy_static! {
static ref DURATION_RE: Regex = Regex::new(
r"^((?P<year>[0-9]+)y)?((?P<week>[0-9]+)w)?((?P<day>[0-9]+)d)?((?P<hour>[0-9]+)h)?((?P<minute>[0-9]+)m)?((?P<second>[0-9]+)s)?((?P<milli>[0-9]+)ms)?$",
Expand Down Expand Up @@ -56,7 +53,7 @@ pub fn parse_duration(ds: &str) -> Result<Duration, String> {
add("second", 1000); // s
add("milli", 1); // ms

if result.as_secs() > MAX_DURATION.as_secs() {
if result > Duration::MAX {
return Err("duration out of range".into());
}

Expand Down Expand Up @@ -118,16 +115,10 @@ mod tests {
#[test]
fn test_invalid_duration() {
let ds = vec![
"1",
"1y1m1d",
"-1w",
"1.5d",
"d",
"294y",
"200y10400w",
"107675d",
"2584200h",
"1", "1y1m1d", "-1w", "1.5d", "d",
"",
// these are invalid in PromQL Go Version
// "294y", "200y10400w", "107675d", "2584200h",
];
for d in ds {
assert!(parse_duration(d).is_err(), "{} is invalid duration!", d);
Expand Down

0 comments on commit 5c66a88

Please sign in to comment.