rdf_fusion_encoding/sortable_term/
scalar.rs1use crate::TermEncoding;
2use crate::encoding::EncodingScalar;
3use crate::sortable_term::{SORTABLE_TERM_ENCODING, SortableTermEncoding};
4use datafusion::common::{DataFusionError, ScalarValue, exec_err};
5use rdf_fusion_model::DFResult;
6
7#[derive(Clone)]
9pub struct SortableTermScalar {
10 inner: ScalarValue,
11}
12
13impl SortableTermScalar {
14 pub fn try_new(value: ScalarValue) -> DFResult<Self> {
20 if value.data_type() != SORTABLE_TERM_ENCODING.data_type() {
21 return exec_err!(
22 "Expected scalar value with SortableTermEncoding, got {:?}",
23 value
24 );
25 }
26 Ok(Self::new_unchecked(value))
27 }
28
29 pub fn new_unchecked(inner: ScalarValue) -> Self {
31 Self { inner }
32 }
33}
34
35impl TryFrom<ScalarValue> for SortableTermScalar {
36 type Error = DataFusionError;
37
38 fn try_from(value: ScalarValue) -> Result<Self, Self::Error> {
39 Self::try_new(value)
40 }
41}
42
43impl EncodingScalar for SortableTermScalar {
44 type Encoding = SortableTermEncoding;
45
46 fn encoding(&self) -> &Self::Encoding {
47 &SORTABLE_TERM_ENCODING
48 }
49
50 fn scalar_value(&self) -> &ScalarValue {
51 &self.inner
52 }
53
54 fn into_scalar_value(self) -> ScalarValue {
55 self.inner
56 }
57}