Skip to content

Commit 1277408

Browse files
committed
Update to modern KV Store API.
1 parent a648baa commit 1277408

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

Diff for: src/main.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use fastly::KVStore;
2+
use fastly::kv_store::KVStoreError;
23
use fastly::{Error, Request, Response};
34

45
#[fastly::main]
@@ -10,21 +11,21 @@ fn main(req: Request) -> Result<Response, Error> {
1011
}
1112

1213
/*
13-
Construct an KVStore instance which is connected to the KV Store named `my-store`
14+
Construct a KVStore instance which is connected to the KV Store named `my-store`
1415
1516
[Documentation for the KVStore open method can be found here](https://docs.rs/fastly/latest/fastly/struct.KVStore.html#method.open)
1617
*/
17-
let mut store = KVStore::open("my-store").map(|store| store.expect("KVStore exists"))?;
18+
let store = KVStore::open("my-store").map(|store| store.expect("KVStore exists"))?;
1819

1920
let path = req.get_path();
2021
if path == "/readme" {
21-
let entry = store.lookup("readme")?;
22+
let mut lookup = match store.lookup("readme") {
23+
Ok(l) => l,
24+
Err(KVStoreError::ItemNotFound) => return Ok(Response::from_body("Not Found").with_status(404)),
25+
Err(_e) => return Ok(Response::from_body("Lookup Error").with_status(503)),
26+
};
2227

23-
return match entry {
24-
// Stream the value back to the user-agent.
25-
Some(entry) => Ok(Response::from_body(entry)),
26-
None => Ok(Response::from_body("Not Found").with_status(404)),
27-
};
28+
return Ok(Response::from_body(lookup.take_body()));
2829
} else {
2930
/*
3031
Adds or updates the key `hello` in the KV Store with the value `world`.
@@ -44,12 +45,12 @@ fn main(req: Request) -> Result<Response, Error> {
4445
4546
[Documentation for the lookup method can be found here](https://docs.rs/fastly/latest/fastly/struct.KVStore.html#method.lookup)
4647
*/
47-
let entry = store.lookup("hello")?;
48+
let mut lookup = match store.lookup("hello") {
49+
Ok(l) => l,
50+
Err(KVStoreError::ItemNotFound) => return Ok(Response::from_body("Not Found").with_status(404)),
51+
Err(_e) => return Ok(Response::from_body("Lookup Error").with_status(503)),
52+
};
4853

49-
return match entry {
50-
// Stream the value back to the user-agent.
51-
Some(entry) => Ok(Response::from_body(entry)),
52-
None => Ok(Response::from_body("Not Found").with_status(404)),
53-
};
54+
return Ok(Response::from_body(lookup.take_body()));
5455
}
5556
}

0 commit comments

Comments
 (0)