forked from rust-postgres/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
163 lines (139 loc) · 4.16 KB
/
Copy pathbuild.rs
File metadata and controls
163 lines (139 loc) · 4.16 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
extern crate phf_codegen;
use std::ascii::AsciiExt;
use std::env;
use std::fs::File;
use std::io::{Write, BufWriter};
use std::path::Path;
use std::convert::AsRef;
const ERRCODES_TXT: &'static str = include_str!("errcodes.txt");
struct Code {
code: String,
variant: String,
}
fn main() {
let path = env::var_os("OUT_DIR").unwrap();
let path: &Path = path.as_ref();
let path = path.join("sqlstate.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
let codes = parse_codes();
make_enum(&codes, &mut file);
make_map(&codes, &mut file);
make_impl(&codes, &mut file);
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=errcodes.txt");
}
fn parse_codes() -> Vec<Code> {
let mut codes = vec![];
for line in ERRCODES_TXT.lines() {
if line.starts_with("#") || line.starts_with("Section") || line.trim().is_empty() {
continue;
}
let mut it = line.split_whitespace();
let code = it.next().unwrap().to_owned();
it.next();
it.next();
// for 2202E
let name = match it.next() {
Some(name) => name,
None => continue,
};
let variant = match variant_name(&code) {
Some(variant) => variant,
None => snake_to_camel(&name),
};
codes.push(Code {
code: code,
variant: variant,
});
}
codes
}
fn snake_to_camel(s: &str) -> String {
let mut out = String::new();
let mut upper = true;
for ch in s.chars() {
if ch == '_' {
upper = true;
} else {
let ch = if upper {
upper = false;
ch.to_ascii_uppercase()
} else {
ch
};
out.push(ch);
}
}
out
}
fn variant_name(code: &str) -> Option<String> {
match code {
"01004" => Some("WarningStringDataRightTruncation".to_owned()),
"22001" => Some("DataStringDataRightTruncation".to_owned()),
"2F002" => Some("SqlRoutineModifyingSqlDataNotPermitted".to_owned()),
"38002" => Some("ForeignRoutineModifyingSqlDataNotPermitted".to_owned()),
"2F003" => Some("SqlRoutineProhibitedSqlStatementAttempted".to_owned()),
"38003" => Some("ForeignRoutineProhibitedSqlStatementAttempted".to_owned()),
"2F004" => Some("SqlRoutineReadingSqlDataNotPermitted".to_owned()),
"38004" => Some("ForeignRoutineReadingSqlDataNotPermitted".to_owned()),
"22004" => Some("DataNullValueNotAllowed".to_owned()),
"39004" => Some("ExternalRoutineInvocationNullValueNotAllowed".to_owned()),
_ => None,
}
}
fn make_enum(codes: &[Code], file: &mut BufWriter<File>) {
write!(file,
r#"/// SQLSTATE error codes
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum SqlState {{
"#
).unwrap();
for code in codes {
write!(file,
" /// `{}`
{},\n",
code.code, code.variant).unwrap();
}
write!(file,
" /// An unknown code
Other(String)
}}
"
).unwrap();
}
fn make_map(codes: &[Code], file: &mut BufWriter<File>) {
write!(file, "static SQLSTATE_MAP: phf::Map<&'static str, SqlState> = ").unwrap();
let mut builder = phf_codegen::Map::new();
for code in codes {
builder.entry(&*code.code, &format!("SqlState::{}", code.variant));
}
builder.build(file).unwrap();
write!(file, ";\n").unwrap();
}
fn make_impl(codes: &[Code], file: &mut BufWriter<File>) {
write!(file, r#"
impl SqlState {{
/// Creates a `SqlState` from its error code.
pub fn from_code(s: String) -> SqlState {{
match SQLSTATE_MAP.get(&*s) {{
Some(state) => state.clone(),
None => SqlState::Other(s)
}}
}}
/// Returns the error code corresponding to the `SqlState`.
pub fn code(&self) -> &str {{
match *self {{"#
).unwrap();
for code in codes {
write!(file, r#"
SqlState::{} => "{}","#,
code.variant, code.code).unwrap();
}
write!(file, r#"
SqlState::Other(ref s) => s,
}}
}}
}}
"#
).unwrap();
}