pub mod dashboard;
pub mod diff;
pub mod erd;
pub mod explain;
pub mod relations;
pub mod sandbox;
pub mod table;
use crate::studio::config::StudioConfig;
use ruprizzle_core::ir::{Field, FieldKind, Provider, Schema};
#[must_use]
pub const fn provider_label(provider: Provider) -> &'static str {
match provider {
Provider::Postgres => "PostgreSQL",
Provider::Sqlite => "SQLite",
Provider::Mysql => "MySQL",
}
}
#[must_use]
pub fn field_type_label(field: &Field) -> String {
fn kind_label(kind: &FieldKind) -> String {
match kind {
FieldKind::Scalar(ty) => ty.as_str().to_string(),
FieldKind::Enum(name) => name.to_string(),
FieldKind::Relation(r) => r.target.to_string(),
FieldKind::List(inner) => format!("{}[]", kind_label(inner)),
}
}
let mut label = kind_label(&field.kind);
if field.optional {
label.push('?');
}
label
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct ModelNav {
pub name: String,
pub field_count: usize,
}
#[derive(Clone)]
pub struct AppState {
pub schema: Schema,
pub config: StudioConfig,
#[allow(dead_code)]
pub pool: Option<ruprizzle::Pool>,
pub models: Vec<ModelNav>,
pub provider: &'static str,
pub session_token: String,
}
impl AppState {
pub fn new(schema: Schema, config: StudioConfig, pool: Option<ruprizzle::Pool>) -> Self {
let models = schema
.models
.values()
.map(|m| ModelNav {
name: m.name.to_string(),
field_count: m.scalar_fields().count(),
})
.collect();
let provider = provider_label(schema.datasource.provider);
let session_token = config
.auth_token
.clone()
.filter(|t| !t.trim().is_empty())
.unwrap_or_else(crate::studio::guard::new_session_token);
Self {
schema,
config,
pool,
models,
provider,
session_token,
}
}
}