qlog_dancer/plots/minmax.rs
1// Copyright (C) 2025, Cloudflare, Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10//
11// * Redistributions in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#[derive(Clone, Copy)]
28pub struct XMinMax {
29 pub min: f64,
30 pub max: f64,
31}
32
33impl XMinMax {
34 pub fn new(min: f64, max: f64, start: Option<f64>, end: Option<f64>) -> Self {
35 let mut minmax = Self { min, max };
36
37 if let Some(s) = start {
38 minmax.min = s;
39 }
40
41 if let Some(e) = end {
42 minmax.max = minmax.max.min(e);
43 }
44
45 minmax
46 }
47
48 pub fn range(&self) -> std::ops::Range<f64> {
49 self.min..self.max
50 }
51}
52
53pub struct XYMinMax<Y> {
54 pub x: XMinMax,
55 pub y_range: std::ops::Range<Y>,
56}
57
58impl<Y> XYMinMax<Y> {
59 pub fn init(
60 x_data_range: std::ops::Range<f64>, x_start: Option<f64>,
61 x_end: Option<f64>, y_range: std::ops::Range<Y>,
62 ) -> Self {
63 let x =
64 XMinMax::new(x_data_range.start, x_data_range.end, x_start, x_end);
65
66 Self { x, y_range }
67 }
68}