@@ -29,15 +29,16 @@ These steps will generate the following folder structure,
2929 └── app.rs
3030```
3131
32- {{< tabs count=3 label="Select File" >}}
33- {{< tab label="app.rs" >}}
32+ {{< tabs count=3 label="Select File" >}} {{< tab label="app.rs" >}}
33+
3434``` rust {title="crates/book_service/src/bin/app.rs"}
3535fn main () {
3636 println! (" Hello, world!" );
3737}
3838```
39- {{< /tab >}}
40- {{< tab label="Cargo.toml" >}}
39+
40+ {{< /tab >}} {{< tab label="Cargo.toml" >}}
41+
4142``` rust {title="crates/book_service/Cargo.toml"}
4243[package ]
4344name = " book_service"
@@ -51,15 +52,16 @@ path = "src/bin/app.rs"
5152
5253[dependencies ]
5354```
54- {{< /tab >}}
55- {{< tab label="Root Cargo.toml" >}}
55+
56+ {{< /tab >}} {{< tab label="Root Cargo.toml" >}}
57+
5658``` toml {title="Cargo.toml"}
5759[workspace ]
5860resolver = " 3"
5961members = [" crates/book_service" ]
6062```
61- {{< /tab >}}
62- {{< /tabs >}}
63+
64+ {{< /tab >}} {{< / tabs >}}
6365
6466> [ !tip]
6567> You should see ` Hello, world! ` while running ` cargo run -p book_service ` from the project root.
@@ -92,7 +94,7 @@ Add a new file `.gitattributes` to the project root.
9294> git commit -m " Create New Workspace"
9395> ` ` `
9496
95- # # Add `rustfmt.toml `
97+ # # Configure `rustfmt`
9698
9799Add a new file ` rustfmt.toml` to the project root.
98100
@@ -118,6 +120,48 @@ format_code_in_doc_comments = true
118120format_macro_matchers = true
119121```
120122
123+ > [ !tip]
124+ > Most above features have not been stable for years. And by default Rust makes, unstable features are only available in
125+ nightly channel. So, we need to:
126+ > - Run ` rustup component add rustfmt --toolchain nightly ` to install ` rustfmt ` on nightly toolchain.
127+ > - Then ` cargo +nightly fmt --all --check ` to run ` rustfmt ` on nightly toolchain, with unstable features.
128+
129+ ## Configure ` clippy `
130+
131+ Add the following configurations to the ` book_service/Cargo.toml ` .
132+
133+ ``` toml
134+ [lints ]
135+ workspace = true
136+ ```
137+
138+ Add the following clippy configurations to the ` Cargo.toml ` file in the project root.
139+
140+ ``` toml
141+ [workspace .lints .clippy ]
142+ doc_markdown = " warn"
143+ manual_let_else = " warn"
144+ match_same_arms = " warn"
145+ redundant_closure_for_method_calls = " warn"
146+ redundant_else = " warn"
147+ semicolon_if_nothing_returned = " warn"
148+ type_complexity = " allow"
149+ undocumented_unsafe_blocks = " warn"
150+ unwrap_or_default = " warn"
151+ needless_lifetimes = " allow"
152+ too_many_arguments = " allow"
153+ nonstandard_macro_braces = " warn"
154+ print_stdout = " warn"
155+ print_stderr = " warn"
156+
157+ ptr_as_ptr = " warn"
158+ ptr_cast_constness = " warn"
159+ ref_as_ptr = " warn"
160+
161+ allow_attributes = " warn"
162+ allow_attributes_without_reason = " warn"
163+ ```
164+
121165## Add Initial Dependency Crates
122166
123167Run following ` cargo add ` commands from the ` book_service ` folder:
@@ -158,7 +202,6 @@ Update `book_service/src/bin/app.rs`
158202
159203``` rust
160204use book_service :: routes;
161-
162205use tracing :: info;
163206use tracing_subscriber :: {EnvFilter , layer :: SubscriberExt , util :: SubscriberInitExt };
164207
@@ -187,12 +230,121 @@ async fn main() -> anyhow::Result<()> {
187230Run ` cargo run -p book_service ` and you should see our initial log ` "Starting server" ` .
188231
189232``` json
190- {"timestamp" :" 2027-01-01T00:00:00.123456Z" ,"level" :" INFO" ,"fields" :{"message" :" Starting server" ,"addr" :" 0.0.0.0:3000" }}
233+ {
234+ "timestamp" : " 2027-01-01T00:00:00.123456Z" ,
235+ "level" : " INFO" ,
236+ "fields" : {
237+ "message" : " Starting server" ,
238+ "addr" : " 0.0.0.0:3000"
239+ }
240+ }
191241```
192242
193243Run ` curl -i 'http://localhost:3000/livez' ` and you should see ` HTTP/1.1 200 OK ` in its response.
194244
195245> [ !tip]
196- > - We use [ ` tracing_subscriber::EnvFilter ` ] ( https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html ) to filter spans and events based the given set of filter directives, \
197- > ` "info,tower_http=warn,axum=warn,toasty=warn,tokio_postgres=warn" `
198- > - Change it (log level) to just ` "trace" ` , rerun the application, and trigger the ` curl ` request. Now, you can see all trace logs generated by Axum and 3rd party crates.
246+ > - We use [
247+ `tracing_subscriber::EnvFilter`](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html)
248+ to filter spans and events based the given set of filter directives, \
249+ > `"info,tower_http=warn,axum=warn,toasty=warn,tokio_postgres=warn"`
250+ > - Change it (log level) to just ` "trace" ` , rerun the application, and trigger the ` curl ` request. Now, you can see all
251+ trace logs generated by Axum and 3rd party crates.
252+
253+ ## Add Initial justfiles
254+
255+ > [ !tip]
256+ > To improve developer productivity, we use [ ` Just ` ] ( https://github.com/casey/just#installation ) , a modern command
257+ runner and alternative to Make.
258+
259+ Add a new file ` justfile ` to the project root.
260+
261+ ``` justfile
262+ # List available commands
263+ help:
264+ @just --list --unsorted --list-prefix ' ' --list-heading $'RESTful API Workspace\n'
265+ @echo ''
266+ @just --list --unsorted --list-prefix ' ' --list-heading $' BOOK SERVICE\n' --justfile crates/book_service/justfile
267+
268+ # Run lints on the workspace members (cargo fmt and clippy)
269+ lint:
270+ cargo +nightly fmt --all --check
271+ cargo clippy --workspace --all-targets -- -D warnings
272+
273+ # Run cargo check on the workspace members
274+ check:
275+ cargo check --workspace
276+
277+ # Run cargo build on the workspace members
278+ build:
279+ cargo build --workspace --all-targets
280+
281+ # Run cargo clean on the workspace members
282+ clean:
283+ cargo clean
284+
285+ # Run cargo test on the workspace members
286+ test:
287+ cargo test --workspace
288+
289+ # Forward to the BOOK-SERVICE
290+ mod book "crates/book_service"
291+ ```
292+
293+ Add a new file ` book_service/justfile ` .
294+
295+ ``` justfile
296+ export CARGO_TARGET_DIR := "../../target"
297+
298+ # List available commands
299+ help:
300+ @just --list --unsorted --list-heading $'📖BOOK-SERVICE\n'
301+
302+ # Run lints (cargo fmt and clippy)
303+ lint:
304+ cargo +nightly fmt --package book_service -- --check
305+ cargo clippy --all-targets -- -D warnings
306+
307+ # Run cargo check
308+ check:
309+ cargo check
310+
311+ # Run cargo build
312+ build:
313+ cargo build --all-targets
314+
315+ # Run cargo clean on the book_service package
316+ clean:
317+ cargo clean --package book_service
318+
319+ # Run cargo test
320+ test:
321+ cargo test
322+
323+ # Run server app
324+ app:
325+ cargo run --bin app
326+ ```
327+
328+ > [ !tip]
329+ > Run ` just ` from the project root to view all available commands.
330+ >
331+ > ``` shell
332+ > RESTful API Workspace
333+ > help # List available commands
334+ > lint # Run lints on the workspace members (cargo fmt and clippy)
335+ > check # Run cargo check on the workspace members
336+ > build # Run cargo build on the workspace members
337+ > clean # Run cargo clean on the workspace members
338+ > test # Run cargo test on the workspace members
339+ > book ... # Forward to the BOOK-SERVICE
340+ >
341+ > BOOK SERVICE
342+ > help # List available commands
343+ > lint # Run lints (cargo fmt and clippy)
344+ > check # Run cargo check
345+ > build # Run cargo build
346+ > clean # Run cargo clean on the book_service package
347+ > test # Run cargo test
348+ > app # Run server app
349+ > ` ` `
350+ > For example: ` just book lint` to run ` cargo fmt` and ` clippy` on the ` book_service` package.
0 commit comments