Skip to content

Commit e63a78b

Browse files
committed
Merge branch 'xmh0511-main'
Fix clippy errors, testings and more.
2 parents 5e24754 + 95f5054 commit e63a78b

16 files changed

Lines changed: 135 additions & 101 deletions

File tree

.github/workflows/rust.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Rust CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
check:
9+
name: Cargo Fmt, Check, and Clippy
10+
runs-on: ${{ matrix.os }}
11+
strategy:
12+
matrix:
13+
os: [ubuntu-latest, windows-latest, macos-latest]
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Set up Rust
19+
uses: actions-rs/toolchain@v1
20+
with:
21+
toolchain: stable
22+
override: true
23+
components: clippy, rustfmt
24+
25+
- name: Cargo Fmt
26+
run: cargo fmt --all -- --check
27+
28+
- name: Cargo Check (all features)
29+
run: cargo check --all --all-features
30+
31+
- name: Cargo Clippy (all features)
32+
run: cargo clippy --all --all-features -- -D warnings

.github/workflows/test.yml

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,42 @@
11
name: test
22
on:
3-
- push
3+
push:
4+
45
jobs:
5-
# Task test
66
test:
77
strategy:
88
matrix:
99
os: [ubuntu-latest, macos-latest, windows-latest]
1010
runs-on: ${{ matrix.os }}
1111
steps:
12-
# Checkout repo
1312
- name: Checkout repo
1413
uses: actions/checkout@v3
15-
# Install toolchain
16-
- name: Install toolchain
14+
- name: Install Rust stable toolchain
1715
uses: dtolnay/rust-toolchain@stable
18-
# build coverage
1916
- name: Run unit tests
2017
run: cargo test --features full
21-
# Task coverage
18+
2219
coverage:
23-
strategy:
24-
matrix:
25-
os: [ubuntu-latest]
26-
runs-on: ${{ matrix.os }}
20+
runs-on: ubuntu-latest
2721
steps:
28-
# Checkout repo
2922
- name: Checkout repo
3023
uses: actions/checkout@v3
31-
# Install toolchain
32-
- name: Install toolchain
33-
uses: dtolnay/rust-toolchain@nightly
34-
# Install grcov
24+
- name: Install Rust stable toolchain
25+
uses: dtolnay/rust-toolchain@stable
26+
- name: Install llvm-tools
27+
run: rustup component add llvm-tools-preview
3528
- name: Install grcov
36-
run: curl -L https://github.com/mozilla/grcov/releases/download/v0.8.2/grcov-linux-x86_64.tar.bz2 | tar jxf -
37-
# Run coverage
38-
- name: Build coverage
29+
run: cargo install grcov
30+
- name: Build and test with coverage instrumentation
3931
run: |
4032
export CARGO_INCREMENTAL=0
41-
export RUSTFLAGS="-Zprofile -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort"
42-
export RUSTDOCFLAGS="-Cpanic=abort"
33+
export RUSTFLAGS="-C instrument-coverage"
34+
export LLVM_PROFILE_FILE="cargo-test-%p-%m.profraw"
4335
cargo build --features full --verbose $CARGO_OPTIONS
4436
cargo test --features full --verbose $CARGO_OPTIONS
45-
zip -0 ccov.zip `find . \( -name "visdom*.gc*" \) -print`;
46-
./grcov ccov.zip -s . -t lcov --llvm --branch --ignore-not-existing --ignore "/*" -o lcov.info;
47-
bash <(curl -s https://codecov.io/bash) -f lcov.info;
37+
mkdir coverage
38+
grcov . --binary-path ./target/debug/ -s . -t lcov --branch --ignore-not-existing --ignore "/*" -o coverage/lcov.info
39+
- name: Upload coverage to Codecov
40+
run: bash <(curl -s https://codecov.io/bash) -f coverage/lcov.info
41+
env:
42+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,21 @@ impl Dom {
8181
Dom::halt(
8282
dom,
8383
method,
84-
&format!("Can't {} for a {:?} type", method, my_node_type),
84+
&format!("Can't {method} for a {my_node_type:?} type"),
8585
);
8686
return false;
8787
}
8888
// document
8989
if let INodeType::Document = node.node_type() {
90-
Dom::halt(dom, method, &format!("Can't {} a document type", method));
90+
Dom::halt(dom, method, &format!("Can't {method} a document type"));
9191
return false;
9292
}
9393
// test if same node
9494
if dom.is(node) {
9595
Dom::halt(
9696
dom,
9797
method,
98-
&format!("Can't {} a dom that contains itself.", method),
98+
&format!("Can't {method} a dom that contains itself."),
9999
);
100100
return false;
101101
}
@@ -106,7 +106,7 @@ impl Dom {
106106
Dom::halt(
107107
dom,
108108
method,
109-
&format!("Can't {} a dom that contains it's parent", method),
109+
&format!("Can't {method} a dom that contains it's parent"),
110110
);
111111
return false;
112112
}
@@ -363,7 +363,7 @@ impl INodeTrait for Rc<RefCell<Node>> {
363363
reset_next_siblings_index(0, &nodes);
364364
}
365365
// set childs as new nodes
366-
(*target.borrow_mut()).childs = if has_nodes { Some(nodes) } else { None };
366+
target.borrow_mut().childs = if has_nodes { Some(nodes) } else { None };
367367
} else if let Some(childs) = &mut target.borrow_mut().childs {
368368
let index = self.index();
369369
// not last node, whenever nodes is empty or not, reset next childs indexs
@@ -453,7 +453,7 @@ impl IElementTrait for Rc<RefCell<Node>> {
453453
cur_type => Dom::halt(
454454
self,
455455
"tag_name",
456-
&format!("The node type of '{:?}' doesn't have a tag name.", cur_type),
456+
&format!("The node type of '{cur_type:?}' doesn't have a tag name."),
457457
),
458458
};
459459
vec![]
@@ -919,7 +919,7 @@ impl IElementTrait for Rc<RefCell<Node>> {
919919
Dom::halt(
920920
self,
921921
action,
922-
&format!("Can't {} that not implemented 'Dom'", action),
922+
&format!("Can't {action} that not implemented 'Dom'"),
923923
);
924924
}
925925
}
@@ -986,7 +986,7 @@ impl IElementTrait for Rc<RefCell<Node>> {
986986
Dom::halt(
987987
self,
988988
action,
989-
&format!("Can't {} that not implemented 'Dom'", action),
989+
&format!("Can't {action} that not implemented 'Dom'"),
990990
);
991991
}
992992
}

src/main.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#![allow(clippy::unnecessary_wraps)]
2-
use std::thread;
3-
use std::time::SystemTime;
4-
use std::{collections::VecDeque, error::Error};
5-
use visdom::types::{BoxDynError, IDocumentTrait};
6-
use visdom::types::{BoxDynNode, INodeType};
2+
use visdom::types::BoxDynError;
3+
use visdom::types::INodeType;
74
use visdom::Vis;
85

96
fn main() -> Result<(), BoxDynError> {
@@ -306,14 +303,15 @@ fn main() -> Result<(), BoxDynError> {
306303
// let div_no_has_p = divs.not(":has(p)");
307304
// println!("div_no_has_p: {}", div_no_has_p.text());
308305
let content = root.find("#content");
306+
#[cfg(feature = "text")]
309307
content.texts_by_rec(
310308
0,
311309
Box::new(|_, text_node| {
312310
println!("{}", text_node.text());
313311
true
314312
}),
315313
Box::new(|ele| {
316-
let tag_name = ele.tag_name();
314+
let _tag_name = ele.tag_name();
317315
true
318316
}),
319317
);
@@ -323,18 +321,22 @@ fn main() -> Result<(), BoxDynError> {
323321
child.set_text("abc");
324322
}
325323
}
326-
let pseduo_root = root.find(":root");
324+
let _pseduo_root = root.find(":root");
327325
let child_nodes = root.get(0).unwrap().child_nodes();
328326
println!("{}", child_nodes[1].text_content());
329327
println!("{}", content.html());
328+
#[allow(unused_mut)]
330329
let mut test_clone = root.find("#test_clone");
331330
println!("test_clone:{:?}", test_clone.html());
332-
let test_clone_new = test_clone.clone();
333-
test_clone_new.find(".abc").set_text("哈哈哈");
334-
println!("test_clone:{:?}", test_clone.html());
335-
println!("test_clone_new:{:?}", test_clone_new.html());
336-
test_clone_new.find(".abc").append_to(&mut test_clone);
337-
println!("test_clone:{:?}", test_clone.html());
338-
println!("test_clone_new:{:?}", test_clone_new.html());
331+
#[cfg(any(feature = "insertion", feature = "destroy"))]
332+
{
333+
let test_clone_new = test_clone.clone();
334+
test_clone_new.find(".abc").set_text("哈哈哈");
335+
println!("test_clone:{:?}", test_clone.html());
336+
println!("test_clone_new:{:?}", test_clone_new.html());
337+
test_clone_new.find(".abc").append_to(&mut test_clone);
338+
println!("test_clone:{:?}", test_clone.html());
339+
println!("test_clone_new:{:?}", test_clone_new.html());
340+
};
339341
Ok(())
340342
}

src/mesdoc/interface/element.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cfg_feat_text! {
33
}
44
use super::{BoxDynNode, BoxDynText, Elements, INodeTrait, INodeType};
55
use crate::mesdoc::error::{BoxDynError, Error as IError};
6+
use std::fmt::Display;
67
use std::ops::Range;
78

89
pub type BoxDynElement<'a> = Box<dyn IElementTrait + 'a>;
@@ -35,12 +36,12 @@ impl IAttrValue {
3536
}
3637
}
3738

38-
/// impl `ToString` for IAttrValue
39-
impl ToString for IAttrValue {
40-
fn to_string(&self) -> String {
39+
/// impl `Display` for IAttrValue
40+
impl Display for IAttrValue {
41+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4142
match self {
42-
IAttrValue::Value(v, _) => v.clone(),
43-
IAttrValue::True => String::new(),
43+
IAttrValue::Value(v, _) => write!(f, "{v}"),
44+
IAttrValue::True => write!(f, ""),
4445
}
4546
}
4647
}
@@ -52,11 +53,11 @@ pub enum IFormValue {
5253
Multiple(Vec<String>),
5354
}
5455

55-
impl ToString for IFormValue {
56-
fn to_string(&self) -> String {
56+
impl Display for IFormValue {
57+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5758
match self {
58-
IFormValue::Single(v) => v.clone(),
59-
IFormValue::Multiple(v) => v.join(","),
59+
IFormValue::Single(v) => write!(f, "{v}"),
60+
IFormValue::Multiple(v) => write!(f, "{}", v.join(",")),
6061
}
6162
}
6263
}
@@ -261,6 +262,7 @@ pub trait IElementTrait: INodeTrait {
261262
// element child nodes
262263
fn child_nodes_length(&self) -> usize;
263264
fn child_nodes_item<'b>(&self, index: usize) -> Option<BoxDynNode<'b>>;
265+
#[allow(clippy::type_complexity)]
264266
fn child_nodes_item_since_by<'a>(
265267
&'a self,
266268
node_index: usize,
@@ -293,6 +295,7 @@ pub trait IElementTrait: INodeTrait {
293295
}
294296
result
295297
}
298+
#[allow(clippy::type_complexity)]
296299
fn children_by<'a>(&'a self, matcher: Box<dyn FnMut(&dyn IElementTrait) + 'a>);
297300
// attribute
298301
fn get_attribute(&self, name: &str) -> Option<IAttrValue>;
@@ -360,7 +363,7 @@ mod tests {
360363
fn test_i_attr_value() {
361364
// string value
362365
let attr_value = IAttrValue::Value("Hello".into(), None);
363-
assert!(format!("{:?}", attr_value).contains("Hello"));
366+
assert!(format!("{attr_value:?}").contains("Hello"));
364367
assert!(attr_value.is_str("Hello"));
365368
assert!(!attr_value.is_true());
366369
assert_eq!(attr_value.to_list(), vec!["Hello"]);

src/mesdoc/interface/elements.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ impl<'a> Elements<'a> {
382382
// set prev parent
383383
prev_parent = Some(parent);
384384
// new parent
385-
if parents_indexs.get(&indexs).is_none() {
385+
if !parents_indexs.contains(&indexs) {
386386
parents_indexs.insert(indexs);
387387
uniques.push(ele.cloned());
388388
}
@@ -3127,6 +3127,7 @@ impl<'a> Elements<'a> {
31273127

31283128
/// pub fn `texts_by`
31293129
/// get the text node of each element, filter by the handle
3130+
#[allow(clippy::type_complexity)]
31303131
pub fn texts_by(
31313132
&self,
31323133
limit_depth: usize,
@@ -3137,6 +3138,7 @@ impl<'a> Elements<'a> {
31373138

31383139
/// pub fn `texts_by_rec`
31393140
/// get the text node of each element, filter by the handle, and check if need recursive by the result of rec_handle with child element
3141+
#[allow(clippy::type_complexity)]
31403142
pub fn texts_by_rec(
31413143
&self,
31423144
limit_depth: usize,

src/mesdoc/interface/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub trait INodeTrait {
6565
}
6666
// text
6767
fn text_content(&self) -> String {
68-
return self.text_contents().iter().collect::<String>();
68+
self.text_contents().iter().collect::<String>()
6969
}
7070
fn text(&self) -> String {
7171
self.text_content()
@@ -87,7 +87,7 @@ mod tests {
8787
#[test]
8888
fn test_inode_type() {
8989
let element_type = INodeType::Element;
90-
assert!(format!("{:?}", element_type).contains("Element"));
90+
assert!(format!("{element_type:?}").contains("Element"));
9191
assert!(element_type.is_element());
9292
}
9393
}

src/mesdoc/rules/attr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ pub fn init(rules: &mut Vec<RuleItem>) {
1818
.get("2")
1919
.or_else(|| value_data.get("3"))
2020
.or_else(|| value_data.get("4"))
21-
.map(|s| s.clone());
21+
.cloned();
2222
let match_mode = value_data.get("1").unwrap_or(&def_mode);
23+
#[allow(clippy::type_complexity)]
2324
let handle: Box<dyn Fn(&Option<IAttrValue>) -> bool> = if let Some(attr_value) = attr_value {
2425
let match_mode = match_mode.as_str();
2526
if attr_value.is_empty() && !matches!(match_mode, "" | "!" | "|") {
@@ -48,7 +49,7 @@ pub fn init(rules: &mut Vec<RuleItem>) {
4849
if *v == attr_value {
4950
return true;
5051
}
51-
let attr_value: String = format!("{}-", attr_value);
52+
let attr_value: String = format!("{attr_value}-");
5253
v.starts_with(&attr_value)
5354
}
5455
_ => attr_value.is_empty(),

src/mesdoc/rules/id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn init(rules: &mut Vec<RuleItem>) {
2121
if !eles.is_empty() {
2222
let first_ele = eles
2323
.get_ref()
24-
.get(0)
24+
.first()
2525
.expect("The elements must have at least one element.");
2626
if let Some(doc) = &first_ele.owner_document() {
2727
if let Some(id_element) = doc.get_element_by_id(&id) {

src/mesdoc/rules/pseudo.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ fn make_asc_or_desc_nth_child(selector: &'static str, asc: bool) -> RuleDefItem
342342
PRIORITY,
343343
Box::new(move |data: MatchedQueue| {
344344
let nth_data = &data[2].data;
345-
let n = nth_data.get("n").map(|s| s.clone());
346-
let index = nth_data.get("index").map(|s| s.clone());
345+
let n = nth_data.get("n").cloned();
346+
let index = nth_data.get("index").cloned();
347347
let handle = make_asc_or_desc_nth_child_handle(asc);
348348
let specified_handle = if n.is_none() {
349349
let index = nth_index_to_number(&index);
@@ -605,8 +605,8 @@ fn make_asc_or_desc_nth_of_type(selector: &'static str, asc: bool) -> RuleDefIte
605605
PRIORITY,
606606
Box::new(move |mut data: MatchedQueue| {
607607
let nth_data = data.remove(2).data;
608-
let n = nth_data.get("n").map(|s| s.clone());
609-
let index = nth_data.get("index").map(|s| s.clone());
608+
let n = nth_data.get("n").cloned();
609+
let index = nth_data.get("index").cloned();
610610
let specified_handle = if n.is_none() {
611611
let index = nth_index_to_number(&index);
612612
Some(make_asc_or_desc_nth_of_type_specified(asc, index))

0 commit comments

Comments
 (0)