1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved.
//---------------------------------------------------------------

use {
    iref::{Iri, IriBuf},
    std::mem::ManuallyDrop,
};

/// A `LiteralValue` is, together with a [`DataType`](crate::DataType), part
/// of a [`Literal`](crate::Literal).
pub union LiteralValue {
    pub iri:              ManuallyDrop<IriBuf>,
    pub string:           ManuallyDrop<String>,
    pub boolean:          bool,
    pub unsigned_integer: u64,
    pub signed_integer:   i64,
    pub date:             ManuallyDrop<chrono::NaiveDate>,
    pub date_time:        chrono::DateTime<chrono::Utc>,
    pub blank_node:       ManuallyDrop<String>,
}

impl Default for LiteralValue {
    fn default() -> Self { Self { boolean: false } }
}

impl LiteralValue {
    pub fn new_string(str: &str) -> Self { Self { string: ManuallyDrop::new(str.to_string()) } }

    pub fn new_iri(iri: &Iri) -> Self { Self { iri: ManuallyDrop::new(IriBuf::from(iri)) } }

    pub fn new_boolean(boolean: bool) -> Self { Self { boolean } }

    pub fn new_unsigned_integer(unsigned_integer: u64) -> Self { Self { unsigned_integer } }

    pub fn new_signed_integer(signed_integer: i64) -> Self { Self { signed_integer } }

    pub fn new_date(date: chrono::NaiveDate) -> Self { Self { date: ManuallyDrop::new(date) } }

    pub fn new_date_time(date_time: chrono::DateTime<chrono::Utc>) -> Self { Self { date_time } }

    pub fn new_blank_node(blank_node: &str) -> Self {
        Self {
            blank_node: ManuallyDrop::new(blank_node.to_string()),
        }
    }
}