2018-04-22 21:55:20 +00:00
|
|
|
use linked_hash_map::LinkedHashMap;
|
2015-02-22 21:39:17 +00:00
|
|
|
use std::fs::File;
|
2018-04-22 21:55:20 +00:00
|
|
|
use std::io::{BufWriter, Write};
|
2015-02-22 21:39:17 +00:00
|
|
|
|
2018-12-10 05:23:31 +00:00
|
|
|
const ERRCODES_TXT: &str = include_str!("errcodes.txt");
|
2016-01-03 07:19:20 +00:00
|
|
|
|
2019-10-08 00:10:34 +00:00
|
|
|
pub fn build() {
|
|
|
|
let mut file = BufWriter::new(File::create("../tokio-postgres/src/error/sqlstate.rs").unwrap());
|
2015-02-22 21:39:17 +00:00
|
|
|
|
2016-01-03 07:19:20 +00:00
|
|
|
let codes = parse_codes();
|
|
|
|
|
2017-07-09 03:52:36 +00:00
|
|
|
make_type(&mut file);
|
2021-04-24 14:19:23 +00:00
|
|
|
make_code(&codes, &mut file);
|
2017-07-09 03:52:36 +00:00
|
|
|
make_consts(&codes, &mut file);
|
2021-04-24 14:19:23 +00:00
|
|
|
make_inner(&codes, &mut file);
|
2016-01-03 07:19:20 +00:00
|
|
|
make_map(&codes, &mut file);
|
2015-02-22 21:39:17 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 03:52:36 +00:00
|
|
|
fn parse_codes() -> LinkedHashMap<String, Vec<String>> {
|
|
|
|
let mut codes = LinkedHashMap::new();
|
2016-01-03 07:19:20 +00:00
|
|
|
|
|
|
|
for line in ERRCODES_TXT.lines() {
|
2018-12-10 05:23:31 +00:00
|
|
|
if line.starts_with('#') || line.starts_with("Section") || line.trim().is_empty() {
|
2016-01-03 07:19:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut it = line.split_whitespace();
|
|
|
|
let code = it.next().unwrap().to_owned();
|
|
|
|
it.next();
|
2017-07-09 03:52:36 +00:00
|
|
|
let name = it.next().unwrap().replace("ERRCODE_", "");
|
2016-01-03 07:19:20 +00:00
|
|
|
|
2017-07-09 03:52:36 +00:00
|
|
|
codes.entry(code).or_insert_with(Vec::new).push(name);
|
2016-01-03 07:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
codes
|
|
|
|
}
|
|
|
|
|
2017-07-09 03:52:36 +00:00
|
|
|
fn make_type(file: &mut BufWriter<File>) {
|
2017-07-01 03:35:17 +00:00
|
|
|
write!(
|
|
|
|
file,
|
|
|
|
"// Autogenerated file - DO NOT EDIT
|
2016-02-21 21:45:31 +00:00
|
|
|
|
2017-07-09 03:52:36 +00:00
|
|
|
/// A SQLSTATE error code
|
2016-02-12 03:56:44 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
2021-04-24 14:19:23 +00:00
|
|
|
pub struct SqlState(Inner);
|
2015-02-22 21:39:17 +00:00
|
|
|
|
|
|
|
impl SqlState {{
|
|
|
|
/// Creates a `SqlState` from its error code.
|
2016-12-19 00:11:38 +00:00
|
|
|
pub fn from_code(s: &str) -> SqlState {{
|
|
|
|
match SQLSTATE_MAP.get(s) {{
|
2015-02-22 21:39:17 +00:00
|
|
|
Some(state) => state.clone(),
|
2021-04-24 14:19:23 +00:00
|
|
|
None => SqlState(Inner::Other(s.into())),
|
2015-02-22 21:39:17 +00:00
|
|
|
}}
|
|
|
|
}}
|
2021-04-24 14:19:23 +00:00
|
|
|
"
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
2015-02-22 21:39:17 +00:00
|
|
|
|
2021-04-24 14:19:23 +00:00
|
|
|
fn make_code(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<File>) {
|
|
|
|
write!(
|
|
|
|
file,
|
|
|
|
r#"
|
2015-02-22 21:39:17 +00:00
|
|
|
/// Returns the error code corresponding to the `SqlState`.
|
|
|
|
pub fn code(&self) -> &str {{
|
2021-04-24 14:19:23 +00:00
|
|
|
match &self.0 {{"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
for code in codes.keys() {
|
|
|
|
write!(
|
|
|
|
file,
|
|
|
|
r#"
|
|
|
|
Inner::E{code} => "{code}","#,
|
|
|
|
code = code,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(
|
|
|
|
file,
|
|
|
|
r#"
|
|
|
|
Inner::Other(code) => code,
|
|
|
|
}}
|
2017-07-09 03:52:36 +00:00
|
|
|
}}
|
2021-04-24 14:19:23 +00:00
|
|
|
"#
|
2018-12-09 01:53:30 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2017-07-09 03:52:36 +00:00
|
|
|
}
|
2015-02-22 21:39:17 +00:00
|
|
|
|
2017-07-09 03:52:36 +00:00
|
|
|
fn make_consts(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<File>) {
|
|
|
|
for (code, names) in codes {
|
|
|
|
for name in names {
|
|
|
|
write!(
|
|
|
|
file,
|
|
|
|
r#"
|
2018-04-22 21:55:20 +00:00
|
|
|
/// {code}
|
2021-04-24 14:19:23 +00:00
|
|
|
pub const {name}: SqlState = SqlState(Inner::E{code});
|
2017-07-09 03:52:36 +00:00
|
|
|
"#,
|
|
|
|
name = name,
|
|
|
|
code = code,
|
2018-12-09 01:53:30 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2017-07-09 03:52:36 +00:00
|
|
|
}
|
2015-02-22 21:39:17 +00:00
|
|
|
}
|
2018-04-22 21:55:20 +00:00
|
|
|
|
|
|
|
write!(file, "}}").unwrap();
|
2017-07-09 03:52:36 +00:00
|
|
|
}
|
2015-02-22 21:39:17 +00:00
|
|
|
|
2021-04-24 14:19:23 +00:00
|
|
|
fn make_inner(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<File>) {
|
|
|
|
write!(
|
|
|
|
file,
|
|
|
|
r#"
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
2021-04-24 14:34:51 +00:00
|
|
|
#[allow(clippy::upper_case_acronyms)]
|
2021-04-24 14:19:23 +00:00
|
|
|
enum Inner {{"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
for code in codes.keys() {
|
|
|
|
write!(
|
|
|
|
file,
|
|
|
|
r#"
|
|
|
|
E{},"#,
|
|
|
|
code,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
write!(
|
|
|
|
file,
|
|
|
|
r#"
|
|
|
|
Other(Box<str>),
|
|
|
|
}}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2017-07-09 03:52:36 +00:00
|
|
|
fn make_map(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<File>) {
|
|
|
|
let mut builder = phf_codegen::Map::new();
|
|
|
|
for (code, names) in codes {
|
2018-04-22 21:55:20 +00:00
|
|
|
builder.entry(&**code, &format!("SqlState::{}", &names[0]));
|
2017-07-09 03:52:36 +00:00
|
|
|
}
|
2019-10-19 17:53:54 +00:00
|
|
|
write!(
|
|
|
|
file,
|
|
|
|
"
|
|
|
|
#[rustfmt::skip]
|
|
|
|
static SQLSTATE_MAP: phf::Map<&'static str, SqlState> = \n{};\n",
|
|
|
|
builder.build()
|
2019-10-27 21:24:25 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2015-02-22 21:39:17 +00:00
|
|
|
}
|