weldscli_lib/config/
mod.rs1use crate::errors::{Result, WeldsError};
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4use welds::Syntax;
5
6mod table;
7pub use table::Table;
8
9mod column;
10pub use column::Column;
11
12pub(crate) fn read(path: &PathBuf) -> Result<Config> {
18 let yaml_str =
19 std::fs::read_to_string(path).map_err(|_| WeldsError::ReadError(path.clone()))?;
20 let config: std::result::Result<Config, serde_yaml::Error> = serde_yaml::from_str(&yaml_str);
21 match config {
22 Err(_err) => Err(WeldsError::ConfigReadError(path.clone())),
23 Ok(config) => Ok(config),
24 }
25}
26
27pub(crate) fn write(path: &PathBuf, config: &Config) -> Result<()> {
29 let yaml = serde_yaml::to_string(config).map_err(|_| WeldsError::ConfigWrite)?;
30 std::fs::write(path, yaml.as_bytes())?;
31 Ok(())
32}
33
34#[derive(Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
35pub struct Config {
36 pub tables: Vec<Table>,
37}
38
39impl Config {
40 pub(crate) fn remove_missing(&mut self, tables: &[welds::detect::TableDef]) {
41 let idents: Vec<_> = tables.iter().map(|x| x.ident()).collect();
42 self.tables
44 .retain(|x| x.manual_update || idents.contains(&&x.ident()));
45 }
46
47 pub(crate) fn add_update(&mut self, provider: DbProvider, tables: &[welds::detect::TableDef]) {
48 let mut to_add = Vec::default();
50 for t in tables {
52 let existing = self.tables.iter_mut().find(|x| &x.ident() == t.ident());
53 match existing {
54 Some(existing) => existing.update_from(t, provider),
55 None => to_add.push(Table::new(t, provider)),
56 }
57 }
58 self.tables.append(&mut to_add);
60 }
61}
62
63#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
64pub struct Relation {
65 pub schema: Option<String>, pub tablename: String, pub foreign_key: String, }
69
70impl From<&welds::detect::RelationDef> for Relation {
71 fn from(value: &welds::detect::RelationDef) -> Self {
72 Relation {
73 schema: value.other_table().schema().map(|x| x.to_owned()),
74 tablename: value.other_table().name().to_owned(),
75 foreign_key: value.foreign_key().to_owned(),
76 }
77 }
78}
79
80#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Copy)]
81pub enum DbProvider {
82 Postgres,
83 Mysql,
84 Mssql,
85 Sqlite,
86}
87
88impl From<Syntax> for DbProvider {
89 fn from(syntax: Syntax) -> Self {
90 match syntax {
91 Syntax::Mysql => DbProvider::Mysql,
92 Syntax::Postgres => DbProvider::Postgres,
93 Syntax::Mssql => DbProvider::Mssql,
94 Syntax::Sqlite => DbProvider::Sqlite,
95 }
96 }
97}
98
99impl From<DbProvider> for Syntax {
100 fn from(provider: DbProvider) -> Syntax {
101 match provider {
102 DbProvider::Mysql => Syntax::Mysql,
103 DbProvider::Postgres => Syntax::Postgres,
104 DbProvider::Mssql => Syntax::Mssql,
105 DbProvider::Sqlite => Syntax::Sqlite,
106 }
107 }
108}