summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpml68 <contact@pml68.dev>2025-01-04 22:45:52 +0100
committerpml68 <contact@pml68.dev>2025-01-04 22:45:52 +0100
commiteaaaf2ac7182de1be043b223337e4d8a56e458e1 (patch)
treef2ea1096499d31ead4f5d1f03087ade7bcebb9ed
parentfeat: add custom theme codegen for `Project` (diff)
downloadiced-builder-eaaaf2ac7182de1be043b223337e4d8a56e458e1.tar.gz
refactor: apply clippy suggestions
-rw-r--r--iced_builder/src/main.rs16
-rw-r--r--iced_builder/src/theme.rs6
-rw-r--r--iced_builder/src/types/element_name.rs7
-rw-r--r--iced_builder/src/types/project.rs9
-rwxr-xr-xiced_builder/src/types/rendered_element.rs88
5 files changed, 56 insertions, 70 deletions
diff --git a/iced_builder/src/main.rs b/iced_builder/src/main.rs
index b30afaa..5b95b94 100644
--- a/iced_builder/src/main.rs
+++ b/iced_builder/src/main.rs
@@ -113,7 +113,7 @@ impl App {
}
fn title(&self) -> String {
- let saved_state = if !self.is_dirty { "" } else { " *" };
+ let saved_state = if self.is_dirty { " *" } else { "" };
let project_name = match &self.project.title {
Some(n) => {
@@ -126,7 +126,7 @@ impl App {
}
)
}
- None => "".to_owned(),
+ None => String::new(),
};
format!("iced Builder{project_name}{saved_state}")
@@ -166,11 +166,8 @@ impl App {
Message::HandleNew(name, zones) => {
let ids: Vec<Id> = zones.into_iter().map(|z| z.0).collect();
if !ids.is_empty() {
- let action = Action::new(
- ids,
- &mut self.project.element_tree.clone(),
- None,
- );
+ let eltree_clone = self.project.element_tree.clone();
+ let action = Action::new(&ids, &eltree_clone, None);
let result = name.handle_action(
self.project.element_tree.as_mut(),
action,
@@ -198,9 +195,10 @@ impl App {
Message::HandleMove(element, zones) => {
let ids: Vec<Id> = zones.into_iter().map(|z| z.0).collect();
if !ids.is_empty() {
+ let eltree_clone = self.project.element_tree.clone();
let action = Action::new(
- ids,
- &mut self.project.element_tree.clone(),
+ &ids,
+ &eltree_clone,
Some(element.get_id()),
);
let result = element.handle_action(
diff --git a/iced_builder/src/theme.rs b/iced_builder/src/theme.rs
index e128162..7d18aa9 100644
--- a/iced_builder/src/theme.rs
+++ b/iced_builder/src/theme.rs
@@ -8,7 +8,7 @@ use crate::config::Config;
pub fn theme_index(theme_name: &str, slice: &[iced::Theme]) -> Option<usize> {
slice
.iter()
- .position(|theme| &theme.to_string() == theme_name)
+ .position(|theme| theme.to_string() == theme_name)
}
pub fn theme_from_str(
@@ -43,7 +43,7 @@ pub fn theme_from_str(
if theme_name == config.theme.selected.to_string() {
config.theme.selected.clone()
} else if let Some(index) =
- theme_index(theme_name.into(), &config.theme.all)
+ theme_index(theme_name, &config.theme.all)
{
config.theme.all[index].clone()
} else {
@@ -142,7 +142,7 @@ pub fn theme_to_string(theme: &iced::Theme) -> String {
fn color_to_hex(color: Color) -> String {
use std::fmt::Write;
- let mut hex = String::with_capacity(16);
+ let mut hex = String::with_capacity(12);
let [r, g, b, a] = color.into_rgba8();
diff --git a/iced_builder/src/types/element_name.rs b/iced_builder/src/types/element_name.rs
index 0e8aa65..2687673 100644
--- a/iced_builder/src/types/element_name.rs
+++ b/iced_builder/src/types/element_name.rs
@@ -42,12 +42,11 @@ impl ElementName {
Self::Column => column(None),
};
match action {
- Action::Stop => Ok(None),
- Action::Drop => Ok(None),
+ Action::Stop | Action::Drop => Ok(None),
Action::AddNew => Ok(Some(element)),
Action::PushFront(id) => {
element_tree
- .ok_or("The action was of kind `PushFront`, but no element tree was provided.")?
+ .ok_or("the action was of kind `PushFront`, but no element tree was provided.")?
.find_by_id(id)
.ok_or(Error::NonExistentElement)?
.push_front(&element);
@@ -56,7 +55,7 @@ impl ElementName {
Action::InsertAfter(parent_id, child_id) => {
element_tree
.ok_or(
- "The action was of kind `InsertAfter`, but no element tree was provided.",
+ "the action was of kind `InsertAfter`, but no element tree was provided.",
)?
.find_by_id(parent_id)
.ok_or(Error::NonExistentElement)?
diff --git a/iced_builder/src/types/project.rs b/iced_builder/src/types/project.rs
index 6479b1d..27c576b 100644
--- a/iced_builder/src/types/project.rs
+++ b/iced_builder/src/types/project.rs
@@ -46,10 +46,11 @@ impl Project {
fn theme_code(&mut self, theme: &Theme) -> String {
let theme_name = theme.to_string();
if theme_index(&theme_name, Theme::ALL).is_none() {
- self.theme_cache
+ (*self
+ .theme_cache
.entry(theme_name)
- .or_insert(theme_to_string(theme))
- .to_string()
+ .or_insert(theme_to_string(theme)))
+ .to_string()
} else {
theme_name.replace(" ", "")
}
@@ -115,7 +116,7 @@ impl Project {
if theme_code.contains("Extended") {
theme_imports = "use iced::{{color,theme::{{Palette,palette::{{Extended,Background,Primary,Secondary,Success,Danger,Pair}}}}}};\n";
} else {
- theme_imports = "use iced::{{color,theme::Palette}};\n"
+ theme_imports = "use iced::{{color,theme::Palette}};\n";
}
}
diff --git a/iced_builder/src/types/rendered_element.rs b/iced_builder/src/types/rendered_element.rs
index 177b43d..b001556 100755
--- a/iced_builder/src/types/rendered_element.rs
+++ b/iced_builder/src/types/rendered_element.rs
@@ -41,12 +41,12 @@ impl RenderedElement {
Id::new(self.id.to_string())
}
- pub fn find_by_id(&mut self, id: Id) -> Option<&mut Self> {
- if self.get_id() == id.clone() {
+ pub fn find_by_id(&mut self, id: &Id) -> Option<&mut Self> {
+ if &self.get_id() == id {
Some(self)
} else if let Some(child_elements) = self.child_elements.as_mut() {
for element in child_elements {
- let element = element.find_by_id(id.clone());
+ let element = element.find_by_id(id);
if element.is_some() {
return element;
}
@@ -81,7 +81,7 @@ impl RenderedElement {
}
}
}
- return None;
+ None
}
pub fn is_parent(&self) -> bool {
@@ -109,10 +109,10 @@ impl RenderedElement {
}
}
- pub fn insert_after(&mut self, id: Id, element: &RenderedElement) {
+ pub fn insert_after(&mut self, id: &Id, element: &RenderedElement) {
if let Some(child_elements) = self.child_elements.as_mut() {
if let Some(index) =
- child_elements.iter().position(|x| x.get_id() == id)
+ child_elements.iter().position(|x| &x.get_id() == id)
{
child_elements.insert(index + 1, element.clone());
} else {
@@ -217,7 +217,7 @@ impl RenderedElement {
for element in els {
let (c_imports, children) = element.codegen();
imports = format!("{imports}{c_imports}");
- elements = format!("{elements}{},", children);
+ elements = format!("{elements}{children},");
}
}
@@ -345,13 +345,13 @@ impl<'a> From<RenderedElement> for Element<'a, Message> {
.padding(20)
.into()
}
- ElementName::Row => widget::Row::from_iter(
- child_elements.into_iter().map(Into::into),
+ ElementName::Row => widget::Row::from_vec(
+ child_elements.into_iter().map(Into::into).collect(),
)
.padding(20)
.into(),
- ElementName::Column => widget::Column::from_iter(
- child_elements.into_iter().map(Into::into),
+ ElementName::Column => widget::Column::from_vec(
+ child_elements.into_iter().map(Into::into).collect(),
)
.padding(20)
.into(),
@@ -367,18 +367,18 @@ impl<'a> From<RenderedElement> for Element<'a, Message> {
}
#[derive(Debug, Clone)]
-pub enum Action {
+pub enum Action<'a> {
AddNew,
- PushFront(Id),
- InsertAfter(Id, Id),
+ PushFront(&'a Id),
+ InsertAfter(&'a Id, &'a Id),
Drop,
Stop,
}
-impl Action {
+impl<'a> Action<'a> {
pub fn new(
- ids: Vec<Id>,
- element_tree: &mut Option<RenderedElement>,
+ ids: &'a [Id],
+ element_tree: &'a Option<RenderedElement>,
source_id: Option<Id>,
) -> Self {
let mut action = Self::Stop;
@@ -389,51 +389,39 @@ impl Action {
action = Self::Drop;
}
} else {
- let id: Id = match source_id {
+ let id: &Id = match source_id {
Some(id) if ids.contains(&id) => {
let element_id =
- ids[ids.iter().position(|x| *x == id).unwrap()].clone();
- if ids.len() > 2 && ids[ids.clone().len() - 1] == element_id
- {
+ &ids[ids.iter().position(|x| *x == id).unwrap()];
+ if ids.len() > 2 && &ids[ids.len() - 1] == element_id {
return Self::Stop;
}
element_id
}
- _ => ids.last().cloned().unwrap(),
+ _ => ids.last().unwrap(),
};
- let element = element_tree
- .as_mut()
- .unwrap()
- .find_by_id(id.clone())
- .unwrap();
+ let mut element_tree = element_tree.clone().unwrap();
+ let element = element_tree.find_by_id(id).unwrap();
// Element is a parent and isn't a non-empty container
- match (element.is_empty()
- || !(element.name == ElementName::Container))
+ if (element.is_empty() || !(element.name == ElementName::Container))
&& element.is_parent()
{
- true => {
- action = Self::PushFront(id);
- }
- false if ids.len() > 2 => {
- let parent = element_tree
- .as_mut()
- .unwrap()
- .find_by_id(ids[&ids.len() - 2].clone())
- .unwrap();
-
- if parent.name == ElementName::Container
- && parent.child_elements != Some(vec![])
- {
- action = Self::Stop;
- } else {
- action = Self::InsertAfter(
- ids[&ids.len() - 2].clone(),
- ids[&ids.len() - 1].clone(),
- );
- }
+ action = Self::PushFront(id);
+ } else if ids.len() > 2 {
+ let parent =
+ element_tree.find_by_id(&ids[ids.len() - 2]).unwrap();
+
+ if parent.name == ElementName::Container
+ && parent.child_elements != Some(vec![])
+ {
+ action = Self::Stop;
+ } else {
+ action = Self::InsertAfter(
+ &ids[ids.len() - 2],
+ &ids[ids.len() - 1],
+ );
}
- _ => {}
}
}
action