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
// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved.
//---------------------------------------------------------------

use {
    crate::literal::this::Literal,
    std::fmt::{Display, Formatter},
};

pub struct LiteralUrlDisplay<'a> {
    pub(crate) literal: &'a Literal,
}

impl<'a> Display for LiteralUrlDisplay<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if self.literal.data_type.is_string() {
            write!(
                f,
                "{}",
                urlencoding::encode(self.literal.as_str().unwrap_or(""))
            )
        } else if self.literal.data_type.is_boolean() {
            write!(
                f,
                "{:}",
                self.literal.as_boolean().unwrap_or(false)
            )
        } else {
            write!(f, "{:}", self.literal.to_string().as_str())
        }
    }
}