From f3a649a3946af04bd1b18174ecacaf250fe44721 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 23 Apr 2026 07:17:58 +0000 Subject: [PATCH 1/5] fix: resolve bugs and apply optimizations identified in code analysis Agent-Logs-Url: https://github.com/xmh0511/visdom/sessions/9b6c2668-d7e3-4448-a5a2-0d58276af047 Co-authored-by: xmh0511 <17973891+xmh0511@users.noreply.github.com> --- src/lib.rs | 2 +- src/mesdoc/interface/element.rs | 2 +- src/mesdoc/interface/elements.rs | 12 ++++++------ src/mesdoc/selector/mod.rs | 33 +++++++++++++++----------------- src/mesdoc/selector/pattern.rs | 6 ++++++ src/mesdoc/utils.rs | 1 + 6 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3466c50..132fbd8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1140,7 +1140,7 @@ impl IDocumentTrait for Document { /// /// # Examples /// -/// ``` +/// ```ignore /// use visdom::Vis; /// use visdom::types::BoxDynError; /// fn main()-> Result<(), BoxDynError>{ diff --git a/src/mesdoc/interface/element.rs b/src/mesdoc/interface/element.rs index 930d43f..08f7dee 100644 --- a/src/mesdoc/interface/element.rs +++ b/src/mesdoc/interface/element.rs @@ -69,7 +69,7 @@ impl IntoIterator for IFormValue { fn into_iter(self) -> Self::IntoIter { match self { IFormValue::Multiple(v) => v.into_iter(), - IFormValue::Single(_) => vec![].into_iter(), + IFormValue::Single(v) => vec![v].into_iter(), } } } diff --git a/src/mesdoc/interface/elements.rs b/src/mesdoc/interface/elements.rs index 3cb4e81..b481e4e 100644 --- a/src/mesdoc/interface/elements.rs +++ b/src/mesdoc/interface/elements.rs @@ -59,7 +59,7 @@ enum ElementRelation { Ancestor, Equal, Descendant, - Feauture, + Feature, } // check if ancestor and descendants fn relation_of(a: &VecDeque, b: &VecDeque) -> ElementRelation { @@ -81,7 +81,7 @@ fn relation_of(a: &VecDeque, b: &VecDeque) -> ElementRelation { let a_left = a_total - equal_num; let b_left = b_total - equal_num; match (a_left == 0, b_left == 0) { - (false, false) => ElementRelation::Feauture, + (false, false) => ElementRelation::Feature, (false, true) => ElementRelation::Descendant, (true, true) => ElementRelation::Equal, (true, false) => ElementRelation::Ancestor, @@ -458,7 +458,7 @@ impl<'a> Elements<'a> { // just check the last ancestor let (top_ele_indexs, _) = &ancestors[cur_len - 1]; match relation_of(&ele_indexs, top_ele_indexs) { - ElementRelation::Feauture => { + ElementRelation::Feature => { ancestors.push((ele_indexs, ele)); } ElementRelation::Descendant => {} @@ -3866,9 +3866,9 @@ mod tests { assert!(matches!(relation_of(&a, &b), ElementRelation::Ancestor)); assert!(matches!(relation_of(&b, &a), ElementRelation::Descendant)); let c: VecDeque = vec![1, 2, 3].into(); - assert!(matches!(relation_of(&b, &c), ElementRelation::Feauture)); - assert!(matches!(relation_of(&c, &b), ElementRelation::Feauture)); - assert!(matches!(relation_of(&c, &a), ElementRelation::Feauture)); + assert!(matches!(relation_of(&b, &c), ElementRelation::Feature)); + assert!(matches!(relation_of(&c, &b), ElementRelation::Feature)); + assert!(matches!(relation_of(&c, &a), ElementRelation::Feature)); let d: VecDeque = vec![0, 1].into(); assert!(matches!(relation_of(&b, &d), ElementRelation::Equal)); } diff --git a/src/mesdoc/selector/mod.rs b/src/mesdoc/selector/mod.rs index 44569e6..fb858ee 100644 --- a/src/mesdoc/selector/mod.rs +++ b/src/mesdoc/selector/mod.rs @@ -224,28 +224,25 @@ impl Selector { let mut max_index: usize = 0; let mut max_priority: u32 = 0; for (index, r) in group.iter_mut().enumerate() { - let mut total_priority = 0; - if r.len() > 1 { - let chain_comb = r[0].1; - r.sort_by(|a, b| b.0.priority.partial_cmp(&a.0.priority).unwrap()); - let now_first = &mut r[0]; - if now_first.1 != chain_comb { - now_first.1 = chain_comb; - total_priority += now_first.0.priority; - for n in &mut r[1..] { - n.1 = Combinator::Chain; - total_priority += n.0.priority; - } + if r.len() > 1 { + let chain_comb = r[0].1; + r.sort_by(|a, b| b.0.priority.cmp(&a.0.priority)); + let now_first = &mut r[0]; + if now_first.1 != chain_comb { + now_first.1 = chain_comb; + for n in &mut r[1..] { + n.1 = Combinator::Chain; } } - if use_lookup { - total_priority = r.iter().map(|p| p.0.priority).sum(); - if total_priority > max_priority { - max_priority = total_priority; - max_index = index; - } + } + if use_lookup { + let total_priority: u32 = r.iter().map(|p| p.0.priority).sum(); + if total_priority > max_priority { + max_priority = total_priority; + max_index = index; } } + } // if the first combinator is child, and the max_index > 1, use the max_index's rule first if use_lookup && max_index > 0 { let is_child = matches!( diff --git a/src/mesdoc/selector/pattern.rs b/src/mesdoc/selector/pattern.rs index 58c4d43..6b6b3d5 100644 --- a/src/mesdoc/selector/pattern.rs +++ b/src/mesdoc/selector/pattern.rs @@ -45,6 +45,9 @@ pub trait Pattern: Send + Sync + Debug { impl Pattern for char { fn matched(&self, chars: &[char]) -> Option { + if chars.is_empty() { + return None; + } let ch = chars[0]; if *self == ch { return Some(Matched { @@ -92,6 +95,9 @@ pub struct Identity; impl Pattern for Identity { fn matched(&self, chars: &[char]) -> Option { + if chars.is_empty() { + return None; + } let mut result: Vec = Vec::with_capacity(5); let first = chars[0]; let name: &str = "identity"; diff --git a/src/mesdoc/utils.rs b/src/mesdoc/utils.rs index 608f430..fcd31df 100644 --- a/src/mesdoc/utils.rs +++ b/src/mesdoc/utils.rs @@ -224,6 +224,7 @@ fn contains_chars_nocheck(target: &[char], search: &[char], t_len: usize, s_len: continue; } move_one = true; + break; } if !move_one { return true; From 759410ee94afec3750e881879f9502a1e364c9a8 Mon Sep 17 00:00:00 2001 From: xmh0511 <970252187@qq.com> Date: Thu, 23 Apr 2026 16:26:57 +0800 Subject: [PATCH 2/5] clippy fix --- src/mesdoc/selector/mod.rs | 6 +++--- src/mesdoc/selector/rule.rs | 7 +------ 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/mesdoc/selector/mod.rs b/src/mesdoc/selector/mod.rs index 44569e6..be2b689 100644 --- a/src/mesdoc/selector/mod.rs +++ b/src/mesdoc/selector/mod.rs @@ -222,7 +222,7 @@ impl Selector { for mut group in groups { // first optimize the chain selectors, the rule who's priority is bigger will apply first let mut max_index: usize = 0; - let mut max_priority: u32 = 0; + const MAX_PRIORITY: u32 = 0; for (index, r) in group.iter_mut().enumerate() { let mut total_priority = 0; if r.len() > 1 { @@ -240,11 +240,11 @@ impl Selector { } if use_lookup { total_priority = r.iter().map(|p| p.0.priority).sum(); - if total_priority > max_priority { - max_priority = total_priority; + if total_priority > MAX_PRIORITY { max_index = index; } } + _ = total_priority; } // if the first combinator is child, and the max_index > 1, use the max_index's rule first if use_lookup && max_index > 0 { diff --git a/src/mesdoc/selector/rule.rs b/src/mesdoc/selector/rule.rs index 9477160..ae596d4 100644 --- a/src/mesdoc/selector/rule.rs +++ b/src/mesdoc/selector/rule.rs @@ -139,12 +139,7 @@ impl Rule { let mut index: usize = 0; for ch in content.chars() { index += 1; - let is_prev_matched_finish = if is_matched_finish { - is_matched_finish = false; - true - } else { - false - }; + let is_prev_matched_finish = std::mem::take(&mut is_matched_finish); if store.is_wait_end { if ch.is_ascii_whitespace() { continue; From 036b10a8a9e2e195239490fcc98792c5d2905318 Mon Sep 17 00:00:00 2001 From: xmh0511 <970252187@qq.com> Date: Thu, 23 Apr 2026 16:40:45 +0800 Subject: [PATCH 3/5] clippy fix --- src/mesdoc/selector/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mesdoc/selector/mod.rs b/src/mesdoc/selector/mod.rs index 962de60..3b026ed 100644 --- a/src/mesdoc/selector/mod.rs +++ b/src/mesdoc/selector/mod.rs @@ -226,6 +226,7 @@ impl Selector { for (index, r) in group.iter_mut().enumerate() { if r.len() > 1 { let chain_comb = r[0].1; + #[allow(clippy::unnecessary_sort_by)] r.sort_by(|a, b| b.0.priority.cmp(&a.0.priority)); let now_first = &mut r[0]; if now_first.1 != chain_comb { From c91a73cc8965c5df944e6819e7c51b6163cb291c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 23 Apr 2026 08:56:00 +0000 Subject: [PATCH 4/5] fix: retain_by_index panic on unsorted indices; simplify is_equal_chars; improve expect messages Agent-Logs-Url: https://github.com/xmh0511/visdom/sessions/28aa0de0-6a79-4b25-8832-4e444dd040e9 Co-authored-by: xmh0511 <17973891+xmh0511@users.noreply.github.com> --- src/mesdoc/selector/pattern.rs | 4 ++-- src/mesdoc/selector/rule.rs | 2 +- src/mesdoc/utils.rs | 21 ++------------------- 3 files changed, 5 insertions(+), 22 deletions(-) diff --git a/src/mesdoc/selector/pattern.rs b/src/mesdoc/selector/pattern.rs index 6b6b3d5..47a9ede 100644 --- a/src/mesdoc/selector/pattern.rs +++ b/src/mesdoc/selector/pattern.rs @@ -275,10 +275,10 @@ impl Nth { ) -> Vec { // has n if let Some(n) = n { - let n = n.parse::().unwrap(); + let n = n.parse::().expect("nth 'n' value must be a valid integer"); let index = index .as_ref() - .map(|index| index.parse::().unwrap()) + .map(|index| index.parse::().expect("nth 'index' value must be a valid integer")) .unwrap_or(0); // n == 0 if n == 0 { diff --git a/src/mesdoc/selector/rule.rs b/src/mesdoc/selector/rule.rs index ae596d4..3b3231e 100644 --- a/src/mesdoc/selector/rule.rs +++ b/src/mesdoc/selector/rule.rs @@ -47,7 +47,7 @@ impl Matcher { if let Some(handle) = &self.all_handle { return handle(eles, use_cache); } - let handle = self.one_handle.as_ref().unwrap(); + let handle = self.one_handle.as_ref().expect("Matcher must have either all_handle or one_handle"); let mut result = Elements::with_capacity(5); for ele in eles.get_ref() { if handle(&**ele, use_cache) { diff --git a/src/mesdoc/utils.rs b/src/mesdoc/utils.rs index fcd31df..be60e73 100644 --- a/src/mesdoc/utils.rs +++ b/src/mesdoc/utils.rs @@ -111,17 +111,12 @@ pub fn divide_isize(a: isize, b: isize, round: RoundType) -> isize { } pub fn retain_by_index(v: &mut Vec, indexs: &[usize]) { - for (i, index) in indexs.iter().enumerate() { - v.remove(index - i); - } - /* let mut loop_index: usize = 0; v.retain(|_| { let removed = indexs.contains(&loop_index); loop_index += 1; !removed }); - */ } // get a class list from class attribute @@ -174,8 +169,7 @@ pub fn is_equal_chars_ignore_case(target: &[char], cmp: &[char]) -> bool { if target.len() != cmp.len() { return false; } - for (index, ch) in target.iter().enumerate() { - let cmp_ch = &cmp[index]; + for (ch, cmp_ch) in target.iter().zip(cmp.iter()) { if cmp_ch == ch { continue; } @@ -191,7 +185,6 @@ pub fn is_equal_chars_ignore_case(target: &[char], cmp: &[char]) -> bool { } } _ => { - // not equal return false; } } @@ -200,17 +193,7 @@ pub fn is_equal_chars_ignore_case(target: &[char], cmp: &[char]) -> bool { } pub fn is_equal_chars(target: &[char], cmp: &[char]) -> bool { - let t_len = target.len(); - let s_len = cmp.len(); - if t_len == s_len { - for (index, ch) in target.iter().enumerate() { - if ch != &cmp[index] { - return false; - } - } - return true; - } - false + target == cmp } fn contains_chars_nocheck(target: &[char], search: &[char], t_len: usize, s_len: usize) -> bool { From 439fcb78f889537628961b0ee2480a710b484d93 Mon Sep 17 00:00:00 2001 From: xmh0511 <970252187@qq.com> Date: Thu, 23 Apr 2026 17:08:13 +0800 Subject: [PATCH 5/5] clippy fix --- src/mesdoc/selector/pattern.rs | 10 ++++++++-- src/mesdoc/selector/rule.rs | 5 ++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/mesdoc/selector/pattern.rs b/src/mesdoc/selector/pattern.rs index 47a9ede..d5e8ecb 100644 --- a/src/mesdoc/selector/pattern.rs +++ b/src/mesdoc/selector/pattern.rs @@ -275,10 +275,16 @@ impl Nth { ) -> Vec { // has n if let Some(n) = n { - let n = n.parse::().expect("nth 'n' value must be a valid integer"); + let n = n + .parse::() + .expect("nth 'n' value must be a valid integer"); let index = index .as_ref() - .map(|index| index.parse::().expect("nth 'index' value must be a valid integer")) + .map(|index| { + index + .parse::() + .expect("nth 'index' value must be a valid integer") + }) .unwrap_or(0); // n == 0 if n == 0 { diff --git a/src/mesdoc/selector/rule.rs b/src/mesdoc/selector/rule.rs index 3b3231e..050961e 100644 --- a/src/mesdoc/selector/rule.rs +++ b/src/mesdoc/selector/rule.rs @@ -47,7 +47,10 @@ impl Matcher { if let Some(handle) = &self.all_handle { return handle(eles, use_cache); } - let handle = self.one_handle.as_ref().expect("Matcher must have either all_handle or one_handle"); + let handle = self + .one_handle + .as_ref() + .expect("Matcher must have either all_handle or one_handle"); let mut result = Elements::with_capacity(5); for ele in eles.get_ref() { if handle(&**ele, use_cache) {