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 {
/// 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<f64>) -> F64Formatter {
pub fn new_with_range(range: Range<f64>) -> 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),
}
}

12
src/plot/histogram.rs

@ -14,8 +14,8 @@ struct Bucket {
}
impl Bucket {
fn new(range: Range<f64>) -> Bucket {
Bucket { range, count: 0 }
fn new(range: Range<f64>) -> 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::<Bucket>::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,

8
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<MatchBarRow>) -> MatchBar {
pub fn new(vec: Vec<MatchBarRow>) -> 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,

12
src/plot/splittimehist.rs

@ -15,8 +15,8 @@ struct TimeBucket {
}
impl TimeBucket {
fn new(start: DateTime<FixedOffset>, counts: usize) -> TimeBucket {
TimeBucket {
fn new(start: DateTime<FixedOffset>, 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<String>,
ts: &[(DateTime<FixedOffset>, usize)],
) -> SplitTimeHistogram {
pub fn new(size: usize, strings: Vec<String>, ts: &[(DateTime<FixedOffset>, usize)]) -> Self {
let mut vec = Vec::<TimeBucket>::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,

4
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,
}

8
src/plot/timehist.rs

@ -13,8 +13,8 @@ struct TimeBucket {
}
impl TimeBucket {
fn new(start: DateTime<FixedOffset>) -> TimeBucket {
TimeBucket { start, count: 0 }
fn new(start: DateTime<FixedOffset>) -> 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<FixedOffset>]) -> TimeHistogram {
pub fn new(size: usize, ts: &[DateTime<FixedOffset>]) -> Self {
let mut vec = Vec::<TimeBucket>::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,

8
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<usize>) -> 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<usize>) -> 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<usize>,
) -> XyPlot {
XyPlot {
) -> Self {
Self {
x_axis: Vec::with_capacity(width),
y_axis: Vec::with_capacity(height),
width,

10
src/read/dateparser.rs

@ -28,14 +28,14 @@ pub struct 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 {
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<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
// 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<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
// 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) {

4
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<usize>) -> Stats {
pub fn new(vec: &[f64], precision: Option<usize>) -> 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,

Loading…
Cancel
Save