Skip to content

Commit

Permalink
clippy: even more
Browse files Browse the repository at this point in the history
  • Loading branch information
geofmureithi committed Dec 8, 2023
1 parent 0fc2348 commit 9efab6d
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 143 deletions.
2 changes: 1 addition & 1 deletion crates/hirola-core/src/templating/suspense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<Res: Default + 'static, N: GenericNode> Render<N> for Suspense<Res, N> {
}

let state = State::new(parent.clone());
let binding = state.clone();
let binding = Rc::<_>::clone(&state);
let mut binding = binding.borrow_mut();
// Apply loading
binding.apply(template(Res::default()))?;
Expand Down
27 changes: 14 additions & 13 deletions crates/hirola-dom/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ pub struct App<S: 'static> {
/// // ... fields and methods for your application state ...
/// }
///
/// fn main() {
/// let initial_state = AppState { /* ... */ };
/// let app = App::new(initial_state);
/// }
/// let initial_state = AppState { /* ... */ };
/// let app = App::new(initial_state);
/// ```
impl<S: Clone + 'static> App<S> {
/// Creates a new instance of the App with the given initial state.
Expand Down Expand Up @@ -78,7 +76,7 @@ impl<S: Clone + 'static> App<S> {
/// use hirola::prelude::*;
/// use hirola::dom::app::App;
/// use hirola::dom::Dom;
///
///
/// #[derive(Clone)]
/// struct AppState {
/// // ... fields and methods for your application state ...
Expand All @@ -97,7 +95,10 @@ impl<S: Clone + 'static> App<S> {
/// app.route("/about", about_page);
/// ```
pub fn route(&mut self, path: impl AsRef<str>, page: fn(&Self) -> Dom) {
self.router.handler.insert(path.as_ref().to_string(), page).unwrap();
self.router
.handler
.insert(path.as_ref().to_string(), page)
.unwrap();
}

/// Set the not-found page for the application.
Expand All @@ -113,7 +114,7 @@ impl<S: Clone + 'static> App<S> {
/// use hirola::prelude::*;
/// use hirola::dom::app::App;
/// use hirola::dom::Dom;
///
///
/// #[derive(Clone)]
/// struct AppState {
/// // ... fields and methods for your application state ...
Expand Down Expand Up @@ -147,7 +148,7 @@ impl<S: Clone + 'static> App<S> {
/// # Example
///
/// ```no_run
/// fn main() {
/// // fn main() {
/// use hirola::prelude::*;
/// use hirola::dom::app::App;
/// #[derive(Clone)]
Expand All @@ -161,7 +162,7 @@ impl<S: Clone + 'static> App<S> {
///
/// // Mount the app on the web page body and start rendering
/// app.mount();
/// }
/// //}
/// ```
pub fn mount(&self) {
let window = web_sys::window().unwrap();
Expand Down Expand Up @@ -191,7 +192,7 @@ impl<S: Clone + 'static> App<S> {
/// # Example
///
/// ```no_run
/// fn main() {
///// fn main() {
/// use hirola::prelude::*;
/// use hirola::dom::app::App;
/// #[derive(Clone)]
Expand All @@ -211,7 +212,7 @@ impl<S: Clone + 'static> App<S> {
///
/// // Mount the app on the specified parent node and start rendering
/// app.mount_to(&parent_node);
/// }
/// //}
/// ```
pub fn mount_to(&self, parent: &web_sys::Node) {
let router = self.router.clone();
Expand Down Expand Up @@ -242,7 +243,7 @@ impl<S: Clone + 'static> App<S> {
/// # Example
///
/// ```no_run
/// fn main() {
///// fn main() {
/// use hirola::prelude::*;
/// use hirola::dom::app::App;
/// use hirola::dom::Dom;
Expand Down Expand Up @@ -281,7 +282,7 @@ impl<S: Clone + 'static> App<S> {
/// </main>
/// }
/// });
/// }
/// //}
/// ```
pub fn mount_with(&self, parent: &web_sys::Node, cb: impl Fn(&Self) -> Dom) {
let res = cb(self);
Expand Down
8 changes: 7 additions & 1 deletion crates/hirola-dom/src/app/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl<S: Clone + 'static> Router<S> {
> 0
{
log::debug!("hash detected");
return ;
return;
}
current.set(path_name.to_string());
log::debug!("pop handle : {path_name}");
Expand Down Expand Up @@ -464,3 +464,9 @@ impl<S: Clone + 'static> Router<S> {
self.handler.clone()
}
}

impl<S: Clone + 'static> Default for Router<S> {
fn default() -> Self {
Self::new()
}
}
4 changes: 3 additions & 1 deletion crates/hirola-dom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub enum DomSideEffect {
Mounted(CancelableFutureHandle),
}

pub type EventHandlers = Rc<RefCell<Vec<Closure<dyn Fn(Event)>>>>;

/// Rendering backend for the DOM.
///
/// The `DomNode` struct represents a node in the Document Object Model (DOM) and serves as the
Expand All @@ -35,7 +37,7 @@ pub enum DomSideEffect {
pub struct Dom {
pub node: Node,
pub side_effects: Rc<RefCell<Vec<DomSideEffect>>>,
event_handlers: Rc<RefCell<Vec<Closure<dyn Fn(Event)>>>>,
event_handlers: EventHandlers,
children: RefCell<Vec<Dom>>,
}

Expand Down
2 changes: 1 addition & 1 deletion crates/hirola-dom/tests/integration/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn test_router_insert_and_render() {
let app = App::new(AppState {});
let body = &body();
let home_dom = (router.handler().at("/").unwrap().value)(&app);
let rendered = router.clone().render(&app, &body);
let rendered = router.clone().render(&app, body);
assert_eq!(rendered.inner_html(), home_dom.inner_html());
router.push("/about");

Expand Down
32 changes: 18 additions & 14 deletions crates/hirola-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn node_to_tokens(node: Node) -> TokenStream {

tokens.extend(quote! {
{
let mut template = ::hirola::prelude::GenericNode::element(#name);
let template = ::hirola::prelude::GenericNode::element(#name);
#children_tokens
#(#attributes)*
template
Expand Down Expand Up @@ -128,7 +128,7 @@ fn attribute_to_tokens(attribute: &NodeAttribute) -> TokenStream {
if name.starts_with("on:") {
let name = name.replace("on:", "");
quote! {
::hirola::prelude::EventListener::event(&mut template, #name, #value);
::hirola::prelude::EventListener::event(&template, #name, #value);
}
} else if name.starts_with("use:") {
let effect = if value.is_some() {
Expand Down Expand Up @@ -190,7 +190,7 @@ fn attribute_to_tokens(attribute: &NodeAttribute) -> TokenStream {
let attribute_name = convert_name(&name);
quote! {
::hirola::prelude::GenericNode::set_attribute(
&mut template,
&template,
#attribute_name,
&::std::format!("{}", #value),
);
Expand All @@ -214,20 +214,22 @@ fn children_to_tokens(children: Vec<Node>) -> TokenStream {
// Its a component
Node::Element(_) if name[0..1].to_lowercase() != name[0..1] => {
append_children.extend(quote! {
::hirola::prelude::GenericNode::append_render(&mut template, #node );
#[allow(unused_braces)]
::hirola::prelude::GenericNode::append_render(&template, #node );
});
}
_ => {
append_children.extend(quote! {
::hirola::prelude::GenericNode::append_child(&mut template, &#node );
#[allow(unused_braces)]
::hirola::prelude::GenericNode::append_child(&template, &#node );
});
}
}
}
Node::Text(text) => {
append_children.extend(quote! {
::hirola::prelude::GenericNode::append_child(
&mut template,
&template,
#[allow(unused_braces)]
&::hirola::prelude::GenericNode::text_node(#text),
);
Expand All @@ -237,7 +239,7 @@ fn children_to_tokens(children: Vec<Node>) -> TokenStream {
let s = comment.value.clone();
append_children.extend(quote! {
::hirola::prelude::GenericNode::append_child(
&mut template,
&template,
#[allow(unused_braces)]
&::hirola::prelude::GenericNode::comment(#s),
);
Expand Down Expand Up @@ -305,7 +307,7 @@ fn children_to_tokens(children: Vec<Node>) -> TokenStream {
append_children.extend(quote! {
for #pat in #expr {
::hirola::prelude::GenericNode::append_child(
&mut template,
&template,
#[allow(unused_braces)]
&#body,
);
Expand All @@ -325,6 +327,7 @@ fn children_to_tokens(children: Vec<Node>) -> TokenStream {
match ty.as_ref() {
&Type::Infer(_) => {
append_children.extend(quote! {
#[allow(unused_braces)]
let switch = {
let switch = ::hirola::prelude::Switch {
signal: #expr,
Expand All @@ -339,7 +342,7 @@ fn children_to_tokens(children: Vec<Node>) -> TokenStream {
switch
};
::hirola::prelude::GenericNode::append_render(
&mut template,
&template,
switch
);
});
Expand All @@ -349,6 +352,7 @@ fn children_to_tokens(children: Vec<Node>) -> TokenStream {
if path.path.is_ident(&ident) {
append_children.extend(quote! {
let switch = {
#[allow(unused_braces)]
let switch = ::hirola::prelude::Switch {
signal: #expr,
renderer: move |res| {
Expand All @@ -362,7 +366,7 @@ fn children_to_tokens(children: Vec<Node>) -> TokenStream {
switch
};
::hirola::prelude::GenericNode::append_render(
&mut template,
&template,
switch
);
});
Expand All @@ -386,7 +390,7 @@ fn children_to_tokens(children: Vec<Node>) -> TokenStream {
} else {
append_children.extend(quote! {
::hirola::prelude::GenericNode::append_child(
&mut template,
&template,
#[allow(unused_braces)]
&#block,
);
Expand All @@ -399,7 +403,7 @@ fn children_to_tokens(children: Vec<Node>) -> TokenStream {
let fut = fut.base;
append_children.extend(quote! {
let suspense = {

#[allow(unused_braces)]
let suspense = ::hirola::prelude::Suspense {
future: Box::pin(#fut),
template: Box::new(move |res| {
Expand All @@ -411,7 +415,7 @@ fn children_to_tokens(children: Vec<Node>) -> TokenStream {
suspense
};
::hirola::prelude::GenericNode::append_render(
&mut template,
&template,
suspense
);

Expand All @@ -420,7 +424,7 @@ fn children_to_tokens(children: Vec<Node>) -> TokenStream {
_ => {
append_children.extend(quote! {
::hirola::prelude::GenericNode::append_child(
&mut template,
&template,
#[allow(unused_braces)]
&#block,
);
Expand Down
2 changes: 1 addition & 1 deletion crates/hirola-ssr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ mod tests {
let node = html! {
<ul>
{
count.signal_vec().render_map(move |item| {
count.signal_vec().map_render(move |item| {
html! {
<li>{item.to_string()}</li>
}
Expand Down
8 changes: 4 additions & 4 deletions examples/docs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ fn with_layout(seo: Seo) -> SsrNode {
<link rel="canonical" href="/" />
<script src="https://cdn.tailwindcss.com"></script>
<style>
{r##"
{r#"
@import url("https://fonts.googleapis.com/css2?family=Grape+Nuts&display=swap");
"##}
"#}
</style>
</head>
<body>
Expand Down Expand Up @@ -196,7 +196,7 @@ fn main() {
Ok(path) => {
let (content, seo) = markdown_page(&path);
let mut layout = "<!DOCTYPE html>".to_string();
layout.push_str(&render_to_string(with_layout(seo)).unwrap().chars());
layout.push_str(&render_to_string(with_layout(seo)).unwrap());
let html_path = path
.to_string_lossy()
.replace("src/pages", "dist")
Expand Down Expand Up @@ -226,7 +226,7 @@ fn markdown_page(path: &PathBuf) -> (String, Seo) {
options.extension.front_matter_delimiter = Some("---".to_owned());
plugins.render.codefence_syntax_highlighter = Some(&adapter);
let data = fronma::parser::parse::<Seo>(&markdown)
.expect(&format!("in file: {}", path.to_string_lossy()));
.unwrap();
let res = markdown_to_html_with_plugins(data.body, &options, &plugins);
(res, data.headers)
}
4 changes: 2 additions & 2 deletions examples/mixin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use hirola::{
use wasm_bindgen::JsCast;
use web_sys::Element;

fn x_html<'a>(text: &'a str) -> Box<dyn Fn(&Dom) -> () + 'a> {
fn x_html<'a>(text: &'a str) -> Box<dyn Fn(&Dom) + 'a> {
let cb = move |node: &Dom| {
let dom = node.inner_element();
let element = dom.dyn_ref::<Element>().unwrap();
element.set_inner_html(&format!("{text}")); // Remember to escape this.
element.set_inner_html(text); // Remember to escape this.
};
Box::new(cb)
}
Expand Down
Loading

0 comments on commit 9efab6d

Please sign in to comment.