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>`. //! Parsing for `/~<group>`.
use {color_eyre::Result, scraper::Html}; use std::str::FromStr;
use {
color_eyre::{eyre::Error, Result},
scraper::Html,
};
use crate::{ use crate::{
regexes::GROUP_SUBSCRIBERS_RE, regexes::GROUP_SUBSCRIBERS_RE,
@ -42,6 +47,15 @@ pub struct GroupWikiLink {
pub url: String, 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 { impl Group {
/// Parses a [`Group`] from a [`scraper::Html`] tree. /// Parses a [`Group`] from a [`scraper::Html`] tree.
pub fn from_html(html: &Html) -> Result<Self> { pub fn from_html(html: &Html) -> Result<Self> {

View File

@ -1,6 +1,11 @@
//! Parsing for [`/groups`](https://tildes.net/groups). //! 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::{ use crate::{
regexes::{DUPLICATE_WHITESPACE_RE, GROUP_LIST_ACTIVITY_RE}, regexes::{DUPLICATE_WHITESPACE_RE, GROUP_LIST_ACTIVITY_RE},
@ -32,6 +37,15 @@ pub struct GroupListSummary {
pub topic_activity: Option<i32>, 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 { impl GroupList {
/// Parses a [`GroupList`] from a [`scraper::Html`] tree. /// Parses a [`GroupList`] from a [`scraper::Html`] tree.
pub fn from_html(html: &Html) -> Result<Self> { pub fn from_html(html: &Html) -> Result<Self> {

View File

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

View File

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

View File

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

View File

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