@ -1,4 +1,3 @@
use crate ::model ::language_tag ::LanguageTag ;
use crate ::model ::named_node ::NamedNode ;
use crate ::model ::vocab ::rdf ;
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:
/// ```
/// use rudf::model::Literal;
/// use rudf::model::LanguageTag;
/// use rudf::model::vocab::xsd;
///
/// assert_eq!(
@ -34,7 +32,7 @@ use std::option::Option;
///
/// assert_eq!(
/// "\"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) ]
@ -43,10 +41,7 @@ pub struct Literal(LiteralContent);
#[ derive(PartialEq, Eq, Ord, PartialOrd, Debug, Clone, Hash) ]
enum LiteralContent {
String ( String ) ,
LanguageTaggedString {
value : String ,
language : LanguageTag ,
} ,
LanguageTaggedString { value : String , language : String } ,
Boolean ( bool ) ,
Float ( OrderedFloat < f32 > ) ,
Double ( OrderedFloat < f64 > ) ,
@ -56,10 +51,7 @@ enum LiteralContent {
NaiveTime ( NaiveTime ) ,
DateTime ( DateTime < FixedOffset > ) ,
NaiveDateTime ( NaiveDateTime ) ,
TypedLiteral {
value : String ,
datatype : NamedNode ,
} ,
TypedLiteral { value : String , datatype : NamedNode } ,
}
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)
pub fn new_language_tagged_literal (
value : impl Into < String > ,
language : impl Into < LanguageTa g> ,
language : impl Into < Strin g> ,
) -> Self {
let language = language . into ( ) ;
Literal ( LiteralContent ::LanguageTaggedString {
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).
///
/// 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 {
LiteralContent ::LanguageTaggedString { ref language , .. } = > Some ( language ) ,
_ = > None ,