Fix matching on SqlState

Closes #756
This commit is contained in:
Steven Fackler 2021-04-24 10:19:23 -04:00
parent c7a8adf2df
commit 05a0643cc0
2 changed files with 852 additions and 289 deletions

View File

@ -1,5 +1,4 @@
use linked_hash_map::LinkedHashMap;
use phf_codegen;
use std::fs::File;
use std::io::{BufWriter, Write};
@ -11,7 +10,9 @@ pub fn build() {
let codes = parse_codes();
make_type(&mut file);
make_code(&codes, &mut file);
make_consts(&codes, &mut file);
make_inner(&codes, &mut file);
make_map(&codes, &mut file);
}
@ -38,26 +39,51 @@ fn make_type(file: &mut BufWriter<File>) {
write!(
file,
"// Autogenerated file - DO NOT EDIT
use std::borrow::Cow;
/// A SQLSTATE error code
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct SqlState(Cow<'static, str>);
pub struct SqlState(Inner);
impl SqlState {{
/// Creates a `SqlState` from its error code.
pub fn from_code(s: &str) -> SqlState {{
match SQLSTATE_MAP.get(s) {{
Some(state) => state.clone(),
None => SqlState(Cow::Owned(s.to_string())),
None => SqlState(Inner::Other(s.into())),
}}
}}
"
)
.unwrap();
}
fn make_code(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<File>) {
write!(
file,
r#"
/// Returns the error code corresponding to the `SqlState`.
pub fn code(&self) -> &str {{
&self.0
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,
}}
}}
"
"#
)
.unwrap();
}
@ -69,7 +95,7 @@ fn make_consts(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<
file,
r#"
/// {code}
pub const {name}: SqlState = SqlState(Cow::Borrowed("{code}"));
pub const {name}: SqlState = SqlState(Inner::E{code});
"#,
name = name,
code = code,
@ -81,6 +107,34 @@ fn make_consts(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<
write!(file, "}}").unwrap();
}
fn make_inner(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<File>) {
write!(
file,
r#"
#[derive(PartialEq, Eq, Clone, Debug)]
enum Inner {{"#,
)
.unwrap();
for code in codes.keys() {
write!(
file,
r#"
E{},"#,
code,
)
.unwrap();
}
write!(
file,
r#"
Other(Box<str>),
}}
"#,
)
.unwrap();
}
fn make_map(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<File>) {
let mut builder = phf_codegen::Map::new();
for (code, names) in codes {

File diff suppressed because it is too large Load Diff