Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion nemo-physical/src/datavalues/any_datavalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl DecimalType {

/// Enum that can represent arbitrary [DataValue]s.
#[derive(Debug, Clone)]
enum AnyDataValueEnum {
pub enum AnyDataValueEnum {
/// Variant for representing [DataValue]s in [ValueDomain::PlainString].
PlainString(StringDataValue),
/// Variant for representing [DataValue]s in [ValueDomain::LanguageTaggedString].
Expand Down Expand Up @@ -575,6 +575,21 @@ impl AnyDataValue {
panic!("not a null value");
}
}

/// Construct a new [AnyDataValue] from an existing
/// [AnyDataValueEnum].
///
/// This is most useful for implementing deserialization; if the
/// underlying type of the value is known, the type-specific
/// constructors should be preferred.
pub fn from_enum(value: AnyDataValueEnum) -> Self {
Self(value)
}

/// Returns the internal representation as an [AnyDataValueEnum].
pub fn into_inner(self) -> AnyDataValueEnum {
self.0
}
}

impl DataValue for AnyDataValue {
Expand Down Expand Up @@ -627,7 +642,9 @@ impl DataValue for AnyDataValue {
fn length(&self) -> Option<usize>;
fn len_unchecked(&self) -> usize;
fn tuple_element_unchecked(&self, index: usize) -> &AnyDataValue;
fn tuple_len_unchecked(&self) -> usize;
fn map_keys(&self) -> Option<Box<dyn Iterator<Item = &AnyDataValue> + '_>>;
fn map_items(&self) -> Option<Box<dyn Iterator<Item = (&AnyDataValue, &AnyDataValue)> + '_>>;
fn map_element_unchecked(&self, key: &AnyDataValue) -> &AnyDataValue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion nemo-physical/src/datavalues/boolean_datavalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct BooleanDataValue(bool);

impl BooleanDataValue {
/// Create a new [BooleanDataValue].
pub(crate) fn new(value: bool) -> Self {
pub fn new(value: bool) -> Self {
Self(value)
}
}
Expand Down
21 changes: 21 additions & 0 deletions nemo-physical/src/datavalues/datavalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,34 @@ pub trait DataValue: Debug + Display + Into<AnyDataValue> + PartialEq + Eq + Has
panic!("Value is not a tuple");
}

/// Return the length of this complex value in tuple form. For
/// values- in the domain [ValueDomain::Tuple], this is the same
/// as [len_unchecked]. For values in the domain
/// [ValueDomain::Map], this is the length of the tuple encoding,
/// i.e., twice the value of [len_unchecked] (which counts
/// key–value pairs).
///
/// # Panics
/// Panics if the value is not a collection
#[must_use]
fn tuple_len_unchecked(&self) -> usize {
self.len_unchecked()
}

/// Returns an iterator over all keys in a value that is a map.
/// None is returned for values that are no maps.
#[must_use]
fn map_keys(&self) -> Option<Box<dyn Iterator<Item = &AnyDataValue> + '_>> {
None
}

/// Returns an iterator over all key–value pairs in a value that is a map.
/// None is returned for values that are no maps.
#[must_use]
fn map_items(&self) -> Option<Box<dyn Iterator<Item = (&AnyDataValue, &AnyDataValue)> + '_>> {
None
}

/// Returns true if the value is a map that contains the given value
/// as a key. Otherwise, false is returned.
#[must_use]
Expand Down
8 changes: 4 additions & 4 deletions nemo-physical/src/datavalues/float_datavalues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ impl FloatDataValue {
/// Use the given f32 as a [FloatDataValue].
///
/// # Errors
/// The given `value` is NaN.
pub(crate) fn from_f32(value: f32) -> Result<Self, DataValueCreationError> {
/// The given `value` is NaN or infinite.
pub fn from_f32(value: f32) -> Result<Self, DataValueCreationError> {
if !value.is_finite() {
return Err(DataValueCreationError::NonFiniteFloat {});
}
Expand All @@ -28,7 +28,7 @@ impl FloatDataValue {
/// Use the given f32 as a [FloatDataValue].
///
/// # Panics
/// The given `value` is NaN.
/// The given `value` is NaN or infinite.
pub(crate) fn from_f32_unchecked(value: f32) -> Self {
if !value.is_finite() {
panic!(
Expand Down Expand Up @@ -101,7 +101,7 @@ impl DoubleDataValue {
///
/// # Errors
/// The given `value` is NaN or an infinity.
pub(crate) fn from_f64(value: f64) -> Result<Self, DataValueCreationError> {
pub fn from_f64(value: f64) -> Result<Self, DataValueCreationError> {
if !value.is_finite() {
return Err(DataValueCreationError::NonFiniteFloat {});
}
Expand Down
4 changes: 2 additions & 2 deletions nemo-physical/src/datavalues/integer_datavalues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct UnsignedLongDataValue(u64);

impl UnsignedLongDataValue {
/// Constructor.
pub(crate) fn new(value: u64) -> Self {
pub fn new(value: u64) -> Self {
UnsignedLongDataValue(value)
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ pub struct LongDataValue(i64);

impl LongDataValue {
/// Constructor.
pub(crate) fn new(value: i64) -> Self {
pub fn new(value: i64) -> Self {
LongDataValue(value)
}
}
Expand Down
2 changes: 1 addition & 1 deletion nemo-physical/src/datavalues/lang_string_datavalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl LangStringDataValue {
///
/// The langauge tag is normalized to lower case, following RDF Schema 1.1, which states that
/// "The value space of language tags is always in lower case."
pub(crate) fn new(string: String, lang_tag: String) -> Self {
pub fn new(string: String, lang_tag: String) -> Self {
LangStringDataValue(string, lang_tag.to_ascii_lowercase())
}
}
Expand Down
31 changes: 30 additions & 1 deletion nemo-physical/src/datavalues/map_datavalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub struct MapDataValue {

impl MapDataValue {
/// Constructor.
#[allow(dead_code)]
pub fn new<T: IntoIterator<Item = (AnyDataValue, AnyDataValue)>>(
label: Option<IriDataValue>,
pairs_iter: T,
Expand All @@ -32,6 +31,18 @@ impl MapDataValue {
pairs: pairs_iter.into_iter().collect(),
}
}

pub(crate) fn from_chunks<'a, T: Iterator<Item = &'a [AnyDataValue; 2]> + 'a>(
label: Option<IriDataValue>,
chunks: T,
) -> Self {
Self {
label,
pairs: chunks
.map(|item| (item[0].clone(), item[1].clone()))
.collect(),
}
}
}

impl FromIterator<(AnyDataValue, AnyDataValue)> for MapDataValue {
Expand Down Expand Up @@ -89,6 +100,10 @@ impl DataValue for MapDataValue {
Some(Box::new(self.pairs.keys()))
}

fn map_items(&self) -> Option<Box<dyn Iterator<Item = (&AnyDataValue, &AnyDataValue)> + '_>> {
Some(Box::new(self.pairs.iter()))
}

fn contains(&self, key: &AnyDataValue) -> bool {
self.pairs.contains_key(key)
}
Expand All @@ -100,6 +115,20 @@ impl DataValue for MapDataValue {
fn map_element_unchecked(&self, key: &AnyDataValue) -> &AnyDataValue {
self.pairs.get(key).expect("unchecked method")
}

fn tuple_element_unchecked(&self, index: usize) -> &AnyDataValue {
let pair = index / 2;
let (key, value) = self.pairs.iter().nth(pair).expect("unchecked method");

match index.is_multiple_of(2) {
true => key,
false => value,
}
}

fn tuple_len_unchecked(&self) -> usize {
2 * self.pairs.len()
}
}

impl std::hash::Hash for MapDataValue {
Expand Down
2 changes: 1 addition & 1 deletion nemo-physical/src/datavalues/other_datavalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct OtherDataValue(String, String);
impl OtherDataValue {
/// Constructor. We do not currently check if the datatype IRI refers to a
/// known type that is not really in [ValueDomain::Other].
pub(crate) fn new(lexical_value: String, datatype_iri: String) -> Self {
pub fn new(lexical_value: String, datatype_iri: String) -> Self {
OtherDataValue(lexical_value, datatype_iri)
}
}
Expand Down
2 changes: 1 addition & 1 deletion nemo-physical/src/datavalues/string_datavalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct StringDataValue(String);

impl StringDataValue {
/// Constructor.
pub(crate) fn new(string: String) -> Self {
pub fn new(string: String) -> Self {
StringDataValue(string)
}
}
Expand Down
1 change: 0 additions & 1 deletion nemo-physical/src/datavalues/tuple_datavalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub struct TupleDataValue {

impl TupleDataValue {
/// Constructor.
#[allow(dead_code)]
pub fn new<T: IntoIterator<Item = AnyDataValue>>(
label: Option<IriDataValue>,
values: T,
Expand Down
1 change: 1 addition & 0 deletions nemo-physical/src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) mod string_langstring_dv_dict;
pub(crate) mod null_dv_dict;
pub(crate) use null_dv_dict::NullDvDictionary;

pub(crate) mod map_dv_dict;
pub(crate) mod tuple_dv_dict;

pub mod meta_dv_dict;
Expand Down
Loading
Loading