srdf/
lib.rs

1//! This crate contains a _Simple_ RDF wrapper which can be useful to work with RDF data
2//!
3//! It contains several traits that handle RDF data:
4//! - [`SRDFBasic`]: Basic comparisons on RDF nodes
5//! - [`SRDF`]: Definitions on RDF graphs
6//! - [`FocusRDF`]: RDF graphs with a focus node
7//! - [`RDFNodeParse`]: RDF graphs that can be parsed
8pub mod async_srdf;
9pub mod bnode;
10pub mod lang;
11pub mod literal;
12pub mod neighs;
13pub mod numeric_literal;
14pub mod object;
15pub mod query_srdf;
16pub mod rdf;
17pub mod rdf_data_config;
18pub mod rdf_format;
19pub mod shacl_path;
20pub mod srdf;
21pub mod srdf_basic;
22pub mod srdf_builder;
23pub mod srdf_graph;
24pub mod srdf_parser;
25pub mod srdf_sparql;
26// pub mod triple;
27pub mod matcher;
28pub mod vocab;
29
30pub use crate::async_srdf::*;
31pub use crate::neighs::*;
32pub use crate::query_srdf::*;
33pub use crate::rdf_data_config::*;
34pub use crate::srdf::*;
35pub use crate::srdf_basic::*;
36pub use bnode::*;
37pub use object::*;
38pub use rdf::*;
39pub use rdf_format::*;
40pub use shacl_path::*;
41pub use srdf_builder::*;
42pub use srdf_graph::*;
43pub use srdf_parser::*;
44pub use srdf_sparql::*;
45// pub use triple::*;
46pub use vocab::*;
47
48/// Creates an integer literal
49///
50#[macro_export]
51macro_rules! int {
52    (
53        $n: tt
54      ) => {
55        $crate::literal::Literal::integer($n)
56    };
57}
58
59/// Declares a named RDF parser which can be reused.
60///
61/// The expression which creates the parser should have no side effects as it may be called
62/// multiple times even during a single parse attempt.
63///
64/// This macro is useful when declaring mutually recursive parsers
65///
66/// ```
67///
68/// #[macro_use]
69/// use iri_s::IriS;
70/// use srdf::{rdf_parser, RDFParser, RDF, RDFFormat, FocusRDF, satisfy, ReaderMode, RDFNodeParse, Query, Rdf, property_value, rdf_list, set_focus, parse_property_value_as_list};
71/// use srdf::srdf_graph::SRDFGraph;
72///
73/// rdf_parser!{
74///       fn is_term['a, RDF](term: &'a RDF::Term)(RDF) -> ()
75///       where [
76///       ] {
77///        let name = format!("is_{term}");
78///        satisfy(|t| { t == *term }, name.as_str())
79///       }
80/// }
81///
82/// let s = r#"prefix : <http://example.org/>
83///            :x :p 1.
84/// "#;
85/// let mut graph = SRDFGraph::from_str(s, &RDFFormat::Turtle, None, &ReaderMode::default()).unwrap();
86/// let x = IriS::new_unchecked("http://example.org/x");
87/// let term = x.clone().into();
88/// assert_eq!(is_term(&term).parse(&x, graph).unwrap(), ())
89/// ````
90#[macro_export]
91macro_rules! rdf_parser {
92 (
93   $(#[$attr:meta])*
94   $fn_vis: vis fn $name: ident [$($type_params: tt)*]( $($arg: ident :  $arg_type: ty),*)
95     ($input_type: ty) -> $output_type: ty
96   where [$($where_clause: tt)*]
97     $parser: block
98 ) => {
99    $crate::combine_rdf_parser_impl!{
100      #[allow(non_camel_case_types)]
101      #[doc(hidden)]
102      $fn_vis struct $name;
103      $(#[$attr])*
104      $fn_vis fn $name [$($type_params)*]($($arg : $arg_type),*)($input_type) -> $output_type
105         where [$($where_clause)*]
106      $parser
107 }
108 };
109}
110
111/// Auxiliary macro that is invoked from `rdf_parser` which supports different templates
112#[macro_export]
113macro_rules! combine_rdf_parser_impl {
114    (
115        $(#[$derive:meta])*
116        $struct_vis: vis struct $type_name: ident;
117        $(#[$attr:meta])*
118        $fn_vis: vis fn $name: ident [$($type_params: tt)*]( $($arg: ident :  $arg_type: ty),*)
119            ($input_type: ty) -> $output_type: ty
120            where [$($where_clause: tt)*]
121        $parser: block
122    ) => {
123
124        $(#[$derive])*
125        $struct_vis struct $type_name<$($type_params)*>
126            where
127             $input_type : $crate::FocusRDF,
128             $($where_clause)*
129        {
130            $(pub $arg : $arg_type,)*
131            __marker: ::std::marker::PhantomData<$input_type>,
132        }
133
134        impl <$($type_params)*> $crate::RDFNodeParse<$input_type> for $type_name<$($type_params)*>
135            where
136                $input_type : $crate::FocusRDF,
137                $($where_clause)*
138        {
139
140            type Output = $output_type;
141
142            #[inline]
143            fn parse_impl(
144                &mut self,
145                input: &mut $input_type,
146                ) -> $crate::srdf_parser::PResult<$output_type>
147            {
148                let $type_name { $( $arg: ref mut $arg,)* .. } = *self;
149                let r = $parser.parse_impl(input)?;
150                Ok(r)
151            }
152        }
153
154        $(#[$attr])*
155        #[inline]
156        $fn_vis fn $name< $($type_params)* >(
157                $($arg : $arg_type),*
158            ) -> $type_name<$($type_params)*>
159            where
160                $input_type: $crate::FocusRDF,
161                $($where_clause)*
162        {
163            $type_name {
164                $($arg,)*
165                __marker: ::std::marker::PhantomData,
166            }
167        }
168    }
169}
170
171#[macro_export]
172macro_rules! combine_parsers {
173    ($first : expr) => {
174        $first
175    };
176    ($first : expr, $($rest : expr),+) => {
177        combine_vec($first, combine_parsers!($($rest),+))
178    }
179}