Browse Source

lint: use Self when suitable

Changes done by `cargo clippy --fix -- -D clippy::use_self`
log-scale
JuanLeon Lahoz 3 years ago
parent
commit
26f6ae6348
  1. 14
      src/format/mod.rs
  2. 12
      src/plot/histogram.rs
  3. 8
      src/plot/matchbar.rs
  4. 12
      src/plot/splittimehist.rs
  5. 4
      src/plot/terms.rs
  6. 8
      src/plot/timehist.rs
  7. 8
      src/plot/xy.rs
  8. 10
      src/read/dateparser.rs
  9. 4
      src/stats/mod.rs

14
src/format/mod.rs

@ -20,8 +20,8 @@ pub struct F64Formatter {
impl F64Formatter { impl F64Formatter {
/// Initializes a new `HumanF64Formatter` with default values. /// Initializes a new `HumanF64Formatter` with default values.
pub fn new(decimals: usize) -> F64Formatter { pub fn new(decimals: usize) -> Self {
F64Formatter { Self {
decimals, decimals,
divisor: 0, divisor: 0,
suffix: "".to_owned(), suffix: "".to_owned(),
@ -30,14 +30,14 @@ impl F64Formatter {
/// Initializes a new `HumanF64Formatter` for formatting numbers in the /// Initializes a new `HumanF64Formatter` for formatting numbers in the
/// provided range. /// provided range.
pub fn new_with_range(range: Range<f64>) -> F64Formatter { pub fn new_with_range(range: Range<f64>) -> Self {
// Range // Range
let mut decimals = 3; let mut decimals = 3;
let mut divisor = 0_u8; let mut divisor = 0_u8;
let mut suffix = UNITS[0].to_owned(); let mut suffix = UNITS[0].to_owned();
let difference = range.end - range.start; let difference = range.end - range.start;
if difference == 0.0 { if difference == 0.0 {
return F64Formatter { return Self {
decimals, decimals,
divisor, divisor,
suffix, suffix,
@ -51,7 +51,7 @@ impl F64Formatter {
divisor = ((log - 1) / 3).min(5) as u8; divisor = ((log - 1) / 3).min(5) as u8;
} }
suffix = UNITS[divisor as usize].to_owned(); suffix = UNITS[divisor as usize].to_owned();
F64Formatter { Self {
decimals, decimals,
divisor, divisor,
suffix, suffix,
@ -74,8 +74,8 @@ pub struct HorizontalScale {
} }
impl HorizontalScale { impl HorizontalScale {
pub fn new(scale: usize) -> HorizontalScale { pub fn new(scale: usize) -> Self {
HorizontalScale { Self {
scale: 1.max(scale), scale: 1.max(scale),
} }
} }

12
src/plot/histogram.rs

@ -14,8 +14,8 @@ struct Bucket {
} }
impl Bucket { impl Bucket {
fn new(range: Range<f64>) -> Bucket { fn new(range: Range<f64>) -> Self {
Bucket { range, count: 0 } Self { range, count: 0 }
} }
fn inc(&mut self) { fn inc(&mut self) {
@ -63,12 +63,12 @@ impl Histogram {
/// `precision` is an Option with the number of decimals to display. If /// `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 /// "None" is used, human units will be used, with an heuristic based on the
/// input data for deciding the units and the decimal places. /// 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()); options.intervals = options.intervals.min(vec.len());
let stats = Stats::new(vec, options.precision); let stats = Stats::new(vec, options.precision);
let size = options.intervals.min(vec.len()); let size = options.intervals.min(vec.len());
let step = (stats.max - stats.min) / size as f64; 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.load(vec);
histogram histogram
} }
@ -79,14 +79,14 @@ impl Histogram {
/// Parameters are similar to those on the `new` method, but a parameter /// 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 /// named `stats` is needed to decide how future data (to be injected with
/// the load method) will be accommodated. /// 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::<Bucket>::with_capacity(options.intervals); let mut vec = Vec::<Bucket>::with_capacity(options.intervals);
let mut lower = stats.min; let mut lower = stats.min;
for _ in 0..options.intervals { for _ in 0..options.intervals {
vec.push(Bucket::new(lower..lower + step)); vec.push(Bucket::new(lower..lower + step));
lower += step; lower += step;
} }
Histogram { Self {
vec, vec,
max: stats.min + (step * options.intervals as f64), max: stats.min + (step * options.intervals as f64),
step, step,

8
src/plot/matchbar.rs

@ -13,8 +13,8 @@ pub struct MatchBarRow {
} }
impl MatchBarRow { impl MatchBarRow {
pub fn new(string: &str) -> MatchBarRow { pub fn new(string: &str) -> Self {
MatchBarRow { Self {
label: string.to_string(), label: string.to_string(),
count: 0, count: 0,
} }
@ -38,14 +38,14 @@ pub struct MatchBar {
impl MatchBar { impl MatchBar {
/// Creates a Histogram from a vector of `MatchBarRow` elements. /// Creates a Histogram from a vector of `MatchBarRow` elements.
pub fn new(vec: Vec<MatchBarRow>) -> MatchBar { pub fn new(vec: Vec<MatchBarRow>) -> Self {
let mut top_lenght: usize = 0; let mut top_lenght: usize = 0;
let mut top_values: usize = 0; let mut top_values: usize = 0;
for row in vec.iter() { for row in vec.iter() {
top_lenght = top_lenght.max(row.label.len()); top_lenght = top_lenght.max(row.label.len());
top_values = top_values.max(row.count); top_values = top_values.max(row.count);
} }
MatchBar { Self {
vec, vec,
top_values, top_values,
top_lenght, top_lenght,

12
src/plot/splittimehist.rs

@ -15,8 +15,8 @@ struct TimeBucket {
} }
impl TimeBucket { impl TimeBucket {
fn new(start: DateTime<FixedOffset>, counts: usize) -> TimeBucket { fn new(start: DateTime<FixedOffset>, counts: usize) -> Self {
TimeBucket { Self {
start, start,
count: vec![0; counts], count: vec![0; counts],
} }
@ -52,11 +52,7 @@ impl SplitTimeHistogram {
/// `size` is the number of time slots in the histogram. Parameter 'ts' is /// `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 /// a slice of tuples of `DateTime` (the timestamp of a term occurrence) and
/// the index of the term in the `strings` parameter. /// the index of the term in the `strings` parameter.
pub fn new( pub fn new(size: usize, strings: Vec<String>, ts: &[(DateTime<FixedOffset>, usize)]) -> Self {
size: usize,
strings: Vec<String>,
ts: &[(DateTime<FixedOffset>, usize)],
) -> SplitTimeHistogram {
let mut vec = Vec::<TimeBucket>::with_capacity(size); let mut vec = Vec::<TimeBucket>::with_capacity(size);
let min = ts.iter().min().unwrap().0; let min = ts.iter().min().unwrap().0;
let max = ts.iter().max().unwrap().0; let max = ts.iter().max().unwrap().0;
@ -65,7 +61,7 @@ impl SplitTimeHistogram {
for i in 0..size { for i in 0..size {
vec.push(TimeBucket::new(min + (inc * i as i32), strings.len())); vec.push(TimeBucket::new(min + (inc * i as i32), strings.len()));
} }
let mut sth = SplitTimeHistogram { let mut sth = Self {
vec, vec,
strings, strings,
min, min,

4
src/plot/terms.rs

@ -20,8 +20,8 @@ impl CommonTerms {
/// Create and empty `CommonTerms`. /// Create and empty `CommonTerms`.
/// ///
/// `lines` is the number of lines to be displayed. /// `lines` is the number of lines to be displayed.
pub fn new(lines: usize) -> CommonTerms { pub fn new(lines: usize) -> Self {
CommonTerms { Self {
terms: HashMap::new(), terms: HashMap::new(),
lines, lines,
} }

8
src/plot/timehist.rs

@ -13,8 +13,8 @@ struct TimeBucket {
} }
impl TimeBucket { impl TimeBucket {
fn new(start: DateTime<FixedOffset>) -> TimeBucket { fn new(start: DateTime<FixedOffset>) -> Self {
TimeBucket { start, count: 0 } Self { start, count: 0 }
} }
fn inc(&mut self) { fn inc(&mut self) {
@ -38,7 +38,7 @@ impl TimeHistogram {
/// Creates a Histogram from a vector of `DateTime` elements. /// Creates a Histogram from a vector of `DateTime` elements.
/// ///
/// `size` is the number of histogram buckets to display. /// `size` is the number of histogram buckets to display.
pub fn new(size: usize, ts: &[DateTime<FixedOffset>]) -> TimeHistogram { pub fn new(size: usize, ts: &[DateTime<FixedOffset>]) -> Self {
let mut vec = Vec::<TimeBucket>::with_capacity(size); let mut vec = Vec::<TimeBucket>::with_capacity(size);
let min = *ts.iter().min().unwrap(); let min = *ts.iter().min().unwrap();
let max = *ts.iter().max().unwrap(); let max = *ts.iter().max().unwrap();
@ -47,7 +47,7 @@ impl TimeHistogram {
for i in 0..size { for i in 0..size {
vec.push(TimeBucket::new(min + (inc * i as i32))); vec.push(TimeBucket::new(min + (inc * i as i32)));
} }
let mut timehist = TimeHistogram { let mut timehist = Self {
vec, vec,
min, min,
max, max,

8
src/plot/xy.rs

@ -31,8 +31,8 @@ impl XyPlot {
/// `precision` is an Option with the number of decimals to display. If /// `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 /// "None" is used, human units will be used, with an heuristic based on the
/// input data for deciding the units and the decimal places. /// input data for deciding the units and the decimal places.
pub fn new(vec: &[f64], width: usize, height: usize, precision: Option<usize>) -> XyPlot { pub fn new(vec: &[f64], width: usize, height: usize, precision: Option<usize>) -> Self {
let mut plot = XyPlot::new_with_stats(width, height, Stats::new(vec, precision), precision); let mut plot = Self::new_with_stats(width, height, Stats::new(vec, precision), precision);
plot.load(vec); plot.load(vec);
plot plot
} }
@ -47,8 +47,8 @@ impl XyPlot {
height: usize, height: usize,
stats: Stats, stats: Stats,
precision: Option<usize>, precision: Option<usize>,
) -> XyPlot { ) -> Self {
XyPlot { Self {
x_axis: Vec::with_capacity(width), x_axis: Vec::with_capacity(width),
y_axis: Vec::with_capacity(height), y_axis: Vec::with_capacity(height),
width, width,

10
src/read/dateparser.rs

@ -28,14 +28,14 @@ pub struct LogDateParser {
} }
impl LogDateParser { impl LogDateParser {
pub fn new(log_line: &str, format_string: &Option<String>) -> Result<LogDateParser, String> { pub fn new(log_line: &str, format_string: &Option<String>) -> Result<Self, String> {
match format_string { match format_string {
Some(ts_format) => Self::new_with_format(log_line, ts_format), Some(ts_format) => Self::new_with_format(log_line, ts_format),
None => Self::new_with_guess(log_line), None => Self::new_with_guess(log_line),
} }
} }
fn new_with_guess(log_line: &str) -> Result<LogDateParser, String> { fn new_with_guess(log_line: &str) -> Result<Self, String> {
// All the guess work assume that datetimes start with a digit, and that // 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 // 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 // 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() { if c.is_ascii_digit() {
for j in (i..(i + MAX_LEN).min(log_line.len() + 1)).rev() { for j in (i..(i + MAX_LEN).min(log_line.len() + 1)).rev() {
if let Some(parser) = Self::guess_parser(&log_line[i..j]) { if let Some(parser) = Self::guess_parser(&log_line[i..j]) {
return Ok(LogDateParser { return Ok(Self {
range: i..j, range: i..j,
parser, parser,
}); });
@ -57,14 +57,14 @@ impl LogDateParser {
Err(format!("Could not parse a timestamp in {}", log_line)) Err(format!("Could not parse a timestamp in {}", log_line))
} }
fn new_with_format(log_line: &str, format_string: &str) -> Result<LogDateParser, String> { fn new_with_format(log_line: &str, format_string: &str) -> Result<Self, String> {
// We look for where the timestamp is in logs using a brute force // 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 // approach with 1st log line, but capping the max length we scan for
for i in 0..log_line.len() { for i in 0..log_line.len() {
for j in (i..(i + (MAX_LEN * 2)).min(log_line.len() + 1)).rev() { 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() { if NaiveDateTime::parse_from_str(&log_line[i..j], format_string).is_ok() {
let fmt = Box::new(format_string.to_string()); let fmt = Box::new(format_string.to_string());
return Ok(LogDateParser { return Ok(Self {
range: i..j, range: i..j,
parser: Box::new(move |string: &str| { parser: Box::new(move |string: &str| {
match NaiveDateTime::parse_from_str(string, &fmt) { match NaiveDateTime::parse_from_str(string, &fmt) {

4
src/stats/mod.rs

@ -29,7 +29,7 @@ impl Stats {
/// `precision` is an Option with the number of decimals to display. If /// `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 /// "None" is used, human units will be used, with an heuristic based on the
/// input data for deciding the units and the decimal places. /// input data for deciding the units and the decimal places.
pub fn new(vec: &[f64], precision: Option<usize>) -> Stats { pub fn new(vec: &[f64], precision: Option<usize>) -> Self {
let mut max = vec[0]; let mut max = vec[0];
let mut min = max; let mut min = max;
let mut temp: f64 = 0.0; let mut temp: f64 = 0.0;
@ -42,7 +42,7 @@ impl Stats {
} }
let var = temp / vec.len() as f64; let var = temp / vec.len() as f64;
let std = var.sqrt(); let std = var.sqrt();
Stats { Self {
min, min,
max, max,
avg, avg,

Loading…
Cancel
Save