perspective_client/config/
column_type.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use std::fmt::Display;
14use std::str::FromStr;
15
16use crate::proto::ColumnType;
17use crate::ClientError;
18
19impl Display for ColumnType {
20    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
21        write!(fmt, "{}", match self {
22            Self::String => "string",
23            Self::Integer => "integer",
24            Self::Float => "float",
25            Self::Boolean => "boolean",
26            Self::Date => "date",
27            Self::Datetime => "datetime",
28        })
29    }
30}
31
32impl FromStr for ColumnType {
33    type Err = ClientError;
34
35    fn from_str(val: &str) -> Result<Self, Self::Err> {
36        val.try_into()
37    }
38}
39
40impl TryFrom<&str> for ColumnType {
41    type Error = ClientError;
42
43    fn try_from(val: &str) -> Result<Self, Self::Error> {
44        if val == "string" {
45            Ok(Self::String)
46        } else if val == "integer" {
47            Ok(Self::Integer)
48        } else if val == "float" {
49            Ok(Self::Float)
50        } else if val == "boolean" {
51            Ok(Self::Boolean)
52        } else if val == "date" {
53            Ok(Self::Date)
54        } else if val == "datetime" {
55            Ok(Self::Datetime)
56        } else {
57            Err(ClientError::Internal(format!("Unknown type {}", val)))
58        }
59    }
60}
61
62impl ColumnType {
63    pub fn to_capitalized(&self) -> String {
64        match self {
65            ColumnType::String => "String",
66            ColumnType::Datetime => "Datetime",
67            ColumnType::Date => "Date",
68            ColumnType::Integer => "Integer",
69            ColumnType::Float => "Float",
70            ColumnType::Boolean => "Boolean",
71        }
72        .into()
73    }
74}