From 1b74977a035ff4799079dabf3559fdd10271d6a4 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 18 Jan 2014 14:09:31 -0800 Subject: [PATCH] Use a compile time table to map errors Closes #18 --- .gitmodules | 3 +++ Makefile | 19 ++++++++++++------- error.rs | 15 ++++++++++----- lib.rs | 5 ++++- submodules/rust-openssl-trigger | 1 - submodules/rust-phf | 1 + submodules/submodule-trigger | 1 + 7 files changed, 31 insertions(+), 14 deletions(-) delete mode 100644 submodules/rust-openssl-trigger create mode 160000 submodules/rust-phf create mode 100644 submodules/submodule-trigger diff --git a/.gitmodules b/.gitmodules index aca9c587..af18f1b6 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/Makefile b/Makefile index 2cde9777..c56b2afc 100644 --- a/Makefile +++ b/Makefile @@ -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) $< diff --git a/error.rs b/error.rs index 58f7364f..b92c1cff 100644 --- a/error.rs +++ b/error.rs @@ -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 = phf_map!( + $($code => $error),+ + ); + impl FromStr for PostgresSqlState { fn from_str(s: &str) -> Option { - 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()) }) } } diff --git a/lib.rs b/lib.rs index 38dbcd73..e97a78cc 100644 --- a/lib.rs +++ b/lib.rs @@ -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; diff --git a/submodules/rust-openssl-trigger b/submodules/rust-openssl-trigger deleted file mode 100644 index fa83f507..00000000 --- a/submodules/rust-openssl-trigger +++ /dev/null @@ -1 +0,0 @@ -Sat Jan 18 13:39:47 PST 2014 diff --git a/submodules/rust-phf b/submodules/rust-phf new file mode 160000 index 00000000..52ded831 --- /dev/null +++ b/submodules/rust-phf @@ -0,0 +1 @@ +Subproject commit 52ded831cfdb9ebc87e888228edc1644338aa8d6 diff --git a/submodules/submodule-trigger b/submodules/submodule-trigger new file mode 100644 index 00000000..6ab885ed --- /dev/null +++ b/submodules/submodule-trigger @@ -0,0 +1 @@ +Sat Jan 18 14:10:50 PST 2014