Skip to content

Commit

Permalink
Use fully qualified syntax in the example of Drawer (#187)
Browse files Browse the repository at this point in the history
* perf: replace `expect` with `unwrap_or_else`

The macro/functions in `expect` is not lazy, which means it will always be called

* style: remove needless borrowing

* perf: remove needless clone

* style: remove needless `format!`

* style: use `and_then` instead

* style: use `?` instead

* style: remove needless closure

* fix: use fully qualified syntax instead

rust-lang/rust#48919

* style: formatted

* style: fix some other clippy issues
  • Loading branch information
bioinformatist authored May 7, 2024
1 parent 51bfb3c commit 1865804
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 34 deletions.
1 change: 0 additions & 1 deletion demo/src/pages/nav_bar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use leptos::*;
use thaw::mobile::{NavBar, NavBarRight};
use thaw::Icon;


#[component]
pub fn NavBarPage() -> impl IntoView {
view! {
Expand Down
8 changes: 4 additions & 4 deletions demo_markdown/docs/drawer/mod.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ let open = Callback::new(move |new_placement: DrawerPlacement| {

view! {
<ButtonGroup>
<Button on_click=Callback::new(move |_| open.call(DrawerPlacement::Top))>"Top"</Button>
<Button on_click=Callback::new(move |_| open.call(DrawerPlacement::Right))>"Right"</Button>
<Button on_click=Callback::new(move |_| open.call(DrawerPlacement::Bottom))>"Bottom"</Button>
<Button on_click=Callback::new(move |_| open.call(DrawerPlacement::Left))>"Left"</Button>
<Button on_click=Callback::new(move |_| leptos::Callable::call(&open, DrawerPlacement::Top))>"Top"</Button>
<Button on_click=Callback::new(move |_| leptos::Callable::call(&open, DrawerPlacement::Right))>"Right"</Button>
<Button on_click=Callback::new(move |_| leptos::Callable::call(&open, DrawerPlacement::Bottom))>"Bottom"</Button>
<Button on_click=Callback::new(move |_| leptos::Callable::call(&open, DrawerPlacement::Left))>"Left"</Button>
</ButtonGroup>
<Drawer title="Title" show placement>
"Hello"
Expand Down
2 changes: 1 addition & 1 deletion demo_markdown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn include_md(_token_stream: proc_macro::TokenStream) -> proc_macro::TokenSt
links
);
syn::parse_str::<ItemFn>(&toc)
.expect(&format!("Cannot be resolved as a function: \n {toc}"))
.unwrap_or_else(|_| panic!("Cannot be resolved as a function: \n {toc}"))
};

let demos: Vec<ItemFn> = demos
Expand Down
12 changes: 4 additions & 8 deletions demo_markdown/src/markdown/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ pub fn to_tokens(code_block: &NodeCodeBlock, demos: &mut Vec<String>) -> TokenSt
let literal = langs
.iter()
.find(|lang| lang != &&"demo")
.map(|lang| highlight_to_html(&code_block.literal, lang))
.flatten()
.and_then(|lang| highlight_to_html(&code_block.literal, lang))
.unwrap_or_else(|| {
is_highlight = false;
code_block.literal.clone()
Expand All @@ -37,8 +36,7 @@ pub fn to_tokens(code_block: &NodeCodeBlock, demos: &mut Vec<String>) -> TokenSt
let mut is_highlight = true;
let literal = langs
.first()
.map(|lang| highlight_to_html(&code_block.literal, lang))
.flatten()
.and_then(|lang| highlight_to_html(&code_block.literal, lang))
.unwrap_or_else(|| {
is_highlight = false;
code_block.literal.clone()
Expand All @@ -56,10 +54,8 @@ pub fn to_tokens(code_block: &NodeCodeBlock, demos: &mut Vec<String>) -> TokenSt
static SYNTAX_SET: OnceLock<SyntaxSet> = OnceLock::new();

fn highlight_to_html(text: &str, syntax: &str) -> Option<String> {
let syntax_set = SYNTAX_SET.get_or_init(|| SyntaxSet::load_defaults_newlines());
let Some(syntax) = syntax_set.find_syntax_by_token(syntax) else {
return None;
};
let syntax_set = SYNTAX_SET.get_or_init(SyntaxSet::load_defaults_newlines);
let syntax = syntax_set.find_syntax_by_token(syntax)?;

let mut html_generator = ClassedHTMLGenerator::new_with_class_style(
syntax,
Expand Down
23 changes: 14 additions & 9 deletions demo_markdown/src/markdown/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use syn::ItemMacro;

pub fn parse_markdown(md_text: &str) -> Result<(TokenStream, Vec<String>, Vec<(String, String)>), String> {
#[allow(clippy::type_complexity)]
pub fn parse_markdown(
md_text: &str,
) -> Result<(TokenStream, Vec<String>, Vec<(String, String)>), String> {
let mut demos: Vec<String> = vec![];
let mut toc: Vec<(String, String)> = vec![];

let arena = Arena::new();
let mut options = comrak::Options::default();
options.extension.table = true;

let root = parse_document(&arena, &md_text, &options);
let root = parse_document(&arena, md_text, &options);
let body = iter_nodes(md_text, root, &mut demos, &mut toc);
Ok((body, demos, toc))
}
Expand Down Expand Up @@ -45,10 +48,12 @@ fn iter_nodes<'a>(
NodeValue::HtmlBlock(node_html_block) => {
let html =
syn::parse_str::<ItemMacro>(&format!("view! {{ {} }}", node_html_block.literal))
.expect(&format!(
"Cannot be resolved as a macro: \n {}",
node_html_block.literal
));
.unwrap_or_else(|_| {
panic!(
"Cannot be resolved as a macro: \n {}",
node_html_block.literal
)
});
quote!(
{
#html
Expand All @@ -62,10 +67,10 @@ fn iter_nodes<'a>(
),
NodeValue::Heading(node_h) => {
let sourcepos = node.data.borrow().sourcepos;
let text = range_text(md_text, sourcepos.start.clone(), sourcepos.end.clone());
let text = range_text(md_text, sourcepos.start, sourcepos.end);
let level = node_h.level as usize + 1;
let text = text[level..].to_string();
let h_id = format!("{}", text.replace(' ', "-").to_ascii_lowercase());
let h_id = text.replace(' ', "-").to_ascii_lowercase().to_string();
toc.push((h_id.clone(), text));
let h = Ident::new(&format!("h{}", node_h.level), Span::call_site());
quote!(
Expand Down Expand Up @@ -186,7 +191,7 @@ fn range_text(text: &str, start: LineColumn, end: LineColumn) -> &str {
let mut current_line_num = start_line + 1;
while current_line_num < end_line {
let next_line = lines.next().unwrap_or("");
start_line_text = &next_line;
start_line_text = next_line;
current_line_num += 1;
}

Expand Down
3 changes: 2 additions & 1 deletion thaw/src/back_top/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::{use_theme, Icon, Theme};
use leptos::{html::ToHtmlElement, *};
use thaw_components::{CSSTransition, Fallback, OptionComp, Teleport};
use thaw_utils::{
add_event_listener, class_list, get_scroll_parent, mount_style, EventListenerHandle, OptionalProp,
add_event_listener, class_list, get_scroll_parent, mount_style, EventListenerHandle,
OptionalProp,
};

#[component]
Expand Down
1 change: 0 additions & 1 deletion thaw/src/back_top/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::theme::ThemeMethod;
#[derive(Clone)]
pub struct BackTopTheme {
pub background_color: String,

}

impl ThemeMethod for BackTopTheme {
Expand Down
6 changes: 6 additions & 0 deletions thaw_utils/src/class_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ use std::{collections::HashSet, rc::Rc};

pub struct ClassList(RwSignal<HashSet<Oco<'static, str>>>);

impl Default for ClassList {
fn default() -> Self {
Self::new()
}
}

impl ClassList {
pub fn new() -> Self {
Self(RwSignal::new(HashSet::new()))
Expand Down
4 changes: 1 addition & 3 deletions thaw_utils/src/dom/get_scroll_parent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use leptos::{
};

pub fn get_scroll_parent(element: &HtmlElement<AnyElement>) -> Option<HtmlElement<AnyElement>> {
let Some(parent_element) = get_parent_element(element) else {
return None;
};
let parent_element = get_parent_element(element)?;

if parent_element.node_type() == 9 {
return Some(parent_element);
Expand Down
4 changes: 1 addition & 3 deletions thaw_utils/src/hooks/use_click_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ pub fn use_click_position() -> ReadSignal<Option<(i32, i32)>> {
if event.client_x() > 0 || event.client_y() > 0 {
return Some((event.client_x(), event.client_y()));
}
let Some(target) = event.target() else {
return None;
};
let target = event.target()?;

let Ok(target) = target.dyn_into::<web_sys::Element>() else {
return None;
Expand Down
2 changes: 1 addition & 1 deletion thaw_utils/src/hooks/use_lock_html_scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn use_lock_html_scroll(is_lock: MaybeSignal<bool>) {
let style = document()
.create_element("style")
.expect("create style element error");
_ = style.set_attribute("data-id", &format!("thaw-lock-html-scroll"));
_ = style.set_attribute("data-id", "thaw-lock-html-scroll");
style.set_text_content(Some("html { overflow: hidden; }"));
_ = head.append_child(&style);
style_el.set_value(Some(style));
Expand Down
2 changes: 1 addition & 1 deletion thaw_utils/src/hooks/use_next_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl NextFrame {
pub fn run(&self, cb: impl FnOnce() + 'static) {
self.cancel();

let next_frame_hadnle = self.0.clone();
let next_frame_hadnle = self.0;
let handle = request_animation_frame_with_handle(move || {
let handle = request_animation_frame_with_handle(cb).unwrap();
next_frame_hadnle.set_value(Some(handle));
Expand Down
2 changes: 1 addition & 1 deletion thaw_utils/src/throttle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use leptos::{leptos_dom::helpers::TimeoutHandle, *};
use std::time::Duration;

pub fn throttle(cb: impl Fn() + 'static, duration: Duration) -> impl Fn() -> () {
pub fn throttle(cb: impl Fn() + 'static, duration: Duration) -> impl Fn() {
let cb = Callback::new(move |_| cb());
let timeout_handle = StoredValue::new(None::<TimeoutHandle>);
on_cleanup(move || {
Expand Down

0 comments on commit 1865804

Please sign in to comment.