datafusion_dft/tui/state/
mod.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18pub mod tabs;
19
20use crate::tui::state::tabs::sql::SQLTabState;
21use crate::tui::ui::SelectedTab;
22use log::{debug, error, info};
23use std::path::PathBuf;
24
25use self::tabs::{history::HistoryTabState, logs::LogsTabState};
26
27use crate::config::AppConfig;
28#[cfg(feature = "flightsql")]
29use crate::tui::state::tabs::flightsql::FlightSQLTabState;
30
31#[derive(Debug)]
32pub struct Tabs {
33    pub selected: SelectedTab,
34}
35
36impl Default for Tabs {
37    fn default() -> Self {
38        Self {
39            selected: SelectedTab::SQL,
40        }
41    }
42}
43
44#[derive(Debug)]
45pub struct AppState<'app> {
46    pub config: AppConfig,
47    pub should_quit: bool,
48    pub sql_tab: SQLTabState<'app>,
49    #[cfg(feature = "flightsql")]
50    pub flightsql_tab: FlightSQLTabState<'app>,
51    pub logs_tab: LogsTabState,
52    pub history_tab: HistoryTabState,
53    pub tabs: Tabs,
54}
55
56pub fn initialize<'app>(config_path: PathBuf) -> AppState<'app> {
57    debug!("Initializing state");
58    debug!("Config path: {:?}", config_path);
59    let config = if config_path.exists() {
60        debug!("Config exists");
61        let maybe_config_contents = std::fs::read_to_string(config_path);
62        if let Ok(config_contents) = maybe_config_contents {
63            let maybe_parsed_config: std::result::Result<AppConfig, toml::de::Error> =
64                toml::from_str(&config_contents);
65            match maybe_parsed_config {
66                Ok(parsed_config) => {
67                    info!("Parsed config: {:?}", parsed_config);
68                    parsed_config
69                }
70                Err(err) => {
71                    error!("Error parsing config: {:?}", err);
72                    AppConfig::default()
73                }
74            }
75        } else {
76            AppConfig::default()
77        }
78    } else {
79        debug!("No config, using default");
80        AppConfig::default()
81    };
82    AppState::new(config)
83}
84
85impl AppState<'_> {
86    pub fn new(config: AppConfig) -> Self {
87        let tabs = Tabs::default();
88
89        let sql_tab_state = SQLTabState::new(&config);
90        #[cfg(feature = "flightsql")]
91        let flightsql_tab_state = FlightSQLTabState::new(&config);
92        let logs_tab_state = LogsTabState::default();
93        let history_tab_state = HistoryTabState::default();
94
95        AppState {
96            config,
97            tabs,
98            sql_tab: sql_tab_state,
99            #[cfg(feature = "flightsql")]
100            flightsql_tab: flightsql_tab_state,
101            logs_tab: logs_tab_state,
102            history_tab: history_tab_state,
103            should_quit: false,
104        }
105    }
106}