Skip to content

Commit

Permalink
edition 2021
Browse files Browse the repository at this point in the history
  • Loading branch information
Billy-Sheppard committed Nov 29, 2022
1 parent 59e8734 commit 8755d6d
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 104 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -2,7 +2,7 @@
name = "select"
version = "0.6.0-alpha.1"
authors = ["Utkarsh Kukreti <utkarshkukreti@gmail.com>"]
edition = "2018"
edition = "2021"
description = "A library to extract useful data from HTML documents, suitable for web scraping."
license = "MIT"
documentation = "https://docs.rs/select"
Expand Down
6 changes: 3 additions & 3 deletions src/document.rs
Expand Up @@ -7,7 +7,7 @@ use crate::selection::Selection;
use std::io;

/// An HTML document.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Document {
pub nodes: Vec<node::Raw>,
}
Expand Down Expand Up @@ -70,7 +70,7 @@ impl From<StrTendril> for Document {
NodeData::Document => {
let mut prev = None;
for child in node.children.borrow().iter() {
prev = recur(document, &child, None, prev)
prev = recur(document, child, None, prev)
}
None
}
Expand All @@ -97,7 +97,7 @@ impl From<StrTendril> for Document {
let index = append(document, data, parent, prev);
let mut prev = None;
for child in node.children.borrow().iter() {
prev = recur(document, &child, Some(index), prev)
prev = recur(document, child, Some(index), prev)
}
Some(index)
}
Expand Down
23 changes: 9 additions & 14 deletions src/node.rs
Expand Up @@ -8,7 +8,7 @@ use crate::predicate::Predicate;
use crate::selection::Selection;

/// The Node type specific data stored by every Node.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Data {
Text(StrTendril),
Element(QualName, Vec<(QualName, StrTendril)>),
Expand All @@ -17,7 +17,7 @@ pub enum Data {

/// Internal representation of a Node. Not of much use without a reference to a
/// Document.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Raw {
pub index: usize,
pub parent: Option<usize>,
Expand All @@ -29,7 +29,7 @@ pub struct Raw {
}

/// A single node of an HTML document. Nodes may be HTML elements, comments, or text nodes.
#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Node<'a> {
document: &'a Document,
index: usize,
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<'a> Node<'a> {
/// Returns an empty iterator for non Element nodes.
pub fn attrs(&self) -> impl Iterator<Item = (&'a str, &'a str)> {
match *self.data() {
Data::Element(_, ref attrs) => &attrs,
Data::Element(_, ref attrs) => attrs,
_ => [].as_ref(),
}
.iter()
Expand Down Expand Up @@ -174,15 +174,15 @@ impl<'a> Node<'a> {
/// Get the text of a text Node, or None if the node is not text.
pub fn as_text(&self) -> Option<&'a str> {
match *self.data() {
Data::Text(ref text) => Some(&text),
Data::Text(ref text) => Some(text),
_ => None,
}
}

/// Get the text of a comment Node, or None if the node is not a comment.
pub fn as_comment(&self) -> Option<&'a str> {
match *self.data() {
Data::Comment(ref comment) => Some(&comment),
Data::Comment(ref comment) => Some(comment),
_ => None,
}
}
Expand Down Expand Up @@ -249,7 +249,7 @@ impl<'a> serialize::Serialize for Node<'a> {
traversal_scope: serialize::TraversalScope,
) -> io::Result<()> {
match *self.data() {
Data::Text(ref text) => serializer.write_text(&text),
Data::Text(ref text) => serializer.write_text(text),
Data::Element(ref name, ref attrs) => {
let attrs = attrs.iter().map(|&(ref name, ref value)| (name, &**value));

Expand All @@ -263,7 +263,7 @@ impl<'a> serialize::Serialize for Node<'a> {

Ok(())
}
Data::Comment(ref comment) => serializer.write_comment(&comment),
Data::Comment(ref comment) => serializer.write_comment(comment),
}
}
}
Expand Down Expand Up @@ -344,12 +344,7 @@ impl<'a, P: Predicate> Iterator for Select<'a, P> {
type Item = Node<'a>;

fn next(&mut self) -> Option<Node<'a>> {
for node in &mut self.descendants {
if self.predicate.matches(&node) {
return Some(node);
}
}
None
self.descendants.find(|&node| self.predicate.matches(&node))
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/predicate.rs
Expand Up @@ -36,7 +36,7 @@ pub trait Predicate {
}

/// Matches any Node.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Any;

impl Predicate for Any {
Expand All @@ -46,7 +46,7 @@ impl Predicate for Any {
}

/// Matches Element Node with name `T`.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Name<T>(pub T);

impl<'a> Predicate for Name<&'a str> {
Expand All @@ -56,7 +56,7 @@ impl<'a> Predicate for Name<&'a str> {
}

/// Matches Element Node containing class `T`.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Class<T>(pub T);

impl<'a> Predicate for Class<&'a str> {
Expand All @@ -68,7 +68,7 @@ impl<'a> Predicate for Class<&'a str> {
}

/// Matches if the Predicate `T` does not match.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Not<T>(pub T);

impl<T: Predicate> Predicate for Not<T> {
Expand All @@ -79,7 +79,7 @@ impl<T: Predicate> Predicate for Not<T> {

/// Matches Element Node containing attribute `N` with value `V` if `V` is an
/// `&str`, or any value if `V` is `()`.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Attr<N, V>(pub N, pub V);

impl<'a> Predicate for Attr<&'a str, &'a str> {
Expand All @@ -102,7 +102,7 @@ impl<F: Fn(&Node) -> bool> Predicate for F {
}

/// Matches any Element Node.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Element;

impl Predicate for Element {
Expand All @@ -112,7 +112,7 @@ impl Predicate for Element {
}

/// Matches any Text Node.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Text;

impl Predicate for Text {
Expand All @@ -122,7 +122,7 @@ impl Predicate for Text {
}

/// Matches any Comment Node.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Comment;

impl Predicate for Comment {
Expand All @@ -132,7 +132,7 @@ impl Predicate for Comment {
}

/// Matches if either inner Predicate `A` or `B` matches the Node.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Or<A, B>(pub A, pub B);

impl<A: Predicate, B: Predicate> Predicate for Or<A, B> {
Expand All @@ -142,7 +142,7 @@ impl<A: Predicate, B: Predicate> Predicate for Or<A, B> {
}

/// Matches if the inner Predicate `A` and `B` both match the Node.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct And<A, B>(pub A, pub B);

impl<A: Predicate, B: Predicate> Predicate for And<A, B> {
Expand All @@ -153,7 +153,7 @@ impl<A: Predicate, B: Predicate> Predicate for And<A, B> {

/// Matches if inner Predicate `B` matches the node and `A` matches the parent
/// of the node.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Child<A, B>(pub A, pub B);

impl<A: Predicate, B: Predicate> Predicate for Child<A, B> {
Expand All @@ -168,7 +168,7 @@ impl<A: Predicate, B: Predicate> Predicate for Child<A, B> {

/// Matches if inner Predicate `B` matches the node and `A` matches any of the
/// parents of node.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Descendant<A, B>(pub A, pub B);

impl<A: Predicate, B: Predicate> Predicate for Descendant<A, B> {
Expand Down
2 changes: 1 addition & 1 deletion src/selection.rs
Expand Up @@ -3,7 +3,7 @@ use crate::node::Node;
use crate::predicate::Predicate;
use bit_set::{self, BitSet};

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Selection<'a> {
document: &'a Document,
bit_set: BitSet,
Expand Down
10 changes: 5 additions & 5 deletions tests/node_tests.rs
@@ -1,4 +1,4 @@
#![allow(unused_variables, clippy::blacklisted_name)]
#![allow(unused_variables, clippy::disallowed_names)]

pub use std::collections::HashMap;

Expand Down Expand Up @@ -227,7 +227,7 @@ speculate! {
}

test "std::fmt::Debug for Node" {
assert_eq!(format!("{:?}", bar).replace(" ", ""), r#"Element {
assert_eq!(format!("{:?}", bar).replace(' ', ""), r#"Element {
name: "bar",
attrs: [],
children: [
Expand All @@ -237,15 +237,15 @@ speculate! {
attrs: [("class", "another-thing")],
children: [Comment("comment")]
}
]}"#.replace("\n", "").replace(" ", ""));
]}"#.replace(['\n', ' '], ""));

assert_eq!(format!("{:?}", baz), "Text(\"baz\")");

assert_eq!(format!("{:?}", quux).replace(" ", ""), r#"Element {
assert_eq!(format!("{:?}", quux).replace(' ', ""), r#"Element {
name: "quux",
attrs: [("class", "another-thing")],
children: [Comment("comment")]
}"#.replace("\n", "").replace(" ", ""));
}"#.replace(['\n', ' '], ""));

assert_eq!(format!("{:?}", comment), "Comment(\"comment\")");
}
Expand Down

0 comments on commit 8755d6d

Please sign in to comment.