srdf/srdf_parser/
focus_rdf.rs

1use crate::{Query, RDFParseError};
2
3/// Represents RDF graphs that contain a focus node
4///
5/// The trait contains methods to get the focus node and to set its value
6pub trait FocusRDF: Query {
7    /// Set the value of the focus node
8    fn set_focus(&mut self, focus: &Self::Term);
9
10    /// Get the focus node if it exists
11    fn get_focus(&self) -> &Option<Self::Term>;
12
13    /// Get the current focus as a Term
14    fn get_focus_as_term(&self) -> Result<&Self::Term, RDFParseError> {
15        match self.get_focus() {
16            None => Err(RDFParseError::NoFocusNode),
17            Some(term) => Ok(term),
18        }
19    }
20
21    /// Get the current focus as a Subject
22    fn get_focus_as_subject(&self) -> Result<Self::Subject, RDFParseError> {
23        match self.get_focus() {
24            None => Err(RDFParseError::NoFocusNode),
25            Some(term) => {
26                let subject =
27                    term.clone()
28                        .try_into()
29                        .map_err(|_| RDFParseError::ExpectedSubject {
30                            node: format!("{term}"),
31                        })?;
32                Ok(subject)
33            }
34        }
35    }
36}