Fork of https://github.com/oxigraph/oxigraph.git for the purpose of NextGraph project
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
406 lines
10 KiB
406 lines
10 KiB
4 years ago
|
//! This crate provides implementation of [Sophia](https://docs.rs/sophia/) traits for the `model` module.
|
||
4 years ago
|
|
||
3 years ago
|
use crate::*;
|
||
4 years ago
|
use sophia_api::term::*;
|
||
|
use std::fmt;
|
||
|
|
||
|
impl TTerm for BlankNode {
|
||
|
fn kind(&self) -> TermKind {
|
||
|
TermKind::BlankNode
|
||
|
}
|
||
|
|
||
|
fn value_raw(&self) -> RawValue<'_> {
|
||
|
self.as_str().into()
|
||
|
}
|
||
|
|
||
|
fn as_dyn(&self) -> &dyn TTerm {
|
||
|
self
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl TryCopyTerm for BlankNode {
|
||
|
type Error = SophiaToOxigraphConversionError;
|
||
|
|
||
|
fn try_copy<T>(other: &T) -> Result<Self, Self::Error>
|
||
|
where
|
||
|
T: TTerm + ?Sized,
|
||
|
{
|
||
|
match other.kind() {
|
||
4 years ago
|
TermKind::BlankNode => Ok(Self::new_unchecked(other.value_raw().0)),
|
||
4 years ago
|
_ => Err(SophiaToOxigraphConversionError),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a> TTerm for BlankNodeRef<'a> {
|
||
|
fn kind(&self) -> TermKind {
|
||
|
TermKind::BlankNode
|
||
|
}
|
||
|
|
||
|
fn value_raw(&self) -> RawValue<'_> {
|
||
|
self.as_str().into()
|
||
|
}
|
||
|
|
||
|
fn as_dyn(&self) -> &dyn TTerm {
|
||
|
self
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl TTerm for Literal {
|
||
|
fn kind(&self) -> TermKind {
|
||
|
TermKind::Literal
|
||
|
}
|
||
|
|
||
|
fn value_raw(&self) -> RawValue<'_> {
|
||
4 years ago
|
Self::value(self).into()
|
||
4 years ago
|
}
|
||
|
|
||
|
fn datatype(&self) -> Option<SimpleIri<'_>> {
|
||
|
Some(SimpleIri::new_unchecked(
|
||
4 years ago
|
Self::datatype(self).as_str(),
|
||
4 years ago
|
None,
|
||
|
))
|
||
|
}
|
||
|
|
||
|
fn language(&self) -> Option<&str> {
|
||
4 years ago
|
Self::language(self)
|
||
4 years ago
|
}
|
||
|
|
||
|
fn as_dyn(&self) -> &dyn TTerm {
|
||
|
self
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl TryCopyTerm for Literal {
|
||
|
type Error = SophiaToOxigraphConversionError;
|
||
|
|
||
|
fn try_copy<T>(other: &T) -> Result<Self, Self::Error>
|
||
|
where
|
||
|
T: TTerm + ?Sized,
|
||
|
{
|
||
|
match other.kind() {
|
||
|
TermKind::Literal => match other.language() {
|
||
4 years ago
|
Some(tag) => Ok(Self::new_language_tagged_literal_unchecked(
|
||
4 years ago
|
other.value_raw().0,
|
||
|
tag,
|
||
|
)),
|
||
4 years ago
|
None => Ok(Self::new_typed_literal(
|
||
4 years ago
|
other.value_raw().0,
|
||
|
other.datatype().unwrap(),
|
||
|
)),
|
||
|
},
|
||
|
_ => Err(SophiaToOxigraphConversionError),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a> TTerm for LiteralRef<'a> {
|
||
|
fn kind(&self) -> TermKind {
|
||
|
TermKind::Literal
|
||
|
}
|
||
|
|
||
|
fn value_raw(&self) -> RawValue<'_> {
|
||
|
LiteralRef::value(*self).into()
|
||
|
}
|
||
|
|
||
|
fn datatype(&self) -> Option<SimpleIri<'_>> {
|
||
|
Some(SimpleIri::new_unchecked(
|
||
|
LiteralRef::datatype(*self).as_str(),
|
||
|
None,
|
||
|
))
|
||
|
}
|
||
|
|
||
|
fn language(&self) -> Option<&str> {
|
||
|
LiteralRef::language(*self)
|
||
|
}
|
||
|
|
||
|
fn as_dyn(&self) -> &dyn TTerm {
|
||
|
self
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl TTerm for NamedNode {
|
||
|
fn kind(&self) -> TermKind {
|
||
|
TermKind::Iri
|
||
|
}
|
||
|
|
||
|
fn value_raw(&self) -> RawValue<'_> {
|
||
|
self.as_str().into()
|
||
|
}
|
||
|
|
||
|
fn as_dyn(&self) -> &dyn TTerm {
|
||
|
self
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl TryCopyTerm for NamedNode {
|
||
|
type Error = SophiaToOxigraphConversionError;
|
||
|
|
||
|
fn try_copy<T>(other: &T) -> Result<Self, Self::Error>
|
||
|
where
|
||
|
T: TTerm + ?Sized,
|
||
|
{
|
||
|
match other.kind() {
|
||
4 years ago
|
TermKind::Iri => Ok(Self::new_unchecked(other.value())),
|
||
4 years ago
|
_ => Err(SophiaToOxigraphConversionError),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a> From<SimpleIri<'a>> for NamedNode {
|
||
|
fn from(other: SimpleIri<'a>) -> Self {
|
||
4 years ago
|
Self::new_unchecked(other.value())
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a> TTerm for NamedNodeRef<'a> {
|
||
|
fn kind(&self) -> TermKind {
|
||
|
TermKind::BlankNode
|
||
|
}
|
||
|
|
||
|
fn value_raw(&self) -> RawValue<'_> {
|
||
|
self.as_str().into()
|
||
|
}
|
||
|
|
||
|
fn as_dyn(&self) -> &dyn TTerm {
|
||
|
self
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<GraphName> for Option<Term> {
|
||
|
fn from(other: GraphName) -> Self {
|
||
|
match other {
|
||
4 years ago
|
GraphName::NamedNode(n) => Some(n.into()),
|
||
|
GraphName::BlankNode(n) => Some(n.into()),
|
||
|
GraphName::DefaultGraph => None,
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a> From<GraphNameRef<'a>> for Option<TermRef<'a>> {
|
||
|
fn from(other: GraphNameRef<'a>) -> Self {
|
||
|
match other {
|
||
4 years ago
|
GraphNameRef::NamedNode(n) => Some(n.into()),
|
||
|
GraphNameRef::BlankNode(n) => Some(n.into()),
|
||
|
GraphNameRef::DefaultGraph => None,
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
impl TTerm for Subject {
|
||
4 years ago
|
fn kind(&self) -> TermKind {
|
||
|
match self {
|
||
4 years ago
|
Self::NamedNode(_) => TermKind::Iri,
|
||
|
Self::BlankNode(_) => TermKind::BlankNode,
|
||
|
Self::Triple(_) => panic!("RDF-star is not supported yet by Sophia"),
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
fn value_raw(&self) -> RawValue<'_> {
|
||
|
match self {
|
||
4 years ago
|
Self::NamedNode(n) => n.value_raw(),
|
||
|
Self::BlankNode(n) => n.value_raw(),
|
||
|
Self::Triple(_) => panic!("RDF-star is not supported yet by Sophia"),
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
fn as_dyn(&self) -> &dyn TTerm {
|
||
|
match self {
|
||
4 years ago
|
Self::NamedNode(n) => n.as_dyn(),
|
||
|
Self::BlankNode(n) => n.as_dyn(),
|
||
|
Self::Triple(_) => panic!("RDF-star is not supported yet by Sophia"),
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
impl TryCopyTerm for Subject {
|
||
4 years ago
|
type Error = SophiaToOxigraphConversionError;
|
||
|
|
||
|
fn try_copy<T>(other: &T) -> Result<Self, Self::Error>
|
||
|
where
|
||
|
T: TTerm + ?Sized,
|
||
|
{
|
||
|
match other.kind() {
|
||
|
TermKind::Iri => Ok(NamedNode::try_copy(other).unwrap().into()),
|
||
|
TermKind::BlankNode => Ok(BlankNode::try_copy(other).unwrap().into()),
|
||
|
_ => Err(SophiaToOxigraphConversionError),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
impl<'a> TTerm for SubjectRef<'a> {
|
||
4 years ago
|
fn kind(&self) -> TermKind {
|
||
|
match self {
|
||
4 years ago
|
Self::NamedNode(_) => TermKind::Iri,
|
||
|
Self::BlankNode(_) => TermKind::BlankNode,
|
||
|
Self::Triple(_) => panic!("RDF-star is not supported yet by Sophia"),
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
fn value_raw(&self) -> RawValue<'_> {
|
||
|
match self {
|
||
4 years ago
|
Self::NamedNode(n) => n.value_raw(),
|
||
|
Self::BlankNode(n) => n.value_raw(),
|
||
|
Self::Triple(_) => panic!("RDF-star is not supported yet by Sophia"),
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
fn as_dyn(&self) -> &dyn TTerm {
|
||
|
match self {
|
||
4 years ago
|
Self::NamedNode(n) => n.as_dyn(),
|
||
|
Self::BlankNode(n) => n.as_dyn(),
|
||
|
Self::Triple(_) => panic!("RDF-star is not supported yet by Sophia"),
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl TTerm for Term {
|
||
|
fn kind(&self) -> TermKind {
|
||
|
match self {
|
||
4 years ago
|
Self::NamedNode(_) => TermKind::Iri,
|
||
|
Self::BlankNode(_) => TermKind::BlankNode,
|
||
|
Self::Literal(_) => TermKind::Literal,
|
||
|
Self::Triple(_) => panic!("RDF-star is not supported yet by Sophia"),
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
fn value_raw(&self) -> RawValue<'_> {
|
||
|
match self {
|
||
4 years ago
|
Self::NamedNode(n) => n.value_raw(),
|
||
|
Self::BlankNode(n) => n.value_raw(),
|
||
|
Self::Literal(l) => l.value_raw(),
|
||
|
Self::Triple(_) => panic!("RDF-star is not supported yet by Sophia"),
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
fn datatype(&self) -> Option<SimpleIri<'_>> {
|
||
4 years ago
|
if let Self::Literal(l) = self {
|
||
|
TTerm::datatype(l)
|
||
|
} else {
|
||
|
None
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
fn language(&self) -> Option<&str> {
|
||
4 years ago
|
if let Self::Literal(l) = self {
|
||
|
TTerm::language(l)
|
||
|
} else {
|
||
|
None
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
fn as_dyn(&self) -> &dyn TTerm {
|
||
|
match self {
|
||
4 years ago
|
Self::NamedNode(n) => n.as_dyn(),
|
||
|
Self::BlankNode(n) => n.as_dyn(),
|
||
|
Self::Literal(l) => l.as_dyn(),
|
||
|
Self::Triple(_) => panic!("RDF-star is not supported yet by Sophia"),
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl TryCopyTerm for Term {
|
||
|
type Error = SophiaToOxigraphConversionError;
|
||
|
|
||
|
fn try_copy<T>(other: &T) -> Result<Self, Self::Error>
|
||
|
where
|
||
|
T: TTerm + ?Sized,
|
||
|
{
|
||
|
match other.kind() {
|
||
|
TermKind::Iri => Ok(NamedNode::try_copy(other).unwrap().into()),
|
||
|
TermKind::BlankNode => Ok(BlankNode::try_copy(other).unwrap().into()),
|
||
|
TermKind::Literal => Ok(Literal::try_copy(other).unwrap().into()),
|
||
4 years ago
|
TermKind::Variable => Err(SophiaToOxigraphConversionError),
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a> TTerm for TermRef<'a> {
|
||
|
fn kind(&self) -> TermKind {
|
||
|
match self {
|
||
4 years ago
|
Self::NamedNode(_) => TermKind::Iri,
|
||
|
Self::BlankNode(_) => TermKind::BlankNode,
|
||
|
Self::Literal(_) => TermKind::Literal,
|
||
|
Self::Triple(_) => panic!("RDF-star is not supported yet by Sophia"),
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
fn datatype(&self) -> Option<SimpleIri<'_>> {
|
||
4 years ago
|
if let Self::Literal(l) = self {
|
||
|
TTerm::datatype(l)
|
||
|
} else {
|
||
|
None
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
fn language(&self) -> Option<&str> {
|
||
4 years ago
|
if let Self::Literal(l) = self {
|
||
|
TTerm::language(l)
|
||
|
} else {
|
||
|
None
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
fn value_raw(&self) -> RawValue<'_> {
|
||
|
match self {
|
||
|
Self::NamedNode(n) => n.value_raw(),
|
||
|
Self::BlankNode(n) => n.value_raw(),
|
||
|
Self::Literal(l) => l.value_raw(),
|
||
|
Self::Triple(_) => panic!("RDF-star is not supported yet by Sophia"),
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
fn as_dyn(&self) -> &dyn TTerm {
|
||
|
match self {
|
||
4 years ago
|
Self::NamedNode(n) => n.as_dyn(),
|
||
|
Self::BlankNode(n) => n.as_dyn(),
|
||
|
Self::Literal(l) => l.as_dyn(),
|
||
|
Self::Triple(_) => panic!("RDF-star is not supported yet by Sophia"),
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<Quad> for ([Term; 3], Option<Term>) {
|
||
|
fn from(other: Quad) -> Self {
|
||
|
(
|
||
|
[other.subject.into(), other.predicate.into(), other.object],
|
||
|
other.graph_name.into(),
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a> From<QuadRef<'a>> for ([TermRef<'a>; 3], Option<TermRef<'a>>) {
|
||
|
fn from(other: QuadRef<'a>) -> Self {
|
||
|
(
|
||
|
[other.subject.into(), other.predicate.into(), other.object],
|
||
|
other.graph_name.into(),
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<Triple> for [Term; 3] {
|
||
|
fn from(other: Triple) -> Self {
|
||
|
[other.subject.into(), other.predicate.into(), other.object]
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a> From<TripleRef<'a>> for [TermRef<'a>; 3] {
|
||
|
fn from(other: TripleRef<'a>) -> Self {
|
||
|
[other.subject.into(), other.predicate.into(), other.object]
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
/// Error raised when trying to copy a [Sophia](sophia)
|
||
4 years ago
|
/// term as an incompatible Oxigraph term
|
||
|
/// (e.g. a literal into `NamedNode`).
|
||
|
#[derive(Clone, Copy, Debug)]
|
||
|
pub struct SophiaToOxigraphConversionError;
|
||
4 years ago
|
|
||
4 years ago
|
impl fmt::Display for SophiaToOxigraphConversionError {
|
||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||
|
write!(f, "{:?}", self)
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
impl std::error::Error for SophiaToOxigraphConversionError {}
|