forked from Wulf/dsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
136 lines (116 loc) · 3.85 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use dsync::{GenerationConfig, TableOptions};
use std::collections::HashMap;
use std::path::PathBuf;
use structopt::StructOpt;
const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
#[allow(dead_code)]
#[derive(Debug, StructOpt, Clone)]
#[structopt(about = DESCRIPTION)]
struct Args {
/// Input file
#[structopt(
short = "i",
long = "input",
help = "Required; rust file to read diesel schema information from",
required = true
)]
input: PathBuf,
/// Output file, stdout if not present
#[structopt(
parse(from_os_str),
short = "o",
long = "output",
help = "Required; directory to write generated code to"
)]
output: PathBuf,
#[cfg(feature = "tsync")]
#[structopt(
long = "tsync",
help = "Optional: adds the #[tsync] attribute to all structs; see https://github.com/Wulf/tsync"
)]
tsync: bool,
#[cfg(feature = "async")]
#[structopt(
long = "async",
help = "Optional: uses diesel_async for generated functions; see https://github.com/weiznich/diesel_async"
)]
use_async: bool,
#[structopt(
short = "g",
long = "autogenerated-columns",
help = "Optional; List of columns which are automatically generated but are not primary keys (for example: `created_at`, `updated_at`, etc.)"
)]
autogenerated_columns: Option<Vec<String>>,
#[structopt(
short = "c",
long = "connection-type",
help = "Required: rust type which describes a connection, for example: `diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>`"
)]
connection_type: String,
#[structopt(
long = "schema-path",
help = "Optional; Set custom schema use path, Default \"crate::schema::\""
)]
schema_path: Option<String>,
#[structopt(
long = "model-path",
help = "Optional; Set custom model use path, Default \"crate::models::\""
)]
model_path: Option<String>,
#[structopt(
long = "no-serde",
help = "Optional; Disable generating serde implementations"
)]
no_serde: bool,
}
fn main() {
let res = actual_main();
if let Err(err) = res {
eprintln!("Error:\n{err}");
#[cfg(feature = "backtrace")]
{
let backtrace = err.backtrace().to_string();
if backtrace == "disabled backtrace" {
eprintln!(
"note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace"
);
} else {
eprintln!("{}", backtrace);
}
}
#[cfg(not(feature = "backtrace"))]
{
eprintln!("backtrace support is disabled, enable feature \"backtrace\"");
}
std::process::exit(1);
}
}
fn actual_main() -> dsync::Result<()> {
let args: Args = Args::from_args();
let cols = args.autogenerated_columns.unwrap_or_default();
let mut default_table_options = TableOptions::default()
.autogenerated_columns(cols.iter().map(|t| t.as_str()).collect::<Vec<&str>>());
#[cfg(feature = "tsync")]
if args.tsync {
default_table_options = default_table_options.tsync();
}
#[cfg(feature = "async")]
if args.use_async {
default_table_options = default_table_options.use_async();
}
if args.no_serde {
default_table_options = default_table_options.disable_serde();
}
dsync::generate_files(
args.input,
args.output,
GenerationConfig {
default_table_options,
table_options: HashMap::from([]),
connection_type: args.connection_type,
schema_path: args.schema_path.unwrap_or("crate::schema::".to_owned()),
model_path: args.model_path.unwrap_or("crate::models::".to_owned()),
},
)?;
Ok(())
}