Skip to content

Commit 94964ef

Browse files
committed
split out processor graph
1 parent a2fdf24 commit 94964ef

File tree

2 files changed

+65
-57
lines changed

2 files changed

+65
-57
lines changed

html2rdf/src/lib.rs

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use oxrdf::{
1414
use scraper::{ElementRef, Html, node::Element};
1515
use tracing::trace;
1616

17+
use crate::processor_graph::{MessageType, ProcessorGraph};
18+
19+
pub mod processor_graph;
1720
pub mod vocabs;
1821

1922
pub fn process(
@@ -139,7 +142,7 @@ pub fn parse(
139142
}();
140143

141144
if let Err(e) = get_err {
142-
ProcessorGraph(processor_graph).emit(PGType::DocumentError, &e.to_string());
145+
ProcessorGraph(processor_graph).emit_message(MessageType::DocumentError, &e.to_string());
143146
}
144147

145148
Ok(())
@@ -488,8 +491,8 @@ impl<H: HostLanguage> ResolverData<H> {
488491
self.language = Some(Rc::new(lang));
489492
}
490493
Err(e) => {
491-
processor_graph.borrow_mut().emit(
492-
PGType::Warning,
494+
processor_graph.borrow_mut().emit_message(
495+
MessageType::Warning,
493496
&format!("Invalid language identifier ({lang}): {e}"),
494497
);
495498
}
@@ -635,24 +638,24 @@ impl<'e, 'rp, 'p: 'rp, H: HostLanguage> Resolver<'e, 'rp, 'p, H> {
635638
CurieError::EmptyCurie
636639
| CurieError::ExpansionError(ExpansionError::MissingDefault) => {}
637640
CurieError::InvalidBlankNodeSuffix(suffix) => {
638-
self.processor_graph.borrow_mut().emit(
639-
PGType::UnresolvedCurie,
641+
self.processor_graph.borrow_mut().emit_message(
642+
MessageType::UnresolvedCurie,
640643
&format!(
641644
"Invalid CURIE: {value} (invalid blank node suffix: `{suffix}`)",
642645
),
643646
);
644647
}
645648
CurieError::InvalidIRI(iri) => {
646-
self.processor_graph.borrow_mut().emit(
647-
PGType::UnresolvedCurie,
649+
self.processor_graph.borrow_mut().emit_message(
650+
MessageType::UnresolvedCurie,
648651
&format!(
649652
"Invalid CURIE: {value} (expanded to invalid IRI value <{iri}>)",
650653
),
651654
);
652655
}
653656
CurieError::ExpansionError(ExpansionError::Invalid) => {
654-
self.processor_graph.borrow_mut().emit(
655-
PGType::UnresolvedCurie,
657+
self.processor_graph.borrow_mut().emit_message(
658+
MessageType::UnresolvedCurie,
656659
&format!("Invalid CURIE: {value} (no such prefix defined)"),
657660
);
658661
}
@@ -709,8 +712,8 @@ impl<'e, 'rp, 'p: 'rp, H: HostLanguage> Resolver<'e, 'rp, 'p, H> {
709712
}
710713

711714
fn report_invalid_iri(&self, iri_err: IriParseError, value: &str) {
712-
self.processor_graph.borrow_mut().emit(
713-
PGType::Warning,
715+
self.processor_graph.borrow_mut().emit_message(
716+
MessageType::Warning,
714717
&format!("Invalid IRI: <{}> ({})", value, iri_err),
715718
);
716719
}
@@ -1482,7 +1485,6 @@ struct RDFaProcessor<'o, 'p> {
14821485
}
14831486

14841487
struct OutputGraph<'o>(&'o mut oxrdf::Graph);
1485-
struct ProcessorGraph<'p>(&'p mut oxrdf::Graph);
14861488

14871489
impl<'o> OutputGraph<'o> {
14881490
fn emit(&mut self, tr: TripleRef) {
@@ -1491,51 +1493,6 @@ impl<'o> OutputGraph<'o> {
14911493
}
14921494
}
14931495

1494-
impl<'p> ProcessorGraph<'p> {
1495-
fn emit(&mut self, pg_type: PGType, msg: &str) {
1496-
let warning_subj: oxrdf::NamedOrBlankNode = oxrdf::BlankNode::default().into();
1497-
let pg_type: oxrdf::NamedNodeRef = pg_type.into();
1498-
// new bnode is-a PGClass
1499-
let node = TripleRef::new(&warning_subj, oxrdf::vocab::rdf::TYPE, pg_type);
1500-
// add description
1501-
let desc = TripleRef::new(
1502-
&warning_subj,
1503-
vocabs::dc::DESCRIPTION,
1504-
oxrdf::LiteralRef::new_simple_literal(msg),
1505-
);
1506-
trace!(triple = %node, "emitting processor triple (type)");
1507-
self.0.insert(node);
1508-
trace!(triple = %desc, "emitting processor triple (description)");
1509-
self.0.insert(desc);
1510-
}
1511-
}
1512-
1513-
enum PGType {
1514-
// Bases
1515-
Error,
1516-
Warning,
1517-
// Derived
1518-
DocumentError,
1519-
VocabReferenceError,
1520-
UnresolvedCurie,
1521-
UnresolvedTerm,
1522-
PrefixRedefinition,
1523-
}
1524-
1525-
impl From<PGType> for oxrdf::NamedNodeRef<'static> {
1526-
fn from(val: PGType) -> Self {
1527-
match val {
1528-
PGType::Error => vocabs::rdfa::ERROR,
1529-
PGType::Warning => vocabs::rdfa::WARNING,
1530-
PGType::DocumentError => vocabs::rdfa::DOCUMENT_ERROR,
1531-
PGType::VocabReferenceError => vocabs::rdfa::VOCAB_REFERENCE_ERROR,
1532-
PGType::UnresolvedCurie => vocabs::rdfa::UNRESOLVED_CURIE,
1533-
PGType::UnresolvedTerm => vocabs::rdfa::UNRESOLVED_TERM,
1534-
PGType::PrefixRedefinition => vocabs::rdfa::PREFIX_REDEFINITION,
1535-
}
1536-
}
1537-
}
1538-
15391496
trait HostLanguage {
15401497
fn default_language() -> Option<LanguageIdentifier>;
15411498
fn default_vocabulary() -> Option<oxrdf::NamedNode>;

html2rdf/src/processor_graph.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use oxrdf::TripleRef;
2+
use tracing::trace;
3+
4+
use crate::vocabs;
5+
6+
pub(crate) struct ProcessorGraph<'p>(pub &'p mut oxrdf::Graph);
7+
8+
impl<'p> ProcessorGraph<'p> {
9+
pub(crate) fn emit_message(&mut self, pg_type: MessageType, msg: &str) {
10+
let warning_subj: oxrdf::NamedOrBlankNode = oxrdf::BlankNode::default().into();
11+
let pg_type: oxrdf::NamedNodeRef = pg_type.into();
12+
// new bnode is-a PGClass
13+
let node = TripleRef::new(&warning_subj, oxrdf::vocab::rdf::TYPE, pg_type);
14+
// add description
15+
let desc = TripleRef::new(
16+
&warning_subj,
17+
vocabs::dc::DESCRIPTION,
18+
oxrdf::LiteralRef::new_simple_literal(msg),
19+
);
20+
trace!(triple = %node, "emitting processor triple (type)");
21+
self.0.insert(node);
22+
trace!(triple = %desc, "emitting processor triple (description)");
23+
self.0.insert(desc);
24+
}
25+
}
26+
27+
pub enum MessageType {
28+
// Bases
29+
Error,
30+
Warning,
31+
// Derived
32+
DocumentError,
33+
VocabReferenceError,
34+
UnresolvedCurie,
35+
UnresolvedTerm,
36+
PrefixRedefinition,
37+
}
38+
39+
impl From<MessageType> for oxrdf::NamedNodeRef<'static> {
40+
fn from(val: MessageType) -> Self {
41+
match val {
42+
MessageType::Error => vocabs::rdfa::ERROR,
43+
MessageType::Warning => vocabs::rdfa::WARNING,
44+
MessageType::DocumentError => vocabs::rdfa::DOCUMENT_ERROR,
45+
MessageType::VocabReferenceError => vocabs::rdfa::VOCAB_REFERENCE_ERROR,
46+
MessageType::UnresolvedCurie => vocabs::rdfa::UNRESOLVED_CURIE,
47+
MessageType::UnresolvedTerm => vocabs::rdfa::UNRESOLVED_TERM,
48+
MessageType::PrefixRedefinition => vocabs::rdfa::PREFIX_REDEFINITION,
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)