Skip to content

Commit 3d51754

Browse files
Merge pull request #3 from marvin-hansen/main
Code formatting, linting & script to run examples
2 parents 20a7dbe + 31e95b5 commit 3d51754

File tree

8 files changed

+104
-17
lines changed

8 files changed

+104
-17
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,31 @@ pub async fn create_stream(client: &ProtonClient) -> Result<()> {
9999
}
100100
```
101101

102-
Check more examples [here](examples).
102+
## Run the examples
103+
104+
```bash
105+
make example
106+
```
107+
108+
Which shows:
109+
110+
```text
111+
-----------------------------------------
112+
Select the number of the example to run:
113+
-----------------------------------------
114+
1) prepare: Prepare Proton for the examples: create table & load data
115+
2) query: Query Proton with sample queries
116+
3) remove: Cleanup Proton and delete streams
117+
4) quit: Exit
118+
119+
-----------------------------------------
120+
Make sure Proton is running
121+
-----------------------------------------
122+
```
123+
124+
Please select the number of the example to run. Also,
125+
first run the prepare example to prepare Proton for the query example.
126+
See the code of all examples [here](examples).
103127

104128
## What's next?
105129

examples/prepare/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ async fn main() -> Result<()> {
4545

4646
pub async fn create_stream(client: &ProtonClient) -> Result<()> {
4747
client
48-
.execute_query("CREATE STREAM IF NOT EXISTS test_stream(no uint32, name string) ORDER BY no")
48+
.execute_query(
49+
"CREATE STREAM IF NOT EXISTS test_stream(no uint32, name string) ORDER BY no",
50+
)
4951
.await
5052
}
5153

examples/remove/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ async fn main() -> Result<()> {
2222
pub async fn delete_stream(client: &ProtonClient) -> Result<()> {
2323
// Drop a stream
2424
// https://docs.timeplus.com/proton-drop-stream
25-
client.execute_query("DROP STREAM IF EXISTS test_stream").await
25+
client
26+
.execute_query("DROP STREAM IF EXISTS test_stream")
27+
.await
2628
}

makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ SHELL := /usr/bin/env bash
33

44
.PHONY: help
55
help:
6+
@echo ' make example Run the example code.'
67
@echo ' make build Builds the code base incrementally (fast) for dev.'
78
@echo ' make check Checks the code base for security vulnerabilities.'
89
@echo ' make clean Cleans generated files and folders.'
@@ -39,6 +40,11 @@ doc:
3940
@source scripts/doc.sh
4041

4142

43+
.PHONY: example
44+
example:
45+
@source scripts/example.sh
46+
47+
4248
.PHONY: fix
4349
fix:
4450
@source scripts/fix.sh

scripts/example.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# bin/sh
2+
set -o errexit
3+
set -o nounset
4+
set -o pipefail
5+
6+
# Bash Select (Make Menu) https://linuxize.com/post/bash-select/
7+
8+
echo ""
9+
echo "-----------------------------------------"
10+
echo "Select the number of the example to run: "
11+
echo "-----------------------------------------"
12+
echo "1) prepare: Prepare Proton for the examples: create table & load data"
13+
echo "2) query: Query Proton with sample queries"
14+
echo "3) remove: Cleanup Proton and delete streams"
15+
echo "4) quit: Exit"
16+
echo ""
17+
echo "-----------------------------------------"
18+
echo "Make sure Proton is running"
19+
echo "-----------------------------------------"
20+
echo ""
21+
22+
select opt in prepare query remove quit;
23+
do
24+
case $opt in
25+
26+
prepare)
27+
echo "Selected example: Prepare Proton for the examples"
28+
command cargo run --example prepare
29+
break
30+
;;
31+
32+
query)
33+
echo "Selected example: Query Proton with sample queries"
34+
command cargo run --example query
35+
break
36+
;;
37+
38+
remove)
39+
echo "Selected example: Cleanup Proton and delete streams"
40+
command cargo run --example remove
41+
break
42+
;;
43+
44+
quit)
45+
echo "Exiting!"
46+
exit 0
47+
;;
48+
49+
*)
50+
echo "Invalid option $REPLY"
51+
;;
52+
esac
53+
done

src/lib/fetch.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ impl ProtonClient {
3333
/// # Ok(()) }
3434
/// ```
3535
pub async fn fetch<T: Row>(&self, query: &str) -> Result<RowCursor<T>> {
36-
return match self.client.query(query).fetch::<T>() {
36+
match self.client.query(query).fetch::<T>() {
3737
Ok(cursor) => Ok(cursor),
3838
Err(e) => Err(ProtonClientError::FetchFailed(e.to_string())),
39-
};
39+
}
4040
}
4141

4242
/// Executes the query and returns all the generated results, collected into a Vec.
@@ -76,10 +76,10 @@ impl ProtonClient {
7676
where
7777
T: Row + for<'b> Deserialize<'b>,
7878
{
79-
return match self.client.query(query).fetch_all::<T>().await {
79+
match self.client.query(query).fetch_all::<T>().await {
8080
Ok(cursor) => Ok(cursor),
8181
Err(e) => Err(ProtonClientError::FetchAllFailed(e.to_string())),
82-
};
82+
}
8383
}
8484

8585
/// Executes the query and returns just a single row.
@@ -112,10 +112,10 @@ impl ProtonClient {
112112
where
113113
T: Row + for<'b> Deserialize<'b>,
114114
{
115-
return match self.client.query(query).fetch_one::<T>().await {
115+
match self.client.query(query).fetch_one::<T>().await {
116116
Ok(cursor) => Ok(cursor),
117117
Err(e) => Err(ProtonClientError::FetchOneFailed(e.to_string())),
118-
};
118+
}
119119
}
120120

121121
/// Executes the query and returns at most one row.
@@ -158,9 +158,9 @@ impl ProtonClient {
158158
where
159159
T: Row + for<'b> Deserialize<'b>,
160160
{
161-
return match self.client.query(query).fetch_optional::<T>().await {
161+
match self.client.query(query).fetch_optional::<T>().await {
162162
Ok(cursor) => Ok(cursor),
163163
Err(e) => Err(ProtonClientError::FetchOptionalFailed(e.to_string())),
164-
};
164+
}
165165
}
166166
}

src/lib/insert.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ impl ProtonClient {
5252
///
5353
///
5454
pub async fn insert<T: Row>(&self, table: &str) -> Result<insert::Insert<T>> {
55-
return match self.client.insert(table) {
55+
match self.client.insert(table) {
5656
Ok(insert) => Ok(insert),
5757
Err(err) => Err(ProtonClientError::InsertFailed(err.to_string())),
58-
};
58+
}
5959
}
6060
}
6161

@@ -103,9 +103,9 @@ impl ProtonClient {
103103
/// }
104104
///
105105
pub async fn inserter<T: Row>(&self, table: &str) -> Result<inserter::Inserter<T>> {
106-
return match self.client.inserter(table) {
106+
match self.client.inserter(table) {
107107
Ok(inserter) => Ok(inserter),
108108
Err(err) => Err(ProtonClientError::InserterFailed(err.to_string())),
109-
};
109+
}
110110
}
111111
}

src/lib/query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ impl ProtonClient {
3535
/// Ok(())
3636
/// }
3737
pub async fn execute_query(&self, query: &str) -> alias::Result<()> {
38-
return match self.client.query(query).execute().await {
38+
match self.client.query(query).execute().await {
3939
Ok(_) => Ok(()),
4040
Err(e) => Err(ProtonClientError::QueryFailed(e.to_string())),
41-
};
41+
}
4242
}
4343
}

0 commit comments

Comments
 (0)