Skip to main content

perspective_viewer/config/
export_method.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::rc::Rc;
14
15use serde::Deserialize;
16use yew::prelude::*;
17
18use crate::js::*;
19
20#[derive(Clone, Copy, Eq, PartialEq, Deserialize)]
21pub enum ExportMethod {
22    #[serde(rename = "csv")]
23    Csv,
24
25    #[serde(rename = "csv-all")]
26    CsvAll,
27
28    #[serde(rename = "csv-selected")]
29    CsvSelected,
30
31    #[serde(rename = "json")]
32    Json,
33
34    #[serde(rename = "json-all")]
35    JsonAll,
36
37    #[serde(rename = "json-selected")]
38    JsonSelected,
39
40    // #[serde(rename = "columns")]
41    // JsonColumns,
42
43    // #[serde(rename = "columns-all")]
44    // JsonColumnsAll,
45
46    // #[serde(rename = "columns-selected")]
47    // JsonColumnsSelected,
48    #[serde(rename = "ndjson")]
49    Ndjson,
50
51    #[serde(rename = "ndjson-all")]
52    NdjsonAll,
53
54    #[serde(rename = "ndjson-selected")]
55    NdjsonSelected,
56
57    #[serde(rename = "html")]
58    Html,
59
60    #[serde(rename = "plugin")]
61    Plugin,
62
63    #[serde(rename = "arrow")]
64    Arrow,
65
66    #[serde(rename = "arrow-selected")]
67    ArrowSelected,
68
69    #[serde(rename = "arrow-all")]
70    ArrowAll,
71
72    #[serde(rename = "json-config")]
73    JsonConfig,
74}
75
76impl ExportMethod {
77    pub const fn as_filename(&self, is_chart: bool) -> &'static str {
78        match self {
79            Self::Csv => ".csv",
80            Self::CsvAll => ".all.csv",
81            Self::Json => ".json",
82            Self::JsonAll => ".all.json",
83            Self::Html => ".html",
84            Self::Plugin => {
85                if is_chart {
86                    ".png"
87                } else {
88                    ".txt"
89                }
90            },
91            Self::Arrow => ".arrow",
92            Self::ArrowAll => ".all.arrow",
93            Self::JsonConfig => ".config.json",
94            Self::CsvSelected => ".selected.csv",
95            Self::JsonSelected => ".selected.json",
96            Self::ArrowSelected => ".selected.arrow",
97            Self::Ndjson => ".ndjson",
98            Self::NdjsonAll => ".all.ndjson",
99            Self::NdjsonSelected => ".selected.ndjson",
100            // Self::JsonColumns => ".columns.json",
101            // Self::JsonColumnsAll => ".all.columns.json",
102            // Self::JsonColumnsSelected => ".selected.columns.json",
103        }
104    }
105
106    pub const fn mimetype(&self, is_chart: bool) -> MimeType {
107        match self {
108            Self::Plugin if is_chart => MimeType::ImagePng,
109            _ => MimeType::TextPlain,
110        }
111    }
112
113    pub fn to_html(&self, is_chart: bool) -> Html {
114        html! { <code>{ self.as_filename(is_chart) }</code> }
115    }
116}
117
118impl ExportMethod {
119    pub fn new_file(&self, x: &str, is_chart: bool) -> ExportFile {
120        ExportFile {
121            name: Rc::new(x.to_owned()),
122            method: *self,
123            is_chart,
124        }
125    }
126}
127
128#[derive(Clone, Eq, PartialEq)]
129pub struct ExportFile {
130    pub name: Rc<String>,
131    pub method: ExportMethod,
132    pub is_chart: bool,
133}
134
135impl ExportFile {
136    pub fn as_filename(&self, is_chart: bool) -> String {
137        format!("{}{}", self.name, self.method.as_filename(is_chart))
138    }
139}
140
141impl From<ExportFile> for Html {
142    fn from(x: ExportFile) -> Self {
143        let class = if x.name.is_empty() {
144            Some("invalid")
145        } else {
146            None
147        };
148
149        html! { <code {class}>{ x.name }{ x.method.as_filename(x.is_chart) }</code> }
150    }
151}