Skip to content

Commit

Permalink
Fix every single clippy lint in lune-roblox crate (im tired, boss)
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Apr 22, 2024
1 parent 313dbcf commit 7f7cfb5
Show file tree
Hide file tree
Showing 35 changed files with 264 additions and 127 deletions.
16 changes: 16 additions & 0 deletions crates/lune-roblox/src/datatypes/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ use rbx_dom_weak::types::{Variant as DomValue, VariantType as DomType};

use super::extension::DomValueExt;

/**
Checks if the given name is a valid attribute name.
# Errors
- If the name starts with the prefix "RBX".
- If the name contains any characters other than alphanumeric characters and underscore.
- If the name is longer than 100 characters.
*/
pub fn ensure_valid_attribute_name(name: impl AsRef<str>) -> LuaResult<()> {
let name = name.as_ref();
if name.to_ascii_uppercase().starts_with("RBX") {
Expand All @@ -23,6 +32,13 @@ pub fn ensure_valid_attribute_name(name: impl AsRef<str>) -> LuaResult<()> {
}
}

/**
Checks if the given value is a valid attribute value.
# Errors
- If the value is not a valid attribute type.
*/
pub fn ensure_valid_attribute_value(value: &DomValue) -> LuaResult<()> {
let is_valid = matches!(
value.ty(),
Expand Down
6 changes: 4 additions & 2 deletions crates/lune-roblox/src/datatypes/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ impl<'lua> DomValueToLua<'lua> for LuaValue<'lua> {

// NOTE: Some values are either optional or default and we should handle
// that properly here since the userdata conversion above will always fail
DomValue::OptionalCFrame(None) => Ok(LuaValue::Nil),
DomValue::PhysicalProperties(dom::PhysicalProperties::Default) => Ok(LuaValue::Nil),
DomValue::OptionalCFrame(None)
| DomValue::PhysicalProperties(dom::PhysicalProperties::Default) => {
Ok(LuaValue::Nil)
}

_ => Err(e),
},
Expand Down
1 change: 1 addition & 0 deletions crates/lune-roblox/src/datatypes/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) trait DomValueExt {

impl DomValueExt for DomType {
fn variant_name(&self) -> Option<&'static str> {
#[allow(clippy::enum_glob_use)]
use DomType::*;
Some(match self {
Attributes => "Attributes",
Expand Down
3 changes: 1 addition & 2 deletions crates/lune-roblox/src/datatypes/types/axes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ impl LuaExportsTable<'_> for Axes {
check(&e);
} else {
return Err(LuaError::RuntimeError(format!(
"Expected argument #{} to be an EnumItem, got userdata",
index
"Expected argument #{index} to be an EnumItem, got userdata",
)));
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/lune-roblox/src/datatypes/types/brick_color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::{super::*, Color3};
/**
An implementation of the [BrickColor](https://create.roblox.com/docs/reference/engine/datatypes/BrickColor) Roblox datatype.
This implements all documented properties, methods & constructors of the BrickColor class as of March 2023.
This implements all documented properties, methods & constructors of the `BrickColor` class as of March 2023.
*/
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BrickColor {
Expand Down
44 changes: 32 additions & 12 deletions crates/lune-roblox/src/datatypes/types/cframe.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::items_after_statements)]

use core::fmt;
use std::ops;

Expand All @@ -16,7 +18,7 @@ use super::{super::*, Vector3};
Roblox datatype, backed by [`glam::Mat4`].
This implements all documented properties, methods &
constructors of the CFrame class as of March 2023.
constructors of the `CFrame` class as of March 2023.
*/
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CFrame(pub Mat4);
Expand Down Expand Up @@ -44,6 +46,7 @@ impl CFrame {
impl LuaExportsTable<'_> for CFrame {
const EXPORT_NAME: &'static str = "CFrame";

#[allow(clippy::too_many_lines)]
fn create_exports_table(lua: &Lua) -> LuaResult<LuaTable> {
let cframe_angles = |_, (rx, ry, rz): (f32, f32, f32)| {
Ok(CFrame(Mat4::from_euler(EulerRot::XYZ, rx, ry, rz)))
Expand All @@ -70,8 +73,7 @@ impl LuaExportsTable<'_> for CFrame {
Ok(CFrame(Mat4::from_cols(
rx.0.extend(0.0),
ry.0.extend(0.0),
rz.map(|r| r.0)
.unwrap_or_else(|| rx.0.cross(ry.0).normalize())
rz.map_or_else(|| rx.0.cross(ry.0).normalize(), |r| r.0)
.extend(0.0),
pos.0.extend(1.0),
)))
Expand Down Expand Up @@ -197,6 +199,7 @@ impl LuaUserData for CFrame {
});
}

#[allow(clippy::too_many_lines)]
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
// Methods
methods.add_method("Inverse", |_, this, ()| Ok(this.inverse()));
Expand Down Expand Up @@ -228,43 +231,60 @@ impl LuaUserData for CFrame {
methods.add_method(
"ToWorldSpace",
|_, this, rhs: Variadic<LuaUserDataRef<CFrame>>| {
Ok(Variadic::from_iter(rhs.into_iter().map(|cf| *this * *cf)))
Ok(rhs
.into_iter()
.map(|cf| *this * *cf)
.collect::<Variadic<_>>())
},
);
methods.add_method(
"ToObjectSpace",
|_, this, rhs: Variadic<LuaUserDataRef<CFrame>>| {
let inverse = this.inverse();
Ok(Variadic::from_iter(rhs.into_iter().map(|cf| inverse * *cf)))
Ok(rhs
.into_iter()
.map(|cf| inverse * *cf)
.collect::<Variadic<_>>())
},
);
methods.add_method(
"PointToWorldSpace",
|_, this, rhs: Variadic<LuaUserDataRef<Vector3>>| {
Ok(Variadic::from_iter(rhs.into_iter().map(|v3| *this * *v3)))
Ok(rhs
.into_iter()
.map(|v3| *this * *v3)
.collect::<Variadic<_>>())
},
);
methods.add_method(
"PointToObjectSpace",
|_, this, rhs: Variadic<LuaUserDataRef<Vector3>>| {
let inverse = this.inverse();
Ok(Variadic::from_iter(rhs.into_iter().map(|v3| inverse * *v3)))
Ok(rhs
.into_iter()
.map(|v3| inverse * *v3)
.collect::<Variadic<_>>())
},
);
methods.add_method(
"VectorToWorldSpace",
|_, this, rhs: Variadic<LuaUserDataRef<Vector3>>| {
let result = *this - Vector3(this.position());
Ok(Variadic::from_iter(rhs.into_iter().map(|v3| result * *v3)))
Ok(rhs
.into_iter()
.map(|v3| result * *v3)
.collect::<Variadic<_>>())
},
);
methods.add_method(
"VectorToObjectSpace",
|_, this, rhs: Variadic<LuaUserDataRef<Vector3>>| {
let inverse = this.inverse();
let result = inverse - Vector3(inverse.position());

Ok(Variadic::from_iter(rhs.into_iter().map(|v3| result * *v3)))
Ok(rhs
.into_iter()
.map(|v3| result * *v3)
.collect::<Variadic<_>>())
},
);
#[rustfmt::skip]
Expand Down Expand Up @@ -447,7 +467,7 @@ mod cframe_test {
Vec3::new(1.0, 2.0, 3.0).extend(1.0),
));

assert_eq!(CFrame::from(dom_cframe), cframe)
assert_eq!(CFrame::from(dom_cframe), cframe);
}

#[test]
Expand All @@ -468,6 +488,6 @@ mod cframe_test {
),
);

assert_eq!(DomCFrame::from(cframe), dom_cframe)
assert_eq!(DomCFrame::from(cframe), dom_cframe);
}
}
4 changes: 2 additions & 2 deletions crates/lune-roblox/src/datatypes/types/color3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ impl LuaExportsTable<'_> for Color3 {
b: (b as f32) / 255f32,
}),
_ => Err(LuaError::RuntimeError(format!(
"Hex color string '{}' contains invalid character",
trimmed
"Hex color string '{trimmed}' contains invalid character",
))),
}
};
Expand Down Expand Up @@ -155,6 +154,7 @@ impl LuaUserData for Color3 {
let max = r.max(g).max(b);
let diff = max - min;

#[allow(clippy::float_cmp)]
let hue = (match max {
max if max == min => 0.0,
max if max == r => (g - b) / diff + (if g < b { 6.0 } else { 0.0 }),
Expand Down
10 changes: 5 additions & 5 deletions crates/lune-roblox/src/datatypes/types/color_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::{super::*, Color3, ColorSequenceKeypoint};
/**
An implementation of the [ColorSequence](https://create.roblox.com/docs/reference/engine/datatypes/ColorSequence) Roblox datatype.
This implements all documented properties, methods & constructors of the ColorSequence class as of March 2023.
This implements all documented properties, methods & constructors of the `ColorSequence` class as of March 2023.
*/
#[derive(Debug, Clone, PartialEq)]
pub struct ColorSequence {
Expand Down Expand Up @@ -89,9 +89,9 @@ impl fmt::Display for ColorSequence {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (index, keypoint) in self.keypoints.iter().enumerate() {
if index < self.keypoints.len() - 1 {
write!(f, "{}, ", keypoint)?;
write!(f, "{keypoint}, ")?;
} else {
write!(f, "{}", keypoint)?;
write!(f, "{keypoint}")?;
}
}
Ok(())
Expand All @@ -104,7 +104,7 @@ impl From<DomColorSequence> for ColorSequence {
keypoints: v
.keypoints
.iter()
.cloned()
.copied()
.map(ColorSequenceKeypoint::from)
.collect(),
}
Expand All @@ -117,7 +117,7 @@ impl From<ColorSequence> for DomColorSequence {
keypoints: v
.keypoints
.iter()
.cloned()
.copied()
.map(DomColorSequenceKeypoint::from)
.collect(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{super::*, Color3};
/**
An implementation of the [ColorSequenceKeypoint](https://create.roblox.com/docs/reference/engine/datatypes/ColorSequenceKeypoint) Roblox datatype.
This implements all documented properties, methods & constructors of the ColorSequenceKeypoint class as of March 2023.
This implements all documented properties, methods & constructors of the `ColorSequenceKeypoint` class as of March 2023.
*/
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ColorSequenceKeypoint {
Expand Down
2 changes: 1 addition & 1 deletion crates/lune-roblox/src/datatypes/types/enum_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{super::*, Enum};
/**
An implementation of the [EnumItem](https://create.roblox.com/docs/reference/engine/datatypes/EnumItem) Roblox datatype.
This implements all documented properties, methods & constructors of the EnumItem class as of March 2023.
This implements all documented properties, methods & constructors of the `EnumItem` class as of March 2023.
*/
#[derive(Debug, Clone)]
pub struct EnumItem {
Expand Down
3 changes: 1 addition & 2 deletions crates/lune-roblox/src/datatypes/types/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ impl LuaUserData for Enums {
|_, _, name: String| match Enum::from_name(&name) {
Some(e) => Ok(e),
None => Err(LuaError::RuntimeError(format!(
"The enum '{}' does not exist",
name
"The enum '{name}' does not exist",
))),
},
);
Expand Down
5 changes: 3 additions & 2 deletions crates/lune-roblox/src/datatypes/types/faces.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::struct_excessive_bools)]

use core::fmt;

use mlua::prelude::*;
Expand Down Expand Up @@ -56,8 +58,7 @@ impl LuaExportsTable<'_> for Faces {
check(&e);
} else {
return Err(LuaError::RuntimeError(format!(
"Expected argument #{} to be an EnumItem, got userdata",
index
"Expected argument #{index} to be an EnumItem, got userdata",
)));
}
} else {
Expand Down
12 changes: 6 additions & 6 deletions crates/lune-roblox/src/datatypes/types/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl LuaExportsTable<'_> for Font {
let font_from_name =
|_, (file, weight, style): (String, Option<FontWeight>, Option<FontStyle>)| {
Ok(Font {
family: format!("rbxasset://fonts/families/{}.json", file),
family: format!("rbxasset://fonts/families/{file}.json"),
weight: weight.unwrap_or_default(),
style: style.unwrap_or_default(),
cached_id: None,
Expand All @@ -74,7 +74,7 @@ impl LuaExportsTable<'_> for Font {
let font_from_id =
|_, (id, weight, style): (i32, Option<FontWeight>, Option<FontStyle>)| {
Ok(Font {
family: format!("rbxassetid://{}", id),
family: format!("rbxassetid://{id}"),
weight: weight.unwrap_or_default(),
style: style.unwrap_or_default(),
cached_id: None,
Expand Down Expand Up @@ -208,7 +208,7 @@ pub(crate) enum FontWeight {
}

impl FontWeight {
pub(crate) fn as_u16(&self) -> u16 {
pub(crate) fn as_u16(self) -> u16 {
match self {
Self::Thin => 100,
Self::ExtraLight => 200,
Expand Down Expand Up @@ -317,7 +317,7 @@ impl<'lua> IntoLua<'lua> for FontWeight {
None => Err(LuaError::ToLuaConversionError {
from: "FontWeight",
to: "EnumItem",
message: Some(format!("Found unknown Enum.FontWeight value '{}'", self)),
message: Some(format!("Found unknown Enum.FontWeight value '{self}'")),
}),
}
}
Expand All @@ -330,7 +330,7 @@ pub(crate) enum FontStyle {
}

impl FontStyle {
pub(crate) fn as_u8(&self) -> u8 {
pub(crate) fn as_u8(self) -> u8 {
match self {
Self::Normal => 0,
Self::Italic => 1,
Expand Down Expand Up @@ -411,7 +411,7 @@ impl<'lua> IntoLua<'lua> for FontStyle {
None => Err(LuaError::ToLuaConversionError {
from: "FontStyle",
to: "EnumItem",
message: Some(format!("Found unknown Enum.FontStyle value '{}'", self)),
message: Some(format!("Found unknown Enum.FontStyle value '{self}'")),
}),
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/lune-roblox/src/datatypes/types/number_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::super::*;
/**
An implementation of the [NumberRange](https://create.roblox.com/docs/reference/engine/datatypes/NumberRange) Roblox datatype.
This implements all documented properties, methods & constructors of the NumberRange class as of March 2023.
This implements all documented properties, methods & constructors of the `NumberRange` class as of March 2023.
*/
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NumberRange {
Expand Down
10 changes: 5 additions & 5 deletions crates/lune-roblox/src/datatypes/types/number_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::{super::*, NumberSequenceKeypoint};
/**
An implementation of the [NumberSequence](https://create.roblox.com/docs/reference/engine/datatypes/NumberSequence) Roblox datatype.
This implements all documented properties, methods & constructors of the NumberSequence class as of March 2023.
This implements all documented properties, methods & constructors of the `NumberSequence` class as of March 2023.
*/
#[derive(Debug, Clone, PartialEq)]
pub struct NumberSequence {
Expand Down Expand Up @@ -93,9 +93,9 @@ impl fmt::Display for NumberSequence {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (index, keypoint) in self.keypoints.iter().enumerate() {
if index < self.keypoints.len() - 1 {
write!(f, "{}, ", keypoint)?;
write!(f, "{keypoint}, ")?;
} else {
write!(f, "{}", keypoint)?;
write!(f, "{keypoint}")?;
}
}
Ok(())
Expand All @@ -108,7 +108,7 @@ impl From<DomNumberSequence> for NumberSequence {
keypoints: v
.keypoints
.iter()
.cloned()
.copied()
.map(NumberSequenceKeypoint::from)
.collect(),
}
Expand All @@ -121,7 +121,7 @@ impl From<NumberSequence> for DomNumberSequence {
keypoints: v
.keypoints
.iter()
.cloned()
.copied()
.map(DomNumberSequenceKeypoint::from)
.collect(),
}
Expand Down
Loading

0 comments on commit 7f7cfb5

Please sign in to comment.