Compare commits

...

4 Commits

Author SHA1 Message Date
Bauke 0f0b044993
Make tests use .parse instead of Html. 2022-10-16 18:09:04 +02:00
Bauke 2b944b57af
Implement FromStr. 2022-10-16 18:08:40 +02:00
Bauke cd560973ed
Don't export scraper::Html. 2022-10-16 17:52:55 +02:00
Bauke f8969623c6
Fix doc comment. 2022-10-16 17:51:45 +02:00
6 changed files with 35 additions and 10 deletions

View File

@ -1,6 +1,11 @@
//! Parsing for `/~<group>`.
use {color_eyre::Result, scraper::Html};
use std::str::FromStr;
use {
color_eyre::{eyre::Error, Result},
scraper::Html,
};
use crate::{
regexes::GROUP_SUBSCRIBERS_RE,
@ -42,6 +47,15 @@ pub struct GroupWikiLink {
pub url: String,
}
impl FromStr for Group {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let html = Html::parse_document(s);
Self::from_html(&html)
}
}
impl Group {
/// Parses a [`Group`] from a [`scraper::Html`] tree.
pub fn from_html(html: &Html) -> Result<Self> {

View File

@ -1,6 +1,11 @@
//! Parsing for [`/groups`](https://tildes.net/groups).
use {color_eyre::Result, scraper::Html};
use std::str::FromStr;
use {
color_eyre::{eyre::Error, Result},
scraper::Html,
};
use crate::{
regexes::{DUPLICATE_WHITESPACE_RE, GROUP_LIST_ACTIVITY_RE},
@ -32,6 +37,15 @@ pub struct GroupListSummary {
pub topic_activity: Option<i32>,
}
impl FromStr for GroupList {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let html = Html::parse_document(s);
Self::from_html(&html)
}
}
impl GroupList {
/// Parses a [`GroupList`] from a [`scraper::Html`] tree.
pub fn from_html(html: &Html) -> Result<Self> {

View File

@ -30,5 +30,4 @@ pub(crate) mod group_list;
pub use {
group::{Group, GroupWikiLink},
group_list::{GroupList, GroupListSummary},
scraper::Html,
};

View File

@ -23,7 +23,7 @@ lazy_static! {
/// Selector for the group subscriber count.
pub static ref GROUP_SUBSCRIBERS: Selector = selector(".group-subscription-count");
/// Selector for group wiki links.
/// Selector for sub groups.
pub static ref GROUP_SUB_GROUP_LINKS: Selector = selector(r#"#sidebar .link-group"#);
/// Selector for group wiki links.

View File

@ -1,11 +1,10 @@
use std::fs::read_to_string;
use {insta::assert_debug_snapshot, scraper::Html, tildes_parser::Group};
use {insta::assert_debug_snapshot, tildes_parser::Group};
#[test]
fn test_group_parsing() {
let html = read_to_string("tests/samples/group.html").unwrap();
let html = Html::parse_document(&html);
let group = Group::from_html(&html).unwrap();
let group = &html.parse::<Group>().unwrap();
assert_debug_snapshot!(group);
}

View File

@ -1,11 +1,10 @@
use std::fs::read_to_string;
use {insta::assert_debug_snapshot, scraper::Html, tildes_parser::GroupList};
use {insta::assert_debug_snapshot, tildes_parser::GroupList};
#[test]
fn test_group_list_parsing() {
let html = read_to_string("tests/samples/group_list.html").unwrap();
let html = Html::parse_document(&html);
let group_list = GroupList::from_html(&html).unwrap();
let group_list = html.parse::<GroupList>().unwrap();
assert_debug_snapshot!(group_list);
}