microformats/
lib.rs

1pub mod jf2;
2pub mod parse;
3pub use microformats_types as types;
4
5#[derive(thiserror::Error, Debug)]
6pub enum Error {
7    #[error(transparent)]
8    Parse(#[from] parse::Error),
9
10    #[error(transparent)]
11    Types(#[from] types::Error),
12
13    #[error(transparent)]
14    IO(#[from] std::io::Error),
15
16    #[error(transparent)]
17    FromUtf8(#[from] std::string::FromUtf8Error),
18
19    #[error("The required property {name:} was not of the type {kind:?}")]
20    InvalidRequiredProperty { name: String, kind: String },
21
22    #[error(transparent)]
23    Json(#[from] serde_json::Error),
24
25    #[error(transparent)]
26    Jf2Profile(#[from] jf2::profiles::Error),
27}
28
29impl PartialEq for Error {
30    fn eq(&self, other: &Self) -> bool {
31        self.to_string().eq(&other.to_string())
32    }
33}
34
35impl From<url::ParseError> for Error {
36    fn from(value: url::ParseError) -> Self {
37        Self::Parse(parse::Error::from(value))
38    }
39}
40
41impl From<microformats_types::temporal::Error> for Error {
42    fn from(value: microformats_types::temporal::Error) -> Self {
43        Self::Types(types::Error::from(value))
44    }
45}
46
47/// Parses the provided HTML into a `types::Document` resolved with the proviedd URL.
48///
49/// ```
50/// use microformats::from_html;
51///
52/// let document = from_html(r#"
53/// <html>
54///     <head>
55///         <link rel="author me" href="/author">
56///     </head>
57///     <body>
58///     </body>
59/// </html>
60/// "#, "https://example.com".parse().unwrap());
61///
62/// assert!(document.is_ok());
63/// ```
64pub fn from_html(html: &str, url: &url::Url) -> Result<types::Document, Error> {
65    parse::Parser::from_html(html.to_string())
66        .and_then(|parser| parser.into_document(Some(url.clone())))
67}
68
69/// Parses the HTML stored in the provided reader into a `types::Document` resolved with the provided URL.
70pub fn from_reader<R>(mut reader: R, url: &url::Url) -> Result<types::Document, Error>
71where
72    R: std::io::BufRead,
73{
74    let mut html = String::with_capacity(16384);
75    reader
76        .read_to_string(&mut html)
77        .map_err(Error::IO)
78        .and_then(|_| from_html(&html, url))
79}
80
81pub mod http;