Adds VariableRef

pull/190/head
Tpt 3 years ago
parent a6a51cad01
commit fefe583a2d
  1. 2
      lib/oxrdf/src/lib.rs
  2. 16
      lib/oxrdf/src/named_node.rs
  3. 113
      lib/oxrdf/src/variable.rs

@ -34,6 +34,6 @@ pub use crate::triple::{
GraphName, GraphNameRef, NamedOrBlankNode, NamedOrBlankNodeRef, Quad, QuadRef, Subject,
SubjectRef, Term, TermRef, Triple, TripleRef,
};
pub use crate::variable::{Variable, VariableNameParseError};
pub use crate::variable::{Variable, VariableNameParseError, VariableRef};
pub use oxilangtag::LanguageTagParseError;
pub use oxiri::IriParseError;

@ -1,4 +1,5 @@
use oxiri::{Iri, IriParseError};
use std::cmp::Ordering;
use std::fmt;
/// An owned RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri).
@ -154,6 +155,7 @@ impl From<NamedNodeRef<'_>> for NamedNode {
}
impl<'a> From<&'a NamedNode> for NamedNodeRef<'a> {
#[inline]
fn from(node: &'a NamedNode) -> Self {
node.as_ref()
}
@ -200,3 +202,17 @@ impl PartialEq<NamedNodeRef<'_>> for &str {
*self == other
}
}
impl PartialOrd<NamedNode> for NamedNodeRef<'_> {
#[inline]
fn partial_cmp(&self, other: &NamedNode) -> Option<Ordering> {
self.partial_cmp(&other.as_ref())
}
}
impl PartialOrd<NamedNodeRef<'_>> for NamedNode {
#[inline]
fn partial_cmp(&self, other: &NamedNodeRef<'_>) -> Option<Ordering> {
self.as_ref().partial_cmp(other)
}
}

@ -1,8 +1,10 @@
use std::cmp::Ordering;
use std::error::Error;
use std::fmt;
/// A [SPARQL query](https://www.w3.org/TR/sparql11-query/) variable.
/// A [SPARQL query](https://www.w3.org/TR/sparql11-query/) owned variable.
///
/// The default string formatter is returning a SPARQL compatible representation:
/// ```
/// use oxrdf::{Variable, VariableNameParseError};
///
@ -47,15 +49,124 @@ impl Variable {
pub fn into_string(self) -> String {
self.name
}
#[inline]
pub fn as_ref(&self) -> VariableRef<'_> {
VariableRef { name: &self.name }
}
}
impl fmt::Display for Variable {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_ref().fmt(f)
}
}
/// A [SPARQL query](https://www.w3.org/TR/sparql11-query/) borrowed variable.
///
/// The default string formatter is returning a SPARQL compatible representation:
/// ```
/// use oxrdf::{VariableRef, VariableNameParseError};
///
/// assert_eq!(
/// "?foo",
/// VariableRef::new("foo")?.to_string()
/// );
/// # Result::<_,VariableNameParseError>::Ok(())
/// ```
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash)]
pub struct VariableRef<'a> {
name: &'a str,
}
impl<'a> VariableRef<'a> {
/// Creates a variable name from a unique identifier.
///
/// The variable identifier must be valid according to the SPARQL grammar.
pub fn new(name: &'a str) -> Result<Self, VariableNameParseError> {
validate_variable_identifier(name)?;
Ok(Self::new_unchecked(name))
}
/// Creates a variable name from a unique identifier without validation.
///
/// It is the caller's responsibility to ensure that `id` is a valid blank node identifier
/// according to the SPARQL grammar.
///
/// [`Variable::new()`] is a safe version of this constructor and should be used for untrusted data.
#[inline]
pub fn new_unchecked(name: &'a str) -> Self {
Self { name }
}
#[inline]
pub fn as_str(&self) -> &str {
self.name
}
#[inline]
pub fn into_string(self) -> String {
self.name.to_owned()
}
#[inline]
pub fn into_owned(self) -> Variable {
Variable {
name: self.name.to_owned(),
}
}
}
impl fmt::Display for VariableRef<'_> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "?{}", self.name)
}
}
impl<'a> From<&'a Variable> for VariableRef<'a> {
#[inline]
fn from(variable: &'a Variable) -> Self {
variable.as_ref()
}
}
impl<'a> From<VariableRef<'a>> for Variable {
#[inline]
fn from(variable: VariableRef<'a>) -> Self {
variable.into_owned()
}
}
impl PartialEq<Variable> for VariableRef<'_> {
#[inline]
fn eq(&self, other: &Variable) -> bool {
*self == other.as_ref()
}
}
impl PartialEq<VariableRef<'_>> for Variable {
#[inline]
fn eq(&self, other: &VariableRef<'_>) -> bool {
self.as_ref() == *other
}
}
impl PartialOrd<Variable> for VariableRef<'_> {
#[inline]
fn partial_cmp(&self, other: &Variable) -> Option<Ordering> {
self.partial_cmp(&other.as_ref())
}
}
impl PartialOrd<VariableRef<'_>> for Variable {
#[inline]
fn partial_cmp(&self, other: &VariableRef<'_>) -> Option<Ordering> {
self.as_ref().partial_cmp(other)
}
}
fn validate_variable_identifier(id: &str) -> Result<(), VariableNameParseError> {
let mut chars = id.chars();
let front = chars.next().ok_or(VariableNameParseError {})?;

Loading…
Cancel
Save