diff --git a/src/format/mod.rs b/src/format/mod.rs index f507834..06ef31f 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -20,8 +20,8 @@ pub struct F64Formatter { impl F64Formatter { /// Initializes a new `HumanF64Formatter` with default values. - pub fn new(decimals: usize) -> F64Formatter { - F64Formatter { + pub fn new(decimals: usize) -> Self { + Self { decimals, divisor: 0, suffix: "".to_owned(), @@ -30,14 +30,14 @@ impl F64Formatter { /// Initializes a new `HumanF64Formatter` for formatting numbers in the /// provided range. - pub fn new_with_range(range: Range) -> F64Formatter { + pub fn new_with_range(range: Range) -> Self { // Range let mut decimals = 3; let mut divisor = 0_u8; let mut suffix = UNITS[0].to_owned(); let difference = range.end - range.start; if difference == 0.0 { - return F64Formatter { + return Self { decimals, divisor, suffix, @@ -51,7 +51,7 @@ impl F64Formatter { divisor = ((log - 1) / 3).min(5) as u8; } suffix = UNITS[divisor as usize].to_owned(); - F64Formatter { + Self { decimals, divisor, suffix, @@ -74,8 +74,8 @@ pub struct HorizontalScale { } impl HorizontalScale { - pub fn new(scale: usize) -> HorizontalScale { - HorizontalScale { + pub fn new(scale: usize) -> Self { + Self { scale: 1.max(scale), } } diff --git a/src/plot/histogram.rs b/src/plot/histogram.rs index 5da7b27..3b82228 100644 --- a/src/plot/histogram.rs +++ b/src/plot/histogram.rs @@ -14,8 +14,8 @@ struct Bucket { } impl Bucket { - fn new(range: Range) -> Bucket { - Bucket { range, count: 0 } + fn new(range: Range) -> Self { + Self { range, count: 0 } } fn inc(&mut self) { @@ -63,12 +63,12 @@ impl Histogram { /// `precision` is an Option with the number of decimals to display. If /// "None" is used, human units will be used, with an heuristic based on the /// input data for deciding the units and the decimal places. - pub fn new(vec: &[f64], mut options: HistogramOptions) -> Histogram { + pub fn new(vec: &[f64], mut options: HistogramOptions) -> Self { options.intervals = options.intervals.min(vec.len()); let stats = Stats::new(vec, options.precision); let size = options.intervals.min(vec.len()); let step = (stats.max - stats.min) / size as f64; - let mut histogram = Histogram::new_with_stats(step, stats, options); + let mut histogram = Self::new_with_stats(step, stats, options); histogram.load(vec); histogram } @@ -79,14 +79,14 @@ impl Histogram { /// Parameters are similar to those on the `new` method, but a parameter /// named `stats` is needed to decide how future data (to be injected with /// the load method) will be accommodated. - pub fn new_with_stats(step: f64, stats: Stats, options: HistogramOptions) -> Histogram { + pub fn new_with_stats(step: f64, stats: Stats, options: HistogramOptions) -> Self { let mut vec = Vec::::with_capacity(options.intervals); let mut lower = stats.min; for _ in 0..options.intervals { vec.push(Bucket::new(lower..lower + step)); lower += step; } - Histogram { + Self { vec, max: stats.min + (step * options.intervals as f64), step, diff --git a/src/plot/matchbar.rs b/src/plot/matchbar.rs index b6318b6..243aa67 100644 --- a/src/plot/matchbar.rs +++ b/src/plot/matchbar.rs @@ -13,8 +13,8 @@ pub struct MatchBarRow { } impl MatchBarRow { - pub fn new(string: &str) -> MatchBarRow { - MatchBarRow { + pub fn new(string: &str) -> Self { + Self { label: string.to_string(), count: 0, } @@ -38,14 +38,14 @@ pub struct MatchBar { impl MatchBar { /// Creates a Histogram from a vector of `MatchBarRow` elements. - pub fn new(vec: Vec) -> MatchBar { + pub fn new(vec: Vec) -> Self { let mut top_lenght: usize = 0; let mut top_values: usize = 0; for row in vec.iter() { top_lenght = top_lenght.max(row.label.len()); top_values = top_values.max(row.count); } - MatchBar { + Self { vec, top_values, top_lenght, diff --git a/src/plot/splittimehist.rs b/src/plot/splittimehist.rs index ad671d2..4b81a96 100644 --- a/src/plot/splittimehist.rs +++ b/src/plot/splittimehist.rs @@ -15,8 +15,8 @@ struct TimeBucket { } impl TimeBucket { - fn new(start: DateTime, counts: usize) -> TimeBucket { - TimeBucket { + fn new(start: DateTime, counts: usize) -> Self { + Self { start, count: vec![0; counts], } @@ -52,11 +52,7 @@ impl SplitTimeHistogram { /// `size` is the number of time slots in the histogram. Parameter 'ts' is /// a slice of tuples of `DateTime` (the timestamp of a term occurrence) and /// the index of the term in the `strings` parameter. - pub fn new( - size: usize, - strings: Vec, - ts: &[(DateTime, usize)], - ) -> SplitTimeHistogram { + pub fn new(size: usize, strings: Vec, ts: &[(DateTime, usize)]) -> Self { let mut vec = Vec::::with_capacity(size); let min = ts.iter().min().unwrap().0; let max = ts.iter().max().unwrap().0; @@ -65,7 +61,7 @@ impl SplitTimeHistogram { for i in 0..size { vec.push(TimeBucket::new(min + (inc * i as i32), strings.len())); } - let mut sth = SplitTimeHistogram { + let mut sth = Self { vec, strings, min, diff --git a/src/plot/terms.rs b/src/plot/terms.rs index 204e802..6491662 100644 --- a/src/plot/terms.rs +++ b/src/plot/terms.rs @@ -20,8 +20,8 @@ impl CommonTerms { /// Create and empty `CommonTerms`. /// /// `lines` is the number of lines to be displayed. - pub fn new(lines: usize) -> CommonTerms { - CommonTerms { + pub fn new(lines: usize) -> Self { + Self { terms: HashMap::new(), lines, } diff --git a/src/plot/timehist.rs b/src/plot/timehist.rs index 92d7ec0..b98974d 100644 --- a/src/plot/timehist.rs +++ b/src/plot/timehist.rs @@ -13,8 +13,8 @@ struct TimeBucket { } impl TimeBucket { - fn new(start: DateTime) -> TimeBucket { - TimeBucket { start, count: 0 } + fn new(start: DateTime) -> Self { + Self { start, count: 0 } } fn inc(&mut self) { @@ -38,7 +38,7 @@ impl TimeHistogram { /// Creates a Histogram from a vector of `DateTime` elements. /// /// `size` is the number of histogram buckets to display. - pub fn new(size: usize, ts: &[DateTime]) -> TimeHistogram { + pub fn new(size: usize, ts: &[DateTime]) -> Self { let mut vec = Vec::::with_capacity(size); let min = *ts.iter().min().unwrap(); let max = *ts.iter().max().unwrap(); @@ -47,7 +47,7 @@ impl TimeHistogram { for i in 0..size { vec.push(TimeBucket::new(min + (inc * i as i32))); } - let mut timehist = TimeHistogram { + let mut timehist = Self { vec, min, max, diff --git a/src/plot/xy.rs b/src/plot/xy.rs index 654c486..e670092 100644 --- a/src/plot/xy.rs +++ b/src/plot/xy.rs @@ -31,8 +31,8 @@ impl XyPlot { /// `precision` is an Option with the number of decimals to display. If /// "None" is used, human units will be used, with an heuristic based on the /// input data for deciding the units and the decimal places. - pub fn new(vec: &[f64], width: usize, height: usize, precision: Option) -> XyPlot { - let mut plot = XyPlot::new_with_stats(width, height, Stats::new(vec, precision), precision); + pub fn new(vec: &[f64], width: usize, height: usize, precision: Option) -> Self { + let mut plot = Self::new_with_stats(width, height, Stats::new(vec, precision), precision); plot.load(vec); plot } @@ -47,8 +47,8 @@ impl XyPlot { height: usize, stats: Stats, precision: Option, - ) -> XyPlot { - XyPlot { + ) -> Self { + Self { x_axis: Vec::with_capacity(width), y_axis: Vec::with_capacity(height), width, diff --git a/src/read/dateparser.rs b/src/read/dateparser.rs index f636e1a..fc18b6f 100644 --- a/src/read/dateparser.rs +++ b/src/read/dateparser.rs @@ -28,14 +28,14 @@ pub struct LogDateParser { } impl LogDateParser { - pub fn new(log_line: &str, format_string: &Option) -> Result { + pub fn new(log_line: &str, format_string: &Option) -> Result { match format_string { Some(ts_format) => Self::new_with_format(log_line, ts_format), None => Self::new_with_guess(log_line), } } - fn new_with_guess(log_line: &str) -> Result { + fn new_with_guess(log_line: &str) -> Result { // All the guess work assume that datetimes start with a digit, and that // digit is the first digit in the log line. The approach is to locate // the 1st digit and then try to parse as much text as possible with any @@ -45,7 +45,7 @@ impl LogDateParser { if c.is_ascii_digit() { for j in (i..(i + MAX_LEN).min(log_line.len() + 1)).rev() { if let Some(parser) = Self::guess_parser(&log_line[i..j]) { - return Ok(LogDateParser { + return Ok(Self { range: i..j, parser, }); @@ -57,14 +57,14 @@ impl LogDateParser { Err(format!("Could not parse a timestamp in {}", log_line)) } - fn new_with_format(log_line: &str, format_string: &str) -> Result { + fn new_with_format(log_line: &str, format_string: &str) -> Result { // We look for where the timestamp is in logs using a brute force // approach with 1st log line, but capping the max length we scan for for i in 0..log_line.len() { for j in (i..(i + (MAX_LEN * 2)).min(log_line.len() + 1)).rev() { if NaiveDateTime::parse_from_str(&log_line[i..j], format_string).is_ok() { let fmt = Box::new(format_string.to_string()); - return Ok(LogDateParser { + return Ok(Self { range: i..j, parser: Box::new(move |string: &str| { match NaiveDateTime::parse_from_str(string, &fmt) { diff --git a/src/stats/mod.rs b/src/stats/mod.rs index 6a26315..69133ed 100644 --- a/src/stats/mod.rs +++ b/src/stats/mod.rs @@ -29,7 +29,7 @@ impl Stats { /// `precision` is an Option with the number of decimals to display. If /// "None" is used, human units will be used, with an heuristic based on the /// input data for deciding the units and the decimal places. - pub fn new(vec: &[f64], precision: Option) -> Stats { + pub fn new(vec: &[f64], precision: Option) -> Self { let mut max = vec[0]; let mut min = max; let mut temp: f64 = 0.0; @@ -42,7 +42,7 @@ impl Stats { } let var = temp / vec.len() as f64; let std = var.sqrt(); - Stats { + Self { min, max, avg,