pub enum ConcreteLiteral {
StringLiteral {
lexical_form: String,
lang: Option<Lang>,
},
DatatypeLiteral {
lexical_form: String,
datatype: IriRef,
},
NumericLiteral(NumericLiteral),
DatetimeLiteral(XsdDateTime),
BooleanLiteral(bool),
WrongDatatypeLiteral {
lexical_form: String,
datatype: IriRef,
error: String,
},
}Expand description
Concrete representation of RDF literals with type-safe internal representations.
This enum provides a strongly-typed representation of RDF literals, using native Rust types (integers, floats, booleans, etc.) internally for efficiency. It also supports literals with incorrect datatypes to enable parsing and validation of potentially malformed RDF data.
§Type Safety
The enum uses native Rust types internally, providing type safety and efficient
operations on numeric values. For example, NumericLiteral stores actual numeric
values rather than strings, enabling direct mathematical operations.
§Error Handling
The WrongDatatypeLiteral variant allows parsing
and representing malformed RDF data (e.g., "hello"^^xsd:integer) without losing
information. This enables validation workflows that need to report specific errors
while continuing to process other data.
§Comparison and Ordering
Literals implement PartialOrd and Ord following SPARQL ordering rules:
- String literals are compared lexicographically
- Numeric literals are compared by numeric value
- Boolean literals follow
false < true - Datetime literals are compared chronologically
§Panics
The Ord implementation panics when comparing incomparable literals (e.g., NaN
floating-point values or literals with different datatypes). Use PartialOrd
when comparing arbitrary literals to avoid panics.
Variants§
StringLiteral
A plain string literal, optionally with a language tag.
DatatypeLiteral
A literal with an explicit datatype IRI.
NumericLiteral(NumericLiteral)
A numeric literal (integer, float, decimal, etc.).
DatetimeLiteral(XsdDateTime)
An XSD datetime literal.
BooleanLiteral(bool)
A boolean literal (true or false).
WrongDatatypeLiteral
Represents a literal with an invalid datatype.
For example, a value like "hello"^^xsd:integer would be represented as a
WrongDatatypeLiteral. This is useful for parsing RDF data that may contain
malformed literals while still enabling validation.
Implementations§
Source§impl ConcreteLiteral
§Display and formatting
impl ConcreteLiteral
§Display and formatting
Sourcepub fn show_qualified(&self, prefixmap: &PrefixMap) -> String
pub fn show_qualified(&self, prefixmap: &PrefixMap) -> String
Returns a string representation using the given prefix map to qualify IRIs.
This method formats the literal using shortened IRI prefixes from the provided prefix map, making the output more readable.
§Arguments
prefixmap- The prefix map used to shorten IRIs
§Examples
use rudof_rdf::rdf_core::term::literal::ConcreteLiteral;
use prefixmap::PrefixMap;
let lit = ConcreteLiteral::integer(42);
let prefixmap = PrefixMap::basic();
println!("{}", lit.show_qualified(&prefixmap));Sourcepub fn display_qualified<W: Write>(
&self,
f: &mut W,
prefixmap: &PrefixMap,
) -> Result
pub fn display_qualified<W: Write>( &self, f: &mut W, prefixmap: &PrefixMap, ) -> Result
Formats this literal using the given prefix map and writes the result into the provided formatter.
The output follows RDF/Turtle-style literal syntax:
- String literals are quoted, with an optional language tag
- Numeric and boolean literals are written as-is
- Datatype literals are written using
^^and qualified IRIs - Datatypes are shortened using the provided prefix map when possible
This method is intended for internal use by [show_qualified] and
Display implementations rather than being called directly.
§Arguments
f- The output writer to which the literal representation is writtenprefixmap- The prefix map used to qualify datatype IRIs
§Errors
Returns any formatting error encountered while writing to the formatter.
Source§impl ConcreteLiteral
§Validation and Conversion
impl ConcreteLiteral
§Validation and Conversion
Sourcepub fn into_checked_literal(self) -> Result<Self, RDFError>
pub fn into_checked_literal(self) -> Result<Self, RDFError>
Validates that the lexical form matches the declared datatype, consuming the literal and returning a validated version.
This method checks whether the lexical form of a datatype literal
is compatible with its declared datatype. If the validation succeeds,
a properly typed literal is returned. If the validation fails,
a WrongDatatypeLiteral is returned instead.
For non-datatype literals, the value is returned unchanged.
§Errors
Returns an LiteralError if datatype validation fails.
§Examples
use rudof_rdf::rdf_core::term::literal::ConcreteLiteral;
use iri_s::IriS;
use prefixmap::IriRef;
// Create a datatype literal with an integer value
let dt_iri = IriRef::iri(IriS::new_unchecked("http://www.w3.org/2001/XMLSchema#integer"));
let lit = ConcreteLiteral::lit_datatype("42", &dt_iri);
// Validate the literal
let checked = lit.into_checked_literal().unwrap();Sourcepub fn match_literal(&self, literal_expected: &Self) -> bool
pub fn match_literal(&self, literal_expected: &Self) -> bool
Compares this literal with another for equality.
This method performs type-aware comparison, ensuring that literals of different types are not considered equal even if their lexical forms match.
§Arguments
literal_expected- The literal to compare against
§Returns
true if the literals are equal, false otherwise.
§Examples
use rudof_rdf::rdf_core::term::literal::ConcreteLiteral;
use rudof_rdf::rdf_core::term::literal::Lang;
let lit1 = ConcreteLiteral::str("hello");
let lit2 = ConcreteLiteral::str("hello");
let lit3 = ConcreteLiteral::lang_str("hello", Lang::new("en").unwrap());
let lit4 = ConcreteLiteral::integer(42);
// Plain string literals with the same content are equal
assert!(lit1.match_literal(&lit2));
// Language-tagged string literals must match both lexical form and lang
assert!(!lit1.match_literal(&lit3));
// Numeric and string literals are not equal even if lexical forms match
let lit5 = ConcreteLiteral::lit_datatype("42", &lit4.datatype());
assert!(!lit5.match_literal(&lit4));
// Comparing numeric literals of the same value returns true
let lit6 = ConcreteLiteral::integer(42);
assert!(lit4.match_literal(&lit6));Source§impl ConcreteLiteral
§Constructor Methods - Numeric Types
impl ConcreteLiteral
§Constructor Methods - Numeric Types
Sourcepub fn integer(n: i128) -> Self
pub fn integer(n: i128) -> Self
Creates a literal representing an unbounded integer (i128).
This corresponds to XSD’s xsd:integer type, which is unbounded.
Sourcepub fn non_negative_integer(n: u128) -> Self
pub fn non_negative_integer(n: u128) -> Self
Creates a literal representing a non-negative integer (u128 ≥ 0).
This corresponds to XSD’s xsd:nonNegativeInteger type.
Sourcepub fn non_positive_integer(n: i128) -> Result<Self, RDFError>
pub fn non_positive_integer(n: i128) -> Result<Self, RDFError>
Creates a literal representing a non-positive integer (i128 ≤ 0).
This corresponds to XSD’s xsd:nonPositiveInteger type.
§Errors
Returns RDFError::NumericOutOfRange if n is greater than 0.
Sourcepub fn positive_integer(n: u128) -> Result<Self, RDFError>
pub fn positive_integer(n: u128) -> Result<Self, RDFError>
Creates a literal representing a strictly positive integer (u128 > 0).
This corresponds to XSD’s xsd:positiveInteger type.
§Errors
Returns RDFError::NumericOutOfRange if n is 0.
Sourcepub fn negative_integer(n: i128) -> Result<Self, RDFError>
pub fn negative_integer(n: i128) -> Result<Self, RDFError>
Creates a literal representing a strictly negative integer (i128 < 0).
This corresponds to XSD’s xsd:negativeInteger type.
§Errors
Returns RDFError::NumericOutOfRange if n is greater than or equal to 0.
Sourcepub fn double(d: f64) -> Self
pub fn double(d: f64) -> Self
Creates a literal representing a double-precision floating-point number (f64).
This corresponds to XSD’s xsd:double type (64-bit IEEE 754).
Sourcepub fn decimal(d: Decimal) -> Self
pub fn decimal(d: Decimal) -> Self
Creates a literal representing a decimal number (Decimal type for precise arithmetic).
This corresponds to XSD’s xsd:decimal type for exact decimal arithmetic.
Sourcepub fn long(n: i64) -> Self
pub fn long(n: i64) -> Self
Creates a literal representing a 64-bit signed long integer (i64).
This corresponds to XSD’s xsd:long type (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).
Sourcepub fn byte(n: i8) -> Self
pub fn byte(n: i8) -> Self
Creates a literal representing a signed byte (i8), values -128 to 127.
This corresponds to XSD’s xsd:byte type.
Sourcepub fn short(n: i16) -> Self
pub fn short(n: i16) -> Self
Creates a literal representing a signed short (i16), values -32,768 to 32,767.
This corresponds to XSD’s xsd:short type.
Sourcepub fn unsigned_byte(n: u8) -> Self
pub fn unsigned_byte(n: u8) -> Self
Creates a literal representing an unsigned byte (u8), values 0–255.
This corresponds to XSD’s xsd:unsignedByte type.
Sourcepub fn unsigned_short(n: u16) -> Self
pub fn unsigned_short(n: u16) -> Self
Creates a literal representing an unsigned short (u16), values 0–65,535.
This corresponds to XSD’s xsd:unsignedShort type.
Sourcepub fn unsigned_int(n: u32) -> Self
pub fn unsigned_int(n: u32) -> Self
Creates a literal representing an unsigned integer (u32).
This corresponds to XSD’s xsd:unsignedInt type.
Sourcepub fn unsigned_long(n: u64) -> Self
pub fn unsigned_long(n: u64) -> Self
Creates a literal representing an unsigned long integer (u64).
This corresponds to XSD’s xsd:unsignedLong type.
Sourcepub fn float(n: f32) -> Self
pub fn float(n: f32) -> Self
Creates a literal representing a single-precision floating-point number (f32).
This corresponds to XSD’s xsd:float type (32-bit IEEE 754).
Sourcepub fn datetime(dt: XsdDateTime) -> Self
pub fn datetime(dt: XsdDateTime) -> Self
Creates a DatetimeLiteral
Source§impl ConcreteLiteral
§Constructor Methods - Other Types
impl ConcreteLiteral
§Constructor Methods - Other Types
Sourcepub fn lit_datatype(lexical_form: &str, datatype: &IriRef) -> Self
pub fn lit_datatype(lexical_form: &str, datatype: &IriRef) -> Self
Creates a literal with a custom datatype.
§Parameters
lexical_form: The string representation of the literal’s value.datatype: The IRI that identifies the literal’s datatype.
Source§impl ConcreteLiteral
§Accessor Methods
impl ConcreteLiteral
§Accessor Methods
Sourcepub fn lang(&self) -> Option<Lang>
pub fn lang(&self) -> Option<Lang>
Returns the language tag of the literal, if it is a language-tagged string.
Sourcepub fn lexical_form(&self) -> String
pub fn lexical_form(&self) -> String
Returns the lexical form (string representation) of the literal.
§Returns
A String representing the literal’s value:
- For string or datatype literals, returns the literal text.
- For numeric literals, returns the numeric value as a string.
- For boolean literals, returns
"true"or"false". - For datetime literals, returns a standard string representation.
Sourcepub fn datatype(&self) -> IriRef
pub fn datatype(&self) -> IriRef
Returns the datatype IRI of the literal.
§Returns
- For explicitly typed literals (
DatatypeLiteralorWrongDatatypeLiteral), returns the stored datatype IRI. - For plain string literals without a language tag, returns
xsd:string. - For language-tagged string literals, returns
rdf:langString. - For numeric literals, returns the appropriate XML Schema datatype (e.g.,
xsd:integer,xsd:double). - For boolean literals, returns
xsd:boolean. - For datetime literals, returns
xsd:dateTime.
Sourcepub fn numeric_value(&self) -> Option<NumericLiteral>
pub fn numeric_value(&self) -> Option<NumericLiteral>
Returns the numeric literal value, if this literal is numeric.
Source§impl ConcreteLiteral
§Parsing Methods
impl ConcreteLiteral
§Parsing Methods
Sourcepub fn parse_bool(s: &str) -> Result<bool, String>
pub fn parse_bool(s: &str) -> Result<bool, String>
Parses a boolean from its XSD lexical representation.
Valid values are: “true”, “false”, “1” (true), “0” (false). Parsing is case-sensitive.
§Errors
Returns an error if the input string is not a valid boolean representation.
§Examples
use rudof_rdf::rdf_core::term::literal::ConcreteLiteral;
assert_eq!(ConcreteLiteral::parse_bool("true").unwrap(), true);
assert_eq!(ConcreteLiteral::parse_bool("0").unwrap(), false);
assert!(ConcreteLiteral::parse_bool("yes").is_err());Sourcepub fn parse_integer(s: &str) -> Result<i128, String>
pub fn parse_integer(s: &str) -> Result<i128, String>
Parses an integer from its string representation.
XSD integer is unbounded, so this returns i128.
§Errors
Returns an error if the string cannot be parsed as an integer.
§Examples
use rudof_rdf::rdf_core::term::literal::ConcreteLiteral;
assert_eq!(ConcreteLiteral::parse_integer("-7").unwrap(), -7);
assert_eq!(ConcreteLiteral::parse_integer("2").unwrap(), 2);
assert!(ConcreteLiteral::parse_integer("x").is_err());Sourcepub fn parse_negative_integer(s: &str) -> Result<i128, String>
pub fn parse_negative_integer(s: &str) -> Result<i128, String>
Parses a negative integer from its string representation.
§Errors
Returns an error if the value is not negative or cannot be parsed.
§Examples
use rudof_rdf::rdf_core::term::literal::ConcreteLiteral;
assert_eq!(ConcreteLiteral::parse_negative_integer("-3").unwrap(), -3);
assert!(ConcreteLiteral::parse_negative_integer("0").is_err());Sourcepub fn parse_non_positive_integer(s: &str) -> Result<i128, String>
pub fn parse_non_positive_integer(s: &str) -> Result<i128, String>
Parses a non-positive integer from its string representation.
§Errors
Returns an error if the value is positive or cannot be parsed.
Sourcepub fn parse_positive_integer(s: &str) -> Result<u128, String>
pub fn parse_positive_integer(s: &str) -> Result<u128, String>
Parses a positive integer from its string representation.
§Errors
Returns an error if the value is not positive or cannot be parsed.
Sourcepub fn parse_non_negative_integer(s: &str) -> Result<u128, String>
pub fn parse_non_negative_integer(s: &str) -> Result<u128, String>
Parses a non-negative integer from its string representation.
§Errors
Returns an error if the string cannot be parsed as a non-negative integer.
Sourcepub fn parse_unsigned_byte(s: &str) -> Result<u8, String>
pub fn parse_unsigned_byte(s: &str) -> Result<u8, String>
Parses an unsigned byte (0–255) from its string representation.
§Errors
Returns an error if the string cannot be parsed as a u8.
Sourcepub fn parse_unsigned_short(s: &str) -> Result<u16, String>
pub fn parse_unsigned_short(s: &str) -> Result<u16, String>
Parses an unsigned short (0–65535) from its string representation.
§Errors
Returns an error if the string cannot be parsed as a u16.
Sourcepub fn parse_unsigned_int(s: &str) -> Result<u32, String>
pub fn parse_unsigned_int(s: &str) -> Result<u32, String>
Parses an unsigned integer from its string representation.
§Errors
Returns an error if the string cannot be parsed as a u32.
Sourcepub fn parse_unsigned_long(s: &str) -> Result<u64, String>
pub fn parse_unsigned_long(s: &str) -> Result<u64, String>
Parses an unsigned long (0–u64::MAX) from its string representation.
§Errors
Returns an error if the string cannot be parsed as a u64.
Sourcepub fn parse_double(s: &str) -> Result<f64, String>
pub fn parse_double(s: &str) -> Result<f64, String>
Parses a double (64-bit float) from its string representation.
§Errors
Returns an error if the string cannot be parsed as a f64.
Sourcepub fn parse_long(s: &str) -> Result<i64, String>
pub fn parse_long(s: &str) -> Result<i64, String>
Parses a long integer (64-bit signed) from its string representation.
§Errors
Returns an error if the string cannot be parsed as an i64.
Sourcepub fn parse_decimal(s: &str) -> Result<Decimal, String>
pub fn parse_decimal(s: &str) -> Result<Decimal, String>
Parses a decimal from its string representation using rust_decimal::Decimal.
§Errors
Returns an error if the string cannot be parsed as a Decimal.
Sourcepub fn parse_float(s: &str) -> Result<f32, String>
pub fn parse_float(s: &str) -> Result<f32, String>
Parses a float (32-bit) from its string representation.
§Errors
Returns an error if the string cannot be parsed as a float.
Sourcepub fn parse_byte(s: &str) -> Result<i8, String>
pub fn parse_byte(s: &str) -> Result<i8, String>
Parses a signed byte (-128 to 127) from its string representation.
§Errors
Returns an error if the string cannot be parsed as an i8.
Sourcepub fn parse_short(s: &str) -> Result<i16, String>
pub fn parse_short(s: &str) -> Result<i16, String>
Parses a signed short (-32768 to 32767) from its string representation.
§Errors
Returns an error if the string cannot be parsed as an i16.
Sourcepub fn parse_datetime(s: &str) -> Result<XsdDateTime, String>
pub fn parse_datetime(s: &str) -> Result<XsdDateTime, String>
Parses a datetime string using XsdDateTime
Trait Implementations§
Source§impl Clone for ConcreteLiteral
impl Clone for ConcreteLiteral
Source§fn clone(&self) -> ConcreteLiteral
fn clone(&self) -> ConcreteLiteral
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ConcreteLiteral
impl Debug for ConcreteLiteral
Source§impl Default for ConcreteLiteral
impl Default for ConcreteLiteral
Source§impl DerefIri for ConcreteLiteral
impl DerefIri for ConcreteLiteral
Source§fn deref_iri(
self,
base: Option<&IriS>,
prefixmap: Option<&PrefixMap>,
) -> Result<Self, DerefError>
fn deref_iri( self, base: Option<&IriS>, prefixmap: Option<&PrefixMap>, ) -> Result<Self, DerefError>
Resolves IRIs and prefixes contained in the literal.
- Value-based literals (
NumericLiteral,BooleanLiteral,DatetimeLiteral,StringLiteral) are cloned directly. - Datatype literals have their datatype IRIs dereferenced using
baseandprefixmap. - Wrong datatype literals are converted into properly typed literals.
§Errors
Returns DerefError if datatype resolution fails.
Source§impl<'de> Deserialize<'de> for ConcreteLiteral
impl<'de> Deserialize<'de> for ConcreteLiteral
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for ConcreteLiteral
impl Display for ConcreteLiteral
Source§impl From<&ConcreteLiteral> for Literal
impl From<&ConcreteLiteral> for Literal
Source§fn from(value: &ConcreteLiteral) -> Self
fn from(value: &ConcreteLiteral) -> Self
Source§impl From<ConcreteLiteral> for Literal
impl From<ConcreteLiteral> for Literal
Source§fn from(value: ConcreteLiteral) -> Self
fn from(value: ConcreteLiteral) -> Self
Converts an SLiteral into an oxrdf::Literal
Source§impl From<ConcreteLiteral> for Object
Converts a ConcreteLiteral into an Object::Literal.
impl From<ConcreteLiteral> for Object
Converts a ConcreteLiteral into an Object::Literal.
This allows literals to be seamlessly used where objects are expected.
Source§fn from(lit: ConcreteLiteral) -> Self
fn from(lit: ConcreteLiteral) -> Self
Source§impl Hash for ConcreteLiteral
impl Hash for ConcreteLiteral
Source§impl Ord for ConcreteLiteral
Total ordering for literals.
impl Ord for ConcreteLiteral
Total ordering for literals.
§Panics
This implementation panics if two literals are not comparable, such as:
- Literals with different datatypes
- Numeric literals involving
NaN
This is intended as a temporary solution to support sorting in validation workflows where such cases are not expected.
§TODO
Define a total ordering that is well-defined for all literals.
Source§impl PartialEq for ConcreteLiteral
impl PartialEq for ConcreteLiteral
Source§impl PartialOrd for ConcreteLiteral
Partial ordering for literals following SPARQL comparison semantics.
impl PartialOrd for ConcreteLiteral
Partial ordering for literals following SPARQL comparison semantics.
Comparison rules:
- String literals are compared lexicographically by their lexical form.
- Datatype literals are comparable only if they share the same datatype, and are then compared by lexical form.
- Numeric literals are compared by numeric value.
- Boolean literals follow
true > false. - Datetime literals are compared chronologically.
If two literals are not comparable under these rules, None is returned.
Source§impl Serialize for ConcreteLiteral
impl Serialize for ConcreteLiteral
Source§impl TryFrom<Literal> for ConcreteLiteral
impl TryFrom<Literal> for ConcreteLiteral
Source§fn try_from(value: Literal) -> Result<Self, Self::Error>
fn try_from(value: Literal) -> Result<Self, Self::Error>
Attempts to convert an oxrdf literal into an SLiteral.
Supported cases:
- Plain string literals
- Language-tagged string literals
- Typed literals (with datatype parsing)
§Errors
Returns an LiteralError if:
- The language tag is invalid
- The datatype is unsupported or malformed
- The literal structure is unknown
impl Eq for ConcreteLiteral
impl StructuralPartialEq for ConcreteLiteral
Auto Trait Implementations§
impl Freeze for ConcreteLiteral
impl RefUnwindSafe for ConcreteLiteral
impl Send for ConcreteLiteral
impl Sync for ConcreteLiteral
impl Unpin for ConcreteLiteral
impl UnsafeUnpin for ConcreteLiteral
impl UnwindSafe for ConcreteLiteral
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.