Use a compile time table to map errors

Closes #18
This commit is contained in:
Steven Fackler 2014-01-18 14:09:31 -08:00
parent 96221c8a47
commit 1b74977a03
7 changed files with 31 additions and 14 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "submodules/rust-openssl"]
path = submodules/rust-openssl
url = git://github.com/sfackler/rust-openssl
[submodule "submodules/rust-phf"]
path = submodules/rust-phf
url = git://github.com/sfackler/rust-phf

View File

@ -7,6 +7,8 @@ POSTGRES = $(BUILDDIR)/$(shell $(RUSTC) --crate-file-name $(POSTGRES_LIB))
POSTGRES_TEST = $(BUILDDIR)/$(shell $(RUSTC) --test --crate-file-name $(POSTGRES_LIB))
OPENSSL_DIR = submodules/rust-openssl
OPENSSL = $(OPENSSL_DIR)/$(shell $(MAKE) -s -C $(OPENSSL_DIR) print-target)
PHF_DIR = submodules/rust-phf
PHF = $(PHF_DIR)/$(shell $(MAKE) -s -C $(PHF_DIR) print-targets)
all: $(POSTGRES)
@ -16,21 +18,24 @@ all: $(POSTGRES)
$(BUILDDIR):
mkdir -p $@
$(BUILDDIR)/rust-openssl-trigger: submodules/rust-openssl-trigger | $(BUILDDIR)
$(BUILDDIR)/submodule-trigger: submodules/submodule-trigger | $(BUILDDIR)
git submodule init
git submodule update
touch $@
$(OPENSSL): $(BUILDDIR)/rust-openssl-trigger | $(BUILDDIR)
$(OPENSSL): $(BUILDDIR)/submodule-trigger | $(BUILDDIR)
$(MAKE) -C $(OPENSSL_DIR)
$(POSTGRES): $(POSTGRES_LIB) $(OPENSSL) | $(BUILDDIR)
$(RUSTC) $(RUSTFLAGS) --dep-info $(@D)/postgres.d --out-dir $(@D) \
-L $(dir $(OPENSSL)) $<
$(PHF): $(BUILDDIR)/submodule-trigger | $(BUILDDIR)
$(MAKE) -C $(PHF_DIR)
$(POSTGRES_TEST): $(POSTGRES_LIB) $(OPENSSL) | $(BUILDDIR)
$(POSTGRES): $(POSTGRES_LIB) $(OPENSSL) $(PHF) | $(BUILDDIR)
$(RUSTC) $(RUSTFLAGS) --dep-info $(@D)/postgres.d --out-dir $(@D) \
-L $(dir $(OPENSSL)) $(foreach file,$(PHF),-L $(dir $(file))) $<
$(POSTGRES_TEST): $(POSTGRES_LIB) $(OPENSSL) $(PHF) | $(BUILDDIR)
$(RUSTC) $(RUSTFLAGS) --dep-info $(@D)/postgres_test.d --out-dir $(@D) \
-L $(dir $(OPENSSL)) --test $<
-L $(dir $(OPENSSL)) $(foreach file,$(PHF),-L $(dir $(file))) --test $<
check: $(POSTGRES_TEST)
$<

View File

@ -3,22 +3,27 @@
use std::hashmap::HashMap;
use openssl::ssl::error::SslError;
use phf::PhfMap;
macro_rules! make_errors(
($($code:pat => $error:ident),+) => (
($($code:expr => $error:ident),+) => (
/// SQLSTATE error codes
#[deriving(ToStr, Eq)]
#[deriving(ToStr, Eq, Clone)]
#[allow(missing_doc)]
pub enum PostgresSqlState {
$($error,)+
UnknownSqlState(~str)
}
static STATE_MAP: PhfMap<PostgresSqlState> = phf_map!(
$($code => $error),+
);
impl FromStr for PostgresSqlState {
fn from_str(s: &str) -> Option<PostgresSqlState> {
Some(match s {
$($code => $error,)+
state => UnknownSqlState(state.to_owned())
Some(match STATE_MAP.find_str(&s) {
Some(state) => state.clone(),
None => UnknownSqlState(s.to_owned())
})
}
}

5
lib.rs
View File

@ -61,11 +61,14 @@ fn main() {
#[warn(missing_doc)];
#[feature(macro_rules, struct_variant, globs)];
#[feature(macro_rules, struct_variant, globs, phase)];
#[macro_escape];
extern mod extra;
extern mod openssl;
#[phase(syntax)]
extern mod phf_mac;
extern mod phf;
use extra::container::Deque;
use extra::hex::ToHex;

View File

@ -1 +0,0 @@
Sat Jan 18 13:39:47 PST 2014

1
submodules/rust-phf Submodule

@ -0,0 +1 @@
Subproject commit 52ded831cfdb9ebc87e888228edc1644338aa8d6

View File

@ -0,0 +1 @@
Sat Jan 18 14:10:50 PST 2014