@ -1,4 +1,3 @@
use crate ::model ::language_tag ::LanguageTag ;
use crate ::model ::named_node ::NamedNode ;
use crate ::model ::named_node ::NamedNode ;
use crate ::model ::vocab ::rdf ;
use crate ::model ::vocab ::rdf ;
use crate ::model ::vocab ::xsd ;
use crate ::model ::vocab ::xsd ;
@ -19,7 +18,6 @@ use std::option::Option;
/// The default string formatter is returning a N-Triples, Turtle and SPARQL compatible representation:
/// The default string formatter is returning a N-Triples, Turtle and SPARQL compatible representation:
/// ```
/// ```
/// use rudf::model::Literal;
/// use rudf::model::Literal;
/// use rudf::model::LanguageTag;
/// use rudf::model::vocab::xsd;
/// use rudf::model::vocab::xsd;
///
///
/// assert_eq!(
/// assert_eq!(
@ -34,7 +32,7 @@ use std::option::Option;
///
///
/// assert_eq!(
/// assert_eq!(
/// "\"foo\"@en",
/// "\"foo\"@en",
/// Literal::new_language_tagged_literal("foo", LanguageTag::parse( "en").unwrap() ).to_string()
/// Literal::new_language_tagged_literal("foo", "en").to_string()
/// );
/// );
/// ```
/// ```
#[ derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash) ]
#[ derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash) ]
@ -43,10 +41,7 @@ pub struct Literal(LiteralContent);
#[ derive(PartialEq, Eq, Ord, PartialOrd, Debug, Clone, Hash) ]
#[ derive(PartialEq, Eq, Ord, PartialOrd, Debug, Clone, Hash) ]
enum LiteralContent {
enum LiteralContent {
String ( String ) ,
String ( String ) ,
LanguageTaggedString {
LanguageTaggedString { value : String , language : String } ,
value : String ,
language : LanguageTag ,
} ,
Boolean ( bool ) ,
Boolean ( bool ) ,
Float ( OrderedFloat < f32 > ) ,
Float ( OrderedFloat < f32 > ) ,
Double ( OrderedFloat < f64 > ) ,
Double ( OrderedFloat < f64 > ) ,
@ -56,10 +51,7 @@ enum LiteralContent {
NaiveTime ( NaiveTime ) ,
NaiveTime ( NaiveTime ) ,
DateTime ( DateTime < FixedOffset > ) ,
DateTime ( DateTime < FixedOffset > ) ,
NaiveDateTime ( NaiveDateTime ) ,
NaiveDateTime ( NaiveDateTime ) ,
TypedLiteral {
TypedLiteral { value : String , datatype : NamedNode } ,
value : String ,
datatype : NamedNode ,
} ,
}
}
impl Literal {
impl Literal {
@ -139,11 +131,16 @@ impl Literal {
/// Builds a RDF [language-tagged string](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string)
/// Builds a RDF [language-tagged string](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string)
pub fn new_language_tagged_literal (
pub fn new_language_tagged_literal (
value : impl Into < String > ,
value : impl Into < String > ,
language : impl Into < LanguageTa g> ,
language : impl Into < Strin g> ,
) -> Self {
) -> Self {
let language = language . into ( ) ;
Literal ( LiteralContent ::LanguageTaggedString {
Literal ( LiteralContent ::LanguageTaggedString {
value : value . into ( ) ,
value : value . into ( ) ,
language : language . into ( ) ,
language : if language . bytes ( ) . all ( | c | c . is_ascii_lowercase ( ) ) {
language
} else {
language . to_ascii_lowercase ( )
} ,
} )
} )
}
}
@ -166,8 +163,10 @@ impl Literal {
}
}
/// The literal [language tag](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tag) if it is a [language-tagged string](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string).
/// The literal [language tag](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tag) if it is a [language-tagged string](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string).
///
/// Language tags are defined by the [BCP47](https://tools.ietf.org/html/bcp47).
/// Language tags are defined by the [BCP47](https://tools.ietf.org/html/bcp47).
pub fn language ( & self ) -> Option < & LanguageTag > {
/// They are normalized to lowercase by this implementation.
pub fn language ( & self ) -> Option < & String > {
match self . 0 {
match self . 0 {
LiteralContent ::LanguageTaggedString { ref language , .. } = > Some ( language ) ,
LiteralContent ::LanguageTaggedString { ref language , .. } = > Some ( language ) ,
_ = > None ,
_ = > None ,