databend_client/
settings.rs

1// Copyright 2021 Datafuse Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::error::Result;
16use crate::Error;
17use jiff::tz::TimeZone;
18use serde::Deserialize;
19use std::collections::BTreeMap;
20use std::str::FromStr;
21
22#[derive(Debug, Clone)]
23pub struct ResultFormatSettings {
24    pub geometry_output_format: GeometryDataType,
25    pub timezone: TimeZone,
26}
27
28impl Default for ResultFormatSettings {
29    fn default() -> Self {
30        Self {
31            geometry_output_format: GeometryDataType::default(),
32            timezone: TimeZone::UTC,
33        }
34    }
35}
36
37impl ResultFormatSettings {
38    pub fn from_map(settings: &Option<BTreeMap<String, String>>) -> Result<Self> {
39        match settings {
40            None => Ok(Default::default()),
41            Some(settings) => {
42                let timezone = match settings.get("timezone") {
43                    None => TimeZone::UTC,
44                    Some(t) => TimeZone::get(t).map_err(|e| Error::Decode(e.to_string()))?,
45                };
46
47                let geometry_output_format = match settings.get("geometry_output_format") {
48                    None => GeometryDataType::default(),
49                    Some(t) => {
50                        GeometryDataType::from_str(t).map_err(|e| Error::Decode(e.to_string()))?
51                    }
52                };
53
54                Ok(Self {
55                    timezone,
56                    geometry_output_format,
57                })
58            }
59        }
60    }
61}
62
63#[derive(Debug, Clone, Copy, Default, Deserialize)]
64pub enum GeometryDataType {
65    WKB,
66    WKT,
67    EWKB,
68    EWKT,
69    #[default]
70    GEOJSON,
71}
72
73impl FromStr for GeometryDataType {
74    type Err = Error;
75
76    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
77        match s.to_uppercase().as_str() {
78            "WKB" => Ok(GeometryDataType::WKB),
79            "WKT" => Ok(GeometryDataType::WKT),
80            "EWKB" => Ok(GeometryDataType::EWKB),
81            "EWKT" => Ok(GeometryDataType::EWKT),
82            "GEOJSON" => Ok(GeometryDataType::GEOJSON),
83            _ => Err(Error::Decode("Invalid geometry type format".to_string())),
84        }
85    }
86}