Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
holmofy committed Jul 13, 2024
1 parent 48ddbcf commit 787e163
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = [".", "reqwest-scraper-macros"]

[package]
name = "reqwest-scraper"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
description = "Web scraping integration with reqwest"
license = "MIT"
Expand All @@ -23,7 +23,7 @@ reqwest = { version = "0.12" }
scraper = { version = "0.19", optional = true }
serde = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
reqwest-scraper-macros = { version = "0.3.0", path = "./reqwest-scraper-macros", optional = true }
reqwest-scraper-macros = { version = "0.3.1", path = "./reqwest-scraper-macros", optional = true }
thiserror = "1.0"

[features]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Extends [reqwest](https://github.com/seanmonstar/reqwest) to support multiple we
* add dependency
```toml
reqwest = { version = "0.12", features = ["json"] }
reqwest-scraper="0.3.0"
reqwest-scraper="0.3.1"
```
* use ScraperResponse
```rust
Expand Down
2 changes: 1 addition & 1 deletion reqwest-scraper-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reqwest-scraper-macros"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
description = "Web scraping integration with reqwest"
license = "MIT"
Expand Down
17 changes: 8 additions & 9 deletions src/css_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,15 @@ impl<'a, 'b> Iterator for HtmlSelectIterator<'a, 'b> {
type Item = SelectItem<'a>;

fn next(&mut self) -> Option<Self::Item> {
Some(Self::Item {
element: self.select.next()?,
})
self.select.next().map(Self::Item::new)
}
}

impl<'a, 'b> Iterator for ElementSelectIterator<'a, 'b> {
type Item = SelectItem<'a>;

fn next(&mut self) -> Option<Self::Item> {
Some(Self::Item {
element: self.select.next()?,
})
self.select.next().map(Self::Item::new)
}
}

Expand All @@ -103,6 +99,11 @@ pub type Classes<'a> = scraper::node::Classes<'a>;
pub type Attrs<'a> = scraper::node::Attrs<'a>;

impl<'a> SelectItem<'a> {
/// constructor
pub fn new(element: ElementRef<'a>) -> Self {
Self { element }
}

/// Returns the element name.
pub fn name(&self) -> &str {
self.element.value().name()
Expand Down Expand Up @@ -150,9 +151,7 @@ impl<'a> SelectItem<'a> {

/// Iterate over all child nodes which are elements
pub fn children(&self) -> impl Iterator<Item = SelectItem<'a>> {
self.element
.child_elements()
.map(|e| SelectItem { element: e })
self.element.child_elements().map(SelectItem::new)
}

/// Use CSS selector to find elements based on the current element
Expand Down
4 changes: 2 additions & 2 deletions src/jsonpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Json {
.str_path(path)?
.value(&self.value)
.select_as()
.map_err(|e| e.into())
.map_err(ScraperError::from)
}

/// Use jsonpath to select json string fields
Expand All @@ -44,6 +44,6 @@ impl Json {
.str_path(path)?
.value(&self.value)
.select_as_str()
.map_err(|e| e.into())
.map_err(ScraperError::from)
}
}
27 changes: 17 additions & 10 deletions src/xpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ impl XPathResult {
self.object
.get_nodes_as_vec()
.into_iter()
.map(|node| Node { node })
.collect_vec()
.map(Node::new)
.collect::<Vec<_>>()
}

/// return multiple results as string
Expand All @@ -57,16 +57,24 @@ impl XPathResult {
self.object
.get_nodes_as_vec()
.first()
.map(|n| Node { node: n.to_owned() })
.map(|n| Node::new(n.to_owned()))
}

/// return first result as string
pub fn as_str(&self) -> Option<String> {
self.object.get_nodes_as_str().first().map(|s| s.to_owned())
self.object
.get_nodes_as_str()
.first()
.map(ToOwned::to_owned)
}
}

impl Node {
/// constructor
pub fn new(node: libxml::tree::node::Node) -> Self {
Self { node }
}

/// Returns the element name.
pub fn name(&self) -> String {
self.node.get_name()
Expand Down Expand Up @@ -116,7 +124,7 @@ impl Node {
self.node
.get_child_elements()
.into_iter()
.map(|node| Node { node })
.map(Node::new)
.collect_vec()
}

Expand All @@ -129,7 +137,7 @@ impl Node {
ScraperError::XPathError(format!("relative xpath parse failed:{}", relative_xpath))
})?
.into_iter()
.map(|node| Node { node })
.map(Node::new)
.collect_vec())
}

Expand All @@ -146,15 +154,14 @@ impl Node {

/// Find first node based on this node using a relative xpath
pub fn findnode(&self, relative_xpath: &str) -> Result<Option<Node>> {
Ok(self.node
Ok(self
.node
.findnodes(relative_xpath)
.map_err(|_| {
ScraperError::XPathError(format!("relative xpath parse failed:{}", relative_xpath))
})?
.first()
.map(|node| Node {
node: node.to_owned(),
}))
.map(|node| Node::new(node.to_owned())))
}

/// Find first value based on this node using a relative xpath
Expand Down

0 comments on commit 787e163

Please sign in to comment.