perspective_client/
table_data.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 prost::bytes::Bytes;
14
15use crate::proto;
16use crate::proto::*;
17use crate::view::View;
18#[cfg(doc)]
19use crate::{Client, Table};
20
21/// The possible formats of input data which [`Client::table`] may take as an
22/// argument.
23#[derive(Debug)]
24pub enum TableData {
25    Schema(Vec<(String, ColumnType)>),
26    Update(UpdateData),
27    View(View),
28}
29
30/// The possible formats of input data which [`Table::update`] may take as an
31/// argument.
32#[derive(Debug)]
33pub enum UpdateData {
34    Csv(String),
35    Arrow(Bytes),
36    JsonRows(String),
37    JsonColumns(String),
38    Ndjson(String),
39}
40
41impl From<UpdateData> for TableData {
42    fn from(value: UpdateData) -> Self {
43        TableData::Update(value)
44    }
45}
46
47impl From<TableData> for proto::MakeTableData {
48    fn from(value: TableData) -> Self {
49        let data = match value {
50            TableData::Update(x) => return x.into(),
51            TableData::View(view) => make_table_data::Data::FromView(view.name),
52            TableData::Schema(x) => make_table_data::Data::FromSchema(proto::Schema {
53                schema: x
54                    .into_iter()
55                    .map(|(name, r#type)| schema::KeyTypePair {
56                        name,
57                        r#type: r#type as i32,
58                    })
59                    .collect(),
60            }),
61        };
62
63        MakeTableData { data: Some(data) }
64    }
65}
66
67impl From<UpdateData> for proto::MakeTableData {
68    fn from(value: UpdateData) -> Self {
69        let data = match value {
70            UpdateData::Csv(x) => make_table_data::Data::FromCsv(x),
71            UpdateData::Arrow(x) => make_table_data::Data::FromArrow(x.into()),
72            UpdateData::JsonRows(x) => make_table_data::Data::FromRows(x),
73            UpdateData::JsonColumns(x) => make_table_data::Data::FromCols(x),
74            UpdateData::Ndjson(x) => make_table_data::Data::FromNdjson(x),
75        };
76
77        MakeTableData { data: Some(data) }
78    }
79}