|
|
@ -1,8 +1,10 @@ |
|
|
|
|
|
|
|
use std::cmp::Ordering; |
|
|
|
use std::error::Error; |
|
|
|
use std::error::Error; |
|
|
|
use std::fmt; |
|
|
|
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};
|
|
|
|
/// use oxrdf::{Variable, VariableNameParseError};
|
|
|
|
///
|
|
|
|
///
|
|
|
@ -47,15 +49,124 @@ impl Variable { |
|
|
|
pub fn into_string(self) -> String { |
|
|
|
pub fn into_string(self) -> String { |
|
|
|
self.name |
|
|
|
self.name |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
|
|
|
pub fn as_ref(&self) -> VariableRef<'_> { |
|
|
|
|
|
|
|
VariableRef { name: &self.name } |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for Variable { |
|
|
|
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] |
|
|
|
#[inline] |
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
|
|
write!(f, "?{}", self.name) |
|
|
|
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> { |
|
|
|
fn validate_variable_identifier(id: &str) -> Result<(), VariableNameParseError> { |
|
|
|
let mut chars = id.chars(); |
|
|
|
let mut chars = id.chars(); |
|
|
|
let front = chars.next().ok_or(VariableNameParseError {})?; |
|
|
|
let front = chars.next().ok_or(VariableNameParseError {})?; |
|
|
|