Adds variants to Literal for string and boolean

pull/10/head
Tpt 7 years ago
parent 22423b59bd
commit 1800d6deca
  1. 33
      src/model/data.rs

@ -1,3 +1,4 @@
use std::borrow::Cow;
///! Implements data structures for https://www.w3.org/TR/rdf11-concepts/ ///! Implements data structures for https://www.w3.org/TR/rdf11-concepts/
///! Inspired by [RDFjs](http://rdf.js.org/) ///! Inspired by [RDFjs](http://rdf.js.org/)
use std::fmt; use std::fmt;
@ -86,7 +87,9 @@ impl Default for BlankNode {
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub enum Literal { pub enum Literal {
SimpleLiteral(String), SimpleLiteral(String),
String(String),
LanguageTaggedString { value: String, language: String }, LanguageTaggedString { value: String, language: String },
Boolean(bool),
TypedLiteral { value: String, datatype: NamedNode }, TypedLiteral { value: String, datatype: NamedNode },
} }
@ -113,6 +116,7 @@ impl Literal {
/// Builds a RDF [literal](https://www.w3.org/TR/rdf11-concepts/#dfn-literal) with a [datatype](https://www.w3.org/TR/rdf11-concepts/#dfn-datatype-iri) /// Builds a RDF [literal](https://www.w3.org/TR/rdf11-concepts/#dfn-literal) with a [datatype](https://www.w3.org/TR/rdf11-concepts/#dfn-datatype-iri)
pub fn new_typed_literal(value: impl Into<String>, datatype: impl Into<NamedNode>) -> Self { pub fn new_typed_literal(value: impl Into<String>, datatype: impl Into<NamedNode>) -> Self {
//TODO: proper casts
Literal::TypedLiteral { Literal::TypedLiteral {
value: value.into(), value: value.into(),
datatype: datatype.into(), datatype: datatype.into(),
@ -131,11 +135,13 @@ impl Literal {
} }
/// The literal [lexical form](https://www.w3.org/TR/rdf11-concepts/#dfn-lexical-form) /// The literal [lexical form](https://www.w3.org/TR/rdf11-concepts/#dfn-lexical-form)
pub fn value(&self) -> &str { pub fn value<'a>(&'a self) -> Cow<'a, String> {
match self { match self {
Literal::SimpleLiteral(value) => value, Literal::SimpleLiteral(value) => Cow::Borrowed(value),
Literal::LanguageTaggedString { value, .. } => value, Literal::String(value) => Cow::Borrowed(value),
Literal::TypedLiteral { value, .. } => value, Literal::LanguageTaggedString { value, .. } => Cow::Borrowed(value),
Literal::Boolean(value) => Cow::Owned(value.to_string()),
Literal::TypedLiteral { value, .. } => Cow::Borrowed(value),
} }
} }
@ -152,7 +158,9 @@ impl Literal {
pub fn datatype(&self) -> &NamedNode { pub fn datatype(&self) -> &NamedNode {
match self { match self {
Literal::SimpleLiteral(_) => &XSD_STRING, Literal::SimpleLiteral(_) => &XSD_STRING,
Literal::String(_) => &XSD_STRING,
Literal::LanguageTaggedString { .. } => &RDF_LANG_STRING, Literal::LanguageTaggedString { .. } => &RDF_LANG_STRING,
Literal::Boolean(_) => &XSD_BOOLEAN,
Literal::TypedLiteral { datatype, .. } => datatype, Literal::TypedLiteral { datatype, .. } => datatype,
} }
} }
@ -180,22 +188,19 @@ impl fmt::Display for Literal {
impl<'a> From<&'a str> for Literal { impl<'a> From<&'a str> for Literal {
fn from(value: &'a str) -> Self { fn from(value: &'a str) -> Self {
Literal::SimpleLiteral(value.into()) Literal::String(value.into())
} }
} }
impl From<String> for Literal { impl From<String> for Literal {
fn from(value: String) -> Self { fn from(value: String) -> Self {
Literal::SimpleLiteral(value) Literal::String(value)
} }
} }
impl From<bool> for Literal { impl From<bool> for Literal {
fn from(value: bool) -> Self { fn from(value: bool) -> Self {
Literal::TypedLiteral { Literal::Boolean(value)
value: value.to_string(),
datatype: XSD_BOOLEAN.clone(),
}
} }
} }
@ -226,14 +231,6 @@ impl From<f64> for Literal {
} }
} }
impl FromStr for Literal {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(s.into())
}
}
/// The union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) and [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node). /// The union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) and [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node).
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub enum NamedOrBlankNode { pub enum NamedOrBlankNode {

Loading…
Cancel
Save