complete redesign oh how client is displaying data
This commit is contained in:
@@ -4,6 +4,7 @@ use crate::services::grpc_client::GrpcClient;
|
|||||||
use crate::state::pages::canvas_state::CanvasState;
|
use crate::state::pages::canvas_state::CanvasState;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use crate::state::pages::auth::RegisterState;
|
use crate::state::pages::auth::RegisterState;
|
||||||
|
use crate::state::app::state::AppState;
|
||||||
use crate::tui::functions::common::form::{revert, save};
|
use crate::tui::functions::common::form::{revert, save};
|
||||||
use crossterm::event::{KeyCode, KeyEvent};
|
use crossterm::event::{KeyCode, KeyEvent};
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
@@ -13,6 +14,7 @@ pub async fn execute_common_action<S: CanvasState + Any>(
|
|||||||
action: &str,
|
action: &str,
|
||||||
state: &mut S,
|
state: &mut S,
|
||||||
grpc_client: &mut GrpcClient,
|
grpc_client: &mut GrpcClient,
|
||||||
|
app_state: &AppState,
|
||||||
current_position: &mut u64,
|
current_position: &mut u64,
|
||||||
total_count: u64,
|
total_count: u64,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
@@ -27,6 +29,7 @@ pub async fn execute_common_action<S: CanvasState + Any>(
|
|||||||
match action {
|
match action {
|
||||||
"save" => {
|
"save" => {
|
||||||
let outcome = save(
|
let outcome = save(
|
||||||
|
app_state,
|
||||||
form_state,
|
form_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use crate::state::pages::canvas_state::CanvasState;
|
use crate::state::pages::canvas_state::CanvasState;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
|
use crate::state::app::state::AppState;
|
||||||
use crate::tui::functions::common::form::{revert, save};
|
use crate::tui::functions::common::form::{revert, save};
|
||||||
use crate::tui::functions::common::form::SaveOutcome;
|
use crate::tui::functions::common::form::SaveOutcome;
|
||||||
use crate::modes::handlers::event::EventOutcome;
|
use crate::modes::handlers::event::EventOutcome;
|
||||||
@@ -14,6 +15,7 @@ pub async fn execute_common_action<S: CanvasState + Any>(
|
|||||||
action: &str,
|
action: &str,
|
||||||
state: &mut S,
|
state: &mut S,
|
||||||
grpc_client: &mut GrpcClient,
|
grpc_client: &mut GrpcClient,
|
||||||
|
app_state: &AppState,
|
||||||
) -> Result<EventOutcome> {
|
) -> Result<EventOutcome> {
|
||||||
match action {
|
match action {
|
||||||
"save" | "revert" => {
|
"save" | "revert" => {
|
||||||
@@ -26,6 +28,7 @@ pub async fn execute_common_action<S: CanvasState + Any>(
|
|||||||
match action {
|
match action {
|
||||||
"save" => {
|
"save" => {
|
||||||
let save_result = save(
|
let save_result = save(
|
||||||
|
app_state,
|
||||||
form_state,
|
form_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
).await;
|
).await;
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ pub async fn handle_core_action(
|
|||||||
Ok(EventOutcome::Ok(message))
|
Ok(EventOutcome::Ok(message))
|
||||||
} else {
|
} else {
|
||||||
let save_outcome = form_save(
|
let save_outcome = form_save(
|
||||||
|
app_state,
|
||||||
form_state,
|
form_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
).await.context("Register save action failed")?;
|
).await.context("Register save action failed")?;
|
||||||
@@ -52,6 +53,7 @@ pub async fn handle_core_action(
|
|||||||
login_save(auth_state, login_state, auth_client, app_state).await.context("Login save n quit action failed")?
|
login_save(auth_state, login_state, auth_client, app_state).await.context("Login save n quit action failed")?
|
||||||
} else {
|
} else {
|
||||||
let save_outcome = form_save(
|
let save_outcome = form_save(
|
||||||
|
app_state,
|
||||||
form_state,
|
form_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
).await?;
|
).await?;
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ use anyhow::Result;
|
|||||||
pub async fn handle_command_event(
|
pub async fn handle_command_event(
|
||||||
key: KeyEvent,
|
key: KeyEvent,
|
||||||
config: &Config,
|
config: &Config,
|
||||||
app_state: &AppState,
|
app_state: &mut AppState,
|
||||||
login_state: &LoginState,
|
login_state: &LoginState,
|
||||||
register_state: &RegisterState,
|
register_state: &RegisterState,
|
||||||
form_state: &mut FormState,
|
form_state: &mut FormState,
|
||||||
@@ -74,7 +74,7 @@ pub async fn handle_command_event(
|
|||||||
async fn process_command(
|
async fn process_command(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
form_state: &mut FormState,
|
form_state: &mut FormState,
|
||||||
app_state: &AppState,
|
app_state: &mut AppState,
|
||||||
login_state: &LoginState,
|
login_state: &LoginState,
|
||||||
register_state: &RegisterState,
|
register_state: &RegisterState,
|
||||||
command_input: &mut String,
|
command_input: &mut String,
|
||||||
@@ -117,6 +117,7 @@ async fn process_command(
|
|||||||
},
|
},
|
||||||
"save" => {
|
"save" => {
|
||||||
let outcome = save(
|
let outcome = save(
|
||||||
|
app_state,
|
||||||
form_state,
|
form_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
).await?;
|
).await?;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
// src/services/grpc_client.rs
|
// src/services/grpc_client.rs
|
||||||
|
|
||||||
use tonic::transport::Channel;
|
use common::proto::multieko2::common::Empty;
|
||||||
use common::proto::multieko2::common::{CountResponse, Empty};
|
|
||||||
use common::proto::multieko2::table_structure::table_structure_service_client::TableStructureServiceClient;
|
use common::proto::multieko2::table_structure::table_structure_service_client::TableStructureServiceClient;
|
||||||
use common::proto::multieko2::table_structure::{GetTableStructureRequest, TableStructureResponse};
|
use common::proto::multieko2::table_structure::{GetTableStructureRequest, TableStructureResponse};
|
||||||
use common::proto::multieko2::table_definition::{
|
use common::proto::multieko2::table_definition::{
|
||||||
@@ -25,6 +24,7 @@ use common::proto::multieko2::search::{
|
|||||||
};
|
};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use tonic::transport::Channel;
|
||||||
use prost_types::Value;
|
use prost_types::Value;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -155,18 +155,14 @@ pub async fn get_table_data_by_position(
|
|||||||
Ok(response.into_inner())
|
Ok(response.into_inner())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn post_table_data(
|
pub async fn post_table_data(
|
||||||
&mut self,
|
&mut self,
|
||||||
profile_name: String,
|
profile_name: String,
|
||||||
table_name: String,
|
table_name: String,
|
||||||
data: HashMap<String, String>,
|
// CHANGE THIS: Accept the pre-converted data
|
||||||
|
data: HashMap<String, Value>,
|
||||||
) -> Result<PostTableDataResponse> {
|
) -> Result<PostTableDataResponse> {
|
||||||
// 2. CONVERT THE HASHMAP
|
// The conversion logic is now gone from here.
|
||||||
let data: HashMap<String, Value> = data
|
|
||||||
.into_iter()
|
|
||||||
.map(|(k, v)| (k, Value::from(v)))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let grpc_request = PostTableDataRequest {
|
let grpc_request = PostTableDataRequest {
|
||||||
profile_name,
|
profile_name,
|
||||||
table_name,
|
table_name,
|
||||||
@@ -186,14 +182,10 @@ pub async fn post_table_data(
|
|||||||
profile_name: String,
|
profile_name: String,
|
||||||
table_name: String,
|
table_name: String,
|
||||||
id: i64,
|
id: i64,
|
||||||
data: HashMap<String, String>,
|
// CHANGE THIS: Accept the pre-converted data
|
||||||
|
data: HashMap<String, Value>,
|
||||||
) -> Result<PutTableDataResponse> {
|
) -> Result<PutTableDataResponse> {
|
||||||
// 2. CONVERT THE HASHMAP
|
// The conversion logic is now gone from here.
|
||||||
let data: HashMap<String, Value> = data
|
|
||||||
.into_iter()
|
|
||||||
.map(|(k, v)| (k, Value::from(v)))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let grpc_request = PutTableDataRequest {
|
let grpc_request = PutTableDataRequest {
|
||||||
profile_name,
|
profile_name,
|
||||||
table_name,
|
table_name,
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use crate::state::pages::add_logic::AddLogicState;
|
|||||||
use crate::state::app::state::AppState;
|
use crate::state::app::state::AppState;
|
||||||
use crate::utils::columns::filter_user_columns;
|
use crate::utils::columns::filter_user_columns;
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub struct UiService;
|
pub struct UiService;
|
||||||
|
|
||||||
@@ -102,7 +103,6 @@ impl UiService {
|
|||||||
.context("Failed to get profile tree")?;
|
.context("Failed to get profile tree")?;
|
||||||
app_state.profile_tree = profile_tree;
|
app_state.profile_tree = profile_tree;
|
||||||
|
|
||||||
// Determine initial table to load (e.g., first table of first profile, or a default)
|
|
||||||
let initial_profile_name = app_state
|
let initial_profile_name = app_state
|
||||||
.profile_tree
|
.profile_tree
|
||||||
.profiles
|
.profiles
|
||||||
@@ -115,7 +115,7 @@ impl UiService {
|
|||||||
.profiles
|
.profiles
|
||||||
.first()
|
.first()
|
||||||
.and_then(|p| p.tables.first().map(|t| t.name.clone()))
|
.and_then(|p| p.tables.first().map(|t| t.name.clone()))
|
||||||
.unwrap_or_else(|| "2025_company_data1".to_string()); // Fallback if no tables
|
.unwrap_or_else(|| "2025_company_data1".to_string());
|
||||||
|
|
||||||
app_state.set_current_view_table(
|
app_state.set_current_view_table(
|
||||||
initial_profile_name.clone(),
|
initial_profile_name.clone(),
|
||||||
@@ -133,6 +133,15 @@ impl UiService {
|
|||||||
initial_profile_name, initial_table_name
|
initial_profile_name, initial_table_name
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
|
// NEW: Populate the "Rulebook" cache
|
||||||
|
let cache_key = format!(
|
||||||
|
"{}.{}",
|
||||||
|
initial_profile_name, initial_table_name
|
||||||
|
);
|
||||||
|
app_state
|
||||||
|
.schema_cache
|
||||||
|
.insert(cache_key, Arc::new(table_structure.clone()));
|
||||||
|
|
||||||
let column_names: Vec<String> = table_structure
|
let column_names: Vec<String> = table_structure
|
||||||
.columns
|
.columns
|
||||||
.iter()
|
.iter()
|
||||||
|
|||||||
@@ -1,15 +1,19 @@
|
|||||||
// src/state/app/state.rs
|
// src/state/app/state.rs
|
||||||
|
|
||||||
use std::env;
|
|
||||||
use common::proto::multieko2::table_definition::ProfileTreeResponse;
|
|
||||||
use crate::modes::handlers::mode_manager::AppMode;
|
|
||||||
use crate::ui::handlers::context::DialogPurpose;
|
|
||||||
use crate::state::app::search::SearchState; // ADDED
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use common::proto::multieko2::table_definition::ProfileTreeResponse;
|
||||||
|
// NEW: Import the types we need for the cache
|
||||||
|
use common::proto::multieko2::table_structure::TableStructureResponse;
|
||||||
|
use crate::modes::handlers::mode_manager::AppMode;
|
||||||
|
use crate::state::app::search::SearchState;
|
||||||
|
use crate::ui::handlers::context::DialogPurpose;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::env;
|
||||||
|
use std::sync::Arc;
|
||||||
#[cfg(feature = "ui-debug")]
|
#[cfg(feature = "ui-debug")]
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
// --- YOUR EXISTING DIALOGSTATE IS UNTOUCHED ---
|
// --- DialogState and UiState are unchanged ---
|
||||||
pub struct DialogState {
|
pub struct DialogState {
|
||||||
pub dialog_show: bool,
|
pub dialog_show: bool,
|
||||||
pub dialog_title: String,
|
pub dialog_title: String,
|
||||||
@@ -30,7 +34,7 @@ pub struct UiState {
|
|||||||
pub show_form: bool,
|
pub show_form: bool,
|
||||||
pub show_login: bool,
|
pub show_login: bool,
|
||||||
pub show_register: bool,
|
pub show_register: bool,
|
||||||
pub show_search_palette: bool, // ADDED
|
pub show_search_palette: bool,
|
||||||
pub focus_outside_canvas: bool,
|
pub focus_outside_canvas: bool,
|
||||||
pub dialog: DialogState,
|
pub dialog: DialogState,
|
||||||
}
|
}
|
||||||
@@ -52,10 +56,12 @@ pub struct AppState {
|
|||||||
pub current_view_profile_name: Option<String>,
|
pub current_view_profile_name: Option<String>,
|
||||||
pub current_view_table_name: Option<String>,
|
pub current_view_table_name: Option<String>,
|
||||||
|
|
||||||
|
// NEW: The "Rulebook" cache. We use Arc for efficient sharing.
|
||||||
|
pub schema_cache: HashMap<String, Arc<TableStructureResponse>>,
|
||||||
|
|
||||||
pub focused_button_index: usize,
|
pub focused_button_index: usize,
|
||||||
pub pending_table_structure_fetch: Option<(String, String)>,
|
pub pending_table_structure_fetch: Option<(String, String)>,
|
||||||
|
|
||||||
// ADDED: State for the search palette
|
|
||||||
pub search_state: Option<SearchState>,
|
pub search_state: Option<SearchState>,
|
||||||
|
|
||||||
// UI preferences
|
// UI preferences
|
||||||
@@ -67,9 +73,7 @@ pub struct AppState {
|
|||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
pub fn new() -> Result<Self> {
|
pub fn new() -> Result<Self> {
|
||||||
let current_dir = env::current_dir()?
|
let current_dir = env::current_dir()?.to_string_lossy().to_string();
|
||||||
.to_string_lossy()
|
|
||||||
.to_string();
|
|
||||||
Ok(AppState {
|
Ok(AppState {
|
||||||
current_dir,
|
current_dir,
|
||||||
profile_tree: ProfileTreeResponse::default(),
|
profile_tree: ProfileTreeResponse::default(),
|
||||||
@@ -77,9 +81,10 @@ impl AppState {
|
|||||||
current_view_profile_name: None,
|
current_view_profile_name: None,
|
||||||
current_view_table_name: None,
|
current_view_table_name: None,
|
||||||
current_mode: AppMode::General,
|
current_mode: AppMode::General,
|
||||||
|
schema_cache: HashMap::new(), // NEW: Initialize the cache
|
||||||
focused_button_index: 0,
|
focused_button_index: 0,
|
||||||
pending_table_structure_fetch: None,
|
pending_table_structure_fetch: None,
|
||||||
search_state: None, // ADDED
|
search_state: None,
|
||||||
ui: UiState::default(),
|
ui: UiState::default(),
|
||||||
|
|
||||||
#[cfg(feature = "ui-debug")]
|
#[cfg(feature = "ui-debug")]
|
||||||
|
|||||||
@@ -1,19 +1,22 @@
|
|||||||
// src/tui/functions/common/form.rs
|
// src/tui/functions/common/form.rs
|
||||||
|
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
|
use crate::state::app::state::AppState; // NEW: Import AppState
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use anyhow::{Context, Result}; // Added Context
|
use crate::utils::data_converter; // NEW: Import our translator
|
||||||
use std::collections::HashMap; // NEW
|
use anyhow::{anyhow, Context, Result};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum SaveOutcome {
|
pub enum SaveOutcome {
|
||||||
NoChange,
|
NoChange,
|
||||||
UpdatedExisting,
|
UpdatedExisting,
|
||||||
CreatedNew(i64), // Keep the ID
|
CreatedNew(i64),
|
||||||
}
|
}
|
||||||
|
|
||||||
// MODIFIED save function
|
// MODIFIED save function signature and logic
|
||||||
pub async fn save(
|
pub async fn save(
|
||||||
|
app_state: &AppState, // NEW: Pass in AppState
|
||||||
form_state: &mut FormState,
|
form_state: &mut FormState,
|
||||||
grpc_client: &mut GrpcClient,
|
grpc_client: &mut GrpcClient,
|
||||||
) -> Result<SaveOutcome> {
|
) -> Result<SaveOutcome> {
|
||||||
@@ -21,42 +24,64 @@ pub async fn save(
|
|||||||
return Ok(SaveOutcome::NoChange);
|
return Ok(SaveOutcome::NoChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
let data_map: HashMap<String, String> = form_state.fields.iter()
|
// --- NEW: VALIDATION & CONVERSION STEP ---
|
||||||
|
let cache_key =
|
||||||
|
format!("{}.{}", form_state.profile_name, form_state.table_name);
|
||||||
|
let schema = match app_state.schema_cache.get(&cache_key) {
|
||||||
|
Some(s) => s,
|
||||||
|
None => {
|
||||||
|
return Err(anyhow!(
|
||||||
|
"Schema for table '{}' not found in cache. Cannot save.",
|
||||||
|
form_state.table_name
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let data_map: HashMap<String, String> = form_state
|
||||||
|
.fields
|
||||||
|
.iter()
|
||||||
.zip(form_state.values.iter())
|
.zip(form_state.values.iter())
|
||||||
.map(|(field_def, value)| (field_def.data_key.clone(), value.clone()))
|
.map(|(field_def, value)| (field_def.data_key.clone(), value.clone()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
// Use our new translator. It returns a user-friendly error on failure.
|
||||||
|
let converted_data =
|
||||||
|
match data_converter::convert_and_validate_data(&data_map, schema) {
|
||||||
|
Ok(data) => data,
|
||||||
|
Err(user_error) => return Err(anyhow!(user_error)),
|
||||||
|
};
|
||||||
|
// --- END OF NEW STEP ---
|
||||||
|
|
||||||
let outcome: SaveOutcome;
|
let outcome: SaveOutcome;
|
||||||
|
let is_new_entry = form_state.id == 0
|
||||||
let is_new_entry = form_state.id == 0 || (form_state.total_count > 0 && form_state.current_position > form_state.total_count) || (form_state.total_count == 0 && form_state.current_position == 1) ;
|
|| (form_state.total_count > 0
|
||||||
|
&& form_state.current_position > form_state.total_count)
|
||||||
|
|| (form_state.total_count == 0 && form_state.current_position == 1);
|
||||||
|
|
||||||
if is_new_entry {
|
if is_new_entry {
|
||||||
let response = grpc_client
|
let response = grpc_client
|
||||||
.post_table_data(
|
.post_table_data(
|
||||||
form_state.profile_name.clone(),
|
form_state.profile_name.clone(),
|
||||||
form_state.table_name.clone(),
|
form_state.table_name.clone(),
|
||||||
data_map,
|
converted_data, // Use the validated & converted data
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.context("Failed to post new table data")?;
|
.context("Failed to post new table data")?;
|
||||||
|
|
||||||
if response.success {
|
if response.success {
|
||||||
form_state.id = response.inserted_id;
|
form_state.id = response.inserted_id;
|
||||||
// After creating a new entry, total_count increases, and current_position becomes this new total_count
|
|
||||||
form_state.total_count += 1;
|
form_state.total_count += 1;
|
||||||
form_state.current_position = form_state.total_count;
|
form_state.current_position = form_state.total_count;
|
||||||
outcome = SaveOutcome::CreatedNew(response.inserted_id);
|
outcome = SaveOutcome::CreatedNew(response.inserted_id);
|
||||||
} else {
|
} else {
|
||||||
return Err(anyhow::anyhow!(
|
return Err(anyhow!(
|
||||||
"Server failed to insert data: {}",
|
"Server failed to insert data: {}",
|
||||||
response.message
|
response.message
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// This assumes form_state.id is valid for an existing record
|
|
||||||
if form_state.id == 0 {
|
if form_state.id == 0 {
|
||||||
return Err(anyhow::anyhow!(
|
return Err(anyhow!(
|
||||||
"Cannot update record: ID is 0, but not classified as new entry."
|
"Cannot update record: ID is 0, but not classified as new entry."
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@@ -65,7 +90,7 @@ pub async fn save(
|
|||||||
form_state.profile_name.clone(),
|
form_state.profile_name.clone(),
|
||||||
form_state.table_name.clone(),
|
form_state.table_name.clone(),
|
||||||
form_state.id,
|
form_state.id,
|
||||||
data_map,
|
converted_data, // Use the validated & converted data
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.context("Failed to put (update) table data")?;
|
.context("Failed to put (update) table data")?;
|
||||||
@@ -73,7 +98,7 @@ pub async fn save(
|
|||||||
if response.success {
|
if response.success {
|
||||||
outcome = SaveOutcome::UpdatedExisting;
|
outcome = SaveOutcome::UpdatedExisting;
|
||||||
} else {
|
} else {
|
||||||
return Err(anyhow::anyhow!(
|
return Err(anyhow!(
|
||||||
"Server failed to update data: {}",
|
"Server failed to update data: {}",
|
||||||
response.message
|
response.message
|
||||||
));
|
));
|
||||||
|
|||||||
50
client/src/utils/data_converter.rs
Normal file
50
client/src/utils/data_converter.rs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
// src/utils/data_converter.rs
|
||||||
|
|
||||||
|
use common::proto::multieko2::table_structure::TableStructureResponse;
|
||||||
|
use prost_types::{value::Kind, NullValue, Value}; // Removed unused anyhow
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
pub fn convert_and_validate_data(
|
||||||
|
data: &HashMap<String, String>,
|
||||||
|
schema: &TableStructureResponse,
|
||||||
|
) -> Result<HashMap<String, Value>, String> {
|
||||||
|
let type_map: HashMap<_, _> = schema
|
||||||
|
.columns
|
||||||
|
.iter()
|
||||||
|
.map(|col| (col.name.as_str(), col.data_type.as_str()))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
data.iter()
|
||||||
|
.map(|(key, str_value)| {
|
||||||
|
let expected_type = type_map.get(key.as_str()).unwrap_or(&"TEXT");
|
||||||
|
|
||||||
|
let kind = if str_value.is_empty() {
|
||||||
|
// CHANGE THIS: Use the correct enum variant
|
||||||
|
Kind::NullValue(NullValue::NullValue.into())
|
||||||
|
} else {
|
||||||
|
// Attempt to parse the string based on the expected type
|
||||||
|
match *expected_type {
|
||||||
|
"BOOL" => match str_value.to_lowercase().parse::<bool>() {
|
||||||
|
Ok(v) => Kind::BoolValue(v),
|
||||||
|
Err(_) => return Err(format!("Invalid boolean for '{}': must be 'true' or 'false'", key)),
|
||||||
|
},
|
||||||
|
"INT8" | "INT4" | "INT2" | "SERIAL" | "BIGSERIAL" => {
|
||||||
|
match str_value.parse::<f64>() {
|
||||||
|
Ok(v) => Kind::NumberValue(v),
|
||||||
|
Err(_) => return Err(format!("Invalid number for '{}': must be a whole number", key)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"NUMERIC" | "FLOAT4" | "FLOAT8" => match str_value.parse::<f64>() {
|
||||||
|
Ok(v) => Kind::NumberValue(v),
|
||||||
|
Err(_) => return Err(format!("Invalid decimal for '{}': must be a number", key)),
|
||||||
|
},
|
||||||
|
"TIMESTAMPTZ" | "DATE" | "TIME" | "TEXT" | "VARCHAR" | "UUID" => {
|
||||||
|
Kind::StringValue(str_value.clone())
|
||||||
|
}
|
||||||
|
_ => Kind::StringValue(str_value.clone()),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok((key.clone(), Value { kind: Some(kind) }))
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
@@ -2,5 +2,8 @@
|
|||||||
|
|
||||||
pub mod columns;
|
pub mod columns;
|
||||||
pub mod debug_logger;
|
pub mod debug_logger;
|
||||||
|
pub mod data_converter;
|
||||||
|
|
||||||
pub use columns::*;
|
pub use columns::*;
|
||||||
pub use debug_logger::*;
|
pub use debug_logger::*;
|
||||||
|
pub use data_converter::*;
|
||||||
|
|||||||
Reference in New Issue
Block a user