Skip to content

Commit 1b74977

Browse files
committed
Use a compile time table to map errors
Closes rust-postgres#18
1 parent 96221c8 commit 1b74977

7 files changed

Lines changed: 30 additions & 13 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "submodules/rust-openssl"]
22
path = submodules/rust-openssl
33
url = git://github.com/sfackler/rust-openssl
4+
[submodule "submodules/rust-phf"]
5+
path = submodules/rust-phf
6+
url = git://github.com/sfackler/rust-phf

Makefile

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ POSTGRES = $(BUILDDIR)/$(shell $(RUSTC) --crate-file-name $(POSTGRES_LIB))
77
POSTGRES_TEST = $(BUILDDIR)/$(shell $(RUSTC) --test --crate-file-name $(POSTGRES_LIB))
88
OPENSSL_DIR = submodules/rust-openssl
99
OPENSSL = $(OPENSSL_DIR)/$(shell $(MAKE) -s -C $(OPENSSL_DIR) print-target)
10+
PHF_DIR = submodules/rust-phf
11+
PHF = $(PHF_DIR)/$(shell $(MAKE) -s -C $(PHF_DIR) print-targets)
1012

1113
all: $(POSTGRES)
1214

@@ -16,21 +18,24 @@ all: $(POSTGRES)
1618
$(BUILDDIR):
1719
mkdir -p $@
1820

19-
$(BUILDDIR)/rust-openssl-trigger: submodules/rust-openssl-trigger | $(BUILDDIR)
21+
$(BUILDDIR)/submodule-trigger: submodules/submodule-trigger | $(BUILDDIR)
2022
git submodule init
2123
git submodule update
2224
touch $@
2325

24-
$(OPENSSL): $(BUILDDIR)/rust-openssl-trigger | $(BUILDDIR)
26+
$(OPENSSL): $(BUILDDIR)/submodule-trigger | $(BUILDDIR)
2527
$(MAKE) -C $(OPENSSL_DIR)
2628

27-
$(POSTGRES): $(POSTGRES_LIB) $(OPENSSL) | $(BUILDDIR)
29+
$(PHF): $(BUILDDIR)/submodule-trigger | $(BUILDDIR)
30+
$(MAKE) -C $(PHF_DIR)
31+
32+
$(POSTGRES): $(POSTGRES_LIB) $(OPENSSL) $(PHF) | $(BUILDDIR)
2833
$(RUSTC) $(RUSTFLAGS) --dep-info $(@D)/postgres.d --out-dir $(@D) \
29-
-L $(dir $(OPENSSL)) $<
34+
-L $(dir $(OPENSSL)) $(foreach file,$(PHF),-L $(dir $(file))) $<
3035

31-
$(POSTGRES_TEST): $(POSTGRES_LIB) $(OPENSSL) | $(BUILDDIR)
36+
$(POSTGRES_TEST): $(POSTGRES_LIB) $(OPENSSL) $(PHF) | $(BUILDDIR)
3237
$(RUSTC) $(RUSTFLAGS) --dep-info $(@D)/postgres_test.d --out-dir $(@D) \
33-
-L $(dir $(OPENSSL)) --test $<
38+
-L $(dir $(OPENSSL)) $(foreach file,$(PHF),-L $(dir $(file))) --test $<
3439

3540
check: $(POSTGRES_TEST)
3641
$<

error.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@
33
use std::hashmap::HashMap;
44

55
use openssl::ssl::error::SslError;
6+
use phf::PhfMap;
67

78
macro_rules! make_errors(
8-
($($code:pat => $error:ident),+) => (
9+
($($code:expr => $error:ident),+) => (
910
/// SQLSTATE error codes
10-
#[deriving(ToStr, Eq)]
11+
#[deriving(ToStr, Eq, Clone)]
1112
#[allow(missing_doc)]
1213
pub enum PostgresSqlState {
1314
$($error,)+
1415
UnknownSqlState(~str)
1516
}
1617

18+
static STATE_MAP: PhfMap<PostgresSqlState> = phf_map!(
19+
$($code => $error),+
20+
);
21+
1722
impl FromStr for PostgresSqlState {
1823
fn from_str(s: &str) -> Option<PostgresSqlState> {
19-
Some(match s {
20-
$($code => $error,)+
21-
state => UnknownSqlState(state.to_owned())
24+
Some(match STATE_MAP.find_str(&s) {
25+
Some(state) => state.clone(),
26+
None => UnknownSqlState(s.to_owned())
2227
})
2328
}
2429
}

lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ fn main() {
6161

6262
#[warn(missing_doc)];
6363

64-
#[feature(macro_rules, struct_variant, globs)];
64+
#[feature(macro_rules, struct_variant, globs, phase)];
6565
#[macro_escape];
6666

6767
extern mod extra;
6868
extern mod openssl;
69+
#[phase(syntax)]
70+
extern mod phf_mac;
71+
extern mod phf;
6972

7073
use extra::container::Deque;
7174
use extra::hex::ToHex;

submodules/rust-openssl-trigger

Lines changed: 0 additions & 1 deletion
This file was deleted.

submodules/rust-phf

Submodule rust-phf added at 52ded83

submodules/submodule-trigger

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sat Jan 18 14:10:50 PST 2014

0 commit comments

Comments
 (0)