Clean up clippy warnings

This commit is contained in:
Austen Adler 2021-08-21 15:51:10 -04:00
parent 537fec8a4d
commit 489adc1af1
11 changed files with 27 additions and 40 deletions

View File

@ -12,7 +12,7 @@ impl Rgb {
pub fn to_float_tuple(self) -> (f32, f32, f32) {
(f32::from(self.0), f32::from(self.1), f32::from(self.2))
}
pub fn to_hex_str(&self) -> String {
pub fn to_hex_str(self) -> String {
format!("#{:02x}{:02x}{:02x}", self.0, self.1, self.2)
}
// pub fn to_gamma_corrected_tuple(&self) -> (u8, u8, u8) {
@ -108,14 +108,14 @@ pub fn build_ramp(from_color: Rgb, to_color: Rgb, length: usize) -> Vec<Rgb> {
}
pub fn min_with_factor(at_least: u16, factor: u16) -> Result<u16, ()> {
Ok(at_least
at_least
.checked_sub(1)
.ok_or(())?
.div_euclid(factor)
.checked_add(1)
.ok_or(())?
.checked_mul(factor)
.ok_or(())?)
.ok_or(())
}
#[cfg(test)]

View File

@ -10,7 +10,7 @@
// Intentional code
clippy::map_err_ignore,
// "as f32" used frequently in this project
clippy::cast_precision_loss,
clippy::cast_precision_loss, clippy::cast_possible_truncation, clippy::cast_sign_loss,
// This is fine
clippy::implicit_return,
// Missing docs is fine

View File

@ -120,7 +120,7 @@ impl Parameters {
_ => None,
}
}
pub fn get_name(&self) -> &str {
pub const fn get_name(&self) -> &str {
match self {
Self::Collide(_) => "Collide",
Self::Fade(_) => "Fade",

View File

@ -57,9 +57,9 @@ impl Collide {
pub fn new(params: &CollideParams) -> Self {
Self {
num_lights: 0,
left_color: params.left_color.clone(),
right_color: params.right_color.clone(),
conjoined_color: params.conjoined_color.clone(),
left_color: params.left_color,
right_color: params.right_color,
conjoined_color: params.conjoined_color,
step: 0,
step_max: 0,
conjoined_bounds: (0, 0),

View File

@ -39,7 +39,7 @@ impl Default for Fade {
impl Fade {
pub fn new(params: &FadeParams) -> Self {
Self {
color: params.color.clone(),
color: params.color,
step: 0,
direction: true,
num_lights: 1,

View File

@ -39,7 +39,7 @@ impl Default for MovingPixel {
impl MovingPixel {
pub fn new(params: &MovingPixelParams) -> Self {
Self {
color: params.color.clone(),
color: params.color,
step: 0,
// TODO: Better initialization
num_lights: 1,

View File

@ -79,21 +79,7 @@ impl Pattern for MovingRainbow {
.saturating_mul(u16::try_from(RAINBOW.len()).or(Err(()))?);
// The length of the buffer
// Always a factor of length_factor
let buf_length = color::min_with_factor(num_lights, length_factor.into())?;
println!(
"Got buf length: {} with #lights {} and length factor {} ({})",
buf_length,
num_lights,
length_factor,
(self.width + self.skip) as usize * RAINBOW.len()
);
// num_lights
// .checked_sub(1)
// .ok_or(())?
// .div_euclid(length_factor)
// .checked_add(1)
// .ok_or(())?
// .saturating_mul(length_factor);
let buf_length = color::min_with_factor(num_lights, length_factor)?;
self.lights_buf = RAINBOW
.iter()

View File

@ -63,7 +63,7 @@ impl Orb {
pub fn new(params: &OrbParams) -> Self {
Self {
lights_buf: VecDeque::new(),
color: params.color.clone(),
color: params.color,
center_width: params.center_width,
backoff_width: params.backoff_width,
total_width: params

View File

@ -37,7 +37,7 @@ impl Default for Solid {
impl Solid {
pub fn new(params: &SolidParams) -> Self {
Self {
color: params.color.clone(),
color: params.color,
has_run: false,
lights_buf: VecDeque::new(),
}

View File

@ -1,6 +1,5 @@
use crate::color::Rgb;
use crate::pattern::{FormRender, Parameters};
use askama;
#[derive(askama::Template, Clone, Debug, Default)]
#[template(path = "app.html", escape = "none")]
@ -9,7 +8,7 @@ pub struct AppTemplate {
}
impl live_view::Template for AppTemplate {
fn render(&self) -> Result<String, Box<dyn std::error::Error>> {
Ok(<Self as askama::Template>::render(&self)?)
Ok(<Self as askama::Template>::render(self)?)
}
}

View File

@ -1,4 +1,3 @@
use crate::color::Rgb;
use crate::errors;
use crate::pattern;
use crate::strip;
@ -78,23 +77,20 @@ async fn initial_load(_req: HttpRequest) -> impl Responder {
..ControlTemplate::default()
}
.render()
.unwrap(),
..AppTemplate::default()
.map_err(|e| {
println!("Internal error e found: {:?}", e);
ErrorInternalServerError(e)
})?,
// ..AppTemplate::default()
};
state
.render()
.map(|b| HttpResponse::Ok().body(b))
.map_err(|e| ErrorInternalServerError(e))
.map_err(ErrorInternalServerError)
}
async fn start_socket(req: HttpRequest, stream: web::Payload) -> impl Responder {
let mut live_view: LiveView<ControlTemplate> = LiveView::default();
live_view.on_click("inc", |_event, state| {
state.color = Rgb(1_u8, 2_u8, 255_u8);
state.count += 1;
println!("Rendering color: {:?}", state.color);
Some(state.render().unwrap())
});
live_view.on_input("change-template", |event, state| {
println!("Got change template ewvent: {:?}", event);
@ -103,7 +99,13 @@ async fn start_socket(req: HttpRequest, stream: web::Payload) -> impl Responder
println!("Got params!: {:?}", params);
// state.selected_template = template_name.to_string();
state.parameters = params;
Some(state.render().unwrap())
state
.render()
.map_err(|e| {
format!("Error rendering state: {:?}", e);
e
})
.ok()
});
live_view.on_submit("form", |event, _state| {