Skip to content

Commit d979596

Browse files
Update for IDS talk
1 parent 35cff7f commit d979596

File tree

1 file changed

+73
-138
lines changed

1 file changed

+73
-138
lines changed

presentation/slides.md

Lines changed: 73 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Prototype Project <span v-mark.circle.purple="4">("LabWhere")</span>
118118

119119
## Ways of working
120120

121-
- One session (1 - 1.5hours) a week.
121+
- One session (1 - 1½ hours) a week.
122122
- Took turns in running the session.
123123

124124
</v-click>
@@ -285,14 +285,11 @@ ul li {
285285
</style>
286286

287287
---
288-
transition: slide-up
289-
level: 2
288+
transition: slide-left
290289
---
291290

292-
# Controllers
293-
291+
# Code
294292

295-
We check the HTTP method (courtesy of `Hyper`), and the URI to forward request to the service.
296293
````md magic-move {lines: true}
297294
```rs {*|9}{lines:true}
298295
pub async fn process(
@@ -308,7 +305,7 @@ pub async fn process(
308305
```
309306
</div>
310307

311-
```rs {*|7-9|10-12|13-18|*}{lines:true}
308+
```rs {*|8}{lines:true}
312309
async fn route(
313310
req: Request<impl Body<Data = Bytes, Error = hyper::Error> + Send + Sync + 'static>,
314311
connection: &Pool<Sqlite>,
@@ -330,147 +327,85 @@ async fn route(
330327
}
331328
}
332329
```
333-
````
334-
335-
<!--
336-
Our controller is simple - a single function responsible for handling the incoming HTTP requests and dispatching the request to the handler using the route function.
337-
338-
It takes a request and a database connection pool. It returns a response with a body of bytes or an error.
339-
If we don’t recognise the request, we return a bad request response.
340-
341-
➡️ ➡️ ➡️
342-
343-
The route function dispatches the request: it receives incoming HTTP requests and decides which service should handle them.
344-
345-
346-
Requests are matched based on method + URI path:
347-
OPTIONS → handled by preflight() (CORS).
348-
POST /scan → forwarded to the scan service.
349-
POST /searches → forwarded to the search service.
350-
Any other request → returns 404 Not Found.
351-
So, the controller separates request handling from business logic, forwarding requests to services without performing the business logic itself.
352-
-->
353-
354-
---
355-
transition: slide-down
356-
---
357-
358-
# Services
359-
360-
```rust {*|1-4|6-10|*}{lines:true}
361-
pub(crate) async fn search(
330+
```rs {*|8}{lines:true}
331+
pub async fn scan(
362332
connection: &Pool<Sqlite>,
363-
request_string: String,
364-
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> { ... }
365-
366-
#[cfg(test)]
367-
mod tests {
368-
#[tokio::test]
369-
async fn test_search() {...}
333+
request: &str,
334+
) -> std::result::Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
335+
336+
// ... serialisation logic here.
337+
338+
match Scan::create(json, connection).await {
339+
Ok(scan) => {
340+
// Logic for preparing the success response here.
341+
}
342+
Err(err) => {
343+
// Logic for preparing error response here.
344+
}
345+
}
370346
}
371347
```
372-
373-
```rust {*|1-4|6-12|*}{lines:true}
374-
pub async fn scan(
348+
```rs {*|3|8|12}{lines:true}
349+
pub async fn create(scan: Scan, connection: &Pool<Sqlite>) -> Result<Scan, LabwhereError> {
350+
let location: Location =
351+
match Location::find_by_barcode(scan.location_barcode, connection).await {
352+
// ... response handling ...
353+
};
354+
// ... some validations ...
355+
for barcode in split_barcodes.iter() {
356+
match Labware::find_by_barcode(&barcode.to_string(), connection).await {
357+
Ok(mut labware) => {
358+
labware.location_id = location.id;
359+
360+
match Labware::update(&labware, connection).await {
361+
// .. return proper value or handle errors ...
362+
}
363+
}
364+
Err(error) => match error {
365+
// ... error handling
366+
},
367+
};
368+
}
369+
// ... return the correct value ...
370+
}
371+
```
372+
```rs {*}{lines:true}
373+
pub(crate) async fn update(
374+
labware: &Labware,
375375
connection: &Pool<Sqlite>,
376-
request: &str,
377-
) -> std::result::Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> { ... }
378-
379-
#[cfg(test)]
380-
mod tests {
381-
#[tokio::test]
382-
async fn test_scan() { ... }
383-
384-
#[tokio::test]
385-
async fn test_scan_without_correct_content_type() { ... }
376+
) -> Result<Labware, LabwhereError> {
377+
match sqlx::query("UPDATE labwares SET location_id = ? WHERE id = ?")
378+
.bind(labware.location_id)
379+
.bind(labware.id)
380+
.execute(connection)
381+
.await
382+
{
383+
Ok(_) => {
384+
match sqlx::query_as::<_, Location>("SELECT * FROM locations WHERE id = ?")
385+
.bind(labware.location_id)
386+
.fetch_one(connection)
387+
.await
388+
{
389+
// ... handle result ...
390+
}
391+
}
392+
Err(_) => Err(LabwhereError::database_error()),
393+
}
386394
}
387395
```
388-
389-
<!--
390-
These are the service functions.
391-
392-
Both of them take a database connection pool, and the request body.
393-
394-
They return Hyper heap-allocated body type of Bytes.
395-
396-
Search accepts ownership of the string - request body - and it can modify it.
397-
398-
Scan borrows the reference to the string - request body - but it does not modify it.
399-
-->
400-
401-
---
402-
transition: fade-out
403-
---
404-
405-
# Models
406-
407-
<div class="grid md:grid-cols-2 gap-4 dark:invert">
408-
<div>
409-
<img class="h-auto max-w-full rounded-lg" src="./models-l.png" alt="">
410-
</div>
411-
<div>
412-
<img class="h-auto max-w-full rounded-lg" style="max-height: 450px" src="./models-r.png" alt="">
413-
</div>
414-
</div>
415-
416-
<!--
417-
These are the models to access the database.
418-
419-
They are Rust structs and they have implemented functions.
420-
421-
They match the CRUD pattern, create, read, update and delete. We do not have deletions.
422-
423-
Taking create as an example: It takes barcode String, unsigned 32 bit location id, and connection pool. It returns a Result of either Labware or a LabwareError. This is a common pattern in Rust. Using Result allows the caller to handle success and failure explicitly.
424-
425-
As you see we have unit tests for each function we have implemented.
426-
427-
This concludes the overall architecture and a little introduction to code.
428-
-->
429-
430-
---
431-
transition: slide-left
432-
---
433-
434-
# Code
435-
436-
437-
Let's dive into the code.
438-
439-
Pardon the dog gifs 🐶
440-
441-
<div class="flex items-center justify-center">
442-
<div class="grid grid-cols-2 md:grid-cols-3 gap-4">
443-
<div>
444-
<img class="h-auto max-w-full rounded-lg" src="./dog-1.gif" alt="">
445-
</div>
446-
<div>
447-
<img class="h-auto max-w-full rounded-lg" src="./dog-2.webp" alt="">
448-
</div>
449-
<div>
450-
<img class="h-auto max-w-full rounded-lg" src="./dog-3.webp" alt="">
451-
</div>
452-
</div>
453-
</div>
454-
455-
<!--
456-
457-
So now, I’ll walk you through some of the code running behind the demonstration that Shiv just showed. The goal here is simply to give you a sense of how things are structured under the hood. The full codebase is available on GitHub for you to explore at your own pace. We will share the GitHub links for you to go through after the presentation.
458-
459-
-->
460-
396+
````
461397
---
462398
transition: slide-right
463399
class: text-2xl
464400
---
465401

466402

467-
# Oxidation Compiler <img src="https://raw.githubusercontent.com/oxc-project/oxc-assets/main/uwu.png" class="inline-block h-18 mr-2" />
403+
# Future Work: "The Oxidation Compiler" <img src="https://raw.githubusercontent.com/oxc-project/oxc-assets/main/uwu.png" class="inline-block h-18 mr-2" />
468404

469405

470-
- Extended our Rust learning
471-
- Contributing to the Oxc project
472-
- Applying our Rust concepts to larger projects
473-
- Plan to keep contributing
406+
- Extending to **open-source** projects.
407+
- Applying our Rust concepts to bigger projects.
408+
- Plan to keep contributing.
474409

475410
<footer class="absolute bottom-2 left-0 w-full text-center text-xs text-gray-500" style="font-size: 0.4rem">
476411
Logos © their respective owners.
@@ -569,10 +504,10 @@ You've heard about Rust, and now you have seen it too!
569504

570505
<br>
571506

572-
- <v-mark v-mark.highlight.yellow="2"> <b>Collaborative learning</b> </v-mark> played a key role in helping us reach this milestone.
573-
- <v-mark v-mark.highlight.yellow="3"> <b>Consistent, small weekly efforts</b> </v-mark> added up and brought us to this point.
574-
- <v-mark v-mark.highlight.yellow="4"> <b>Keeping abstractions to a minimum</b> </v-mark> helped us better appreciate Rust’s core philosophy.
575-
- <v-mark v-mark.highlight.yellow="5"> <b>Learning together kept our motivation high</b> </v-mark>, and we’ve since kept the momentum going through open-source projects.
507+
- <v-mark v-mark.highlight.yellow="2"> <b>Consistent, small weekly efforts</b> </v-mark> added up and brought us to this point.
508+
- <v-mark v-mark.highlight.yellow="3"> <b>Minimal use of out-of-the-box frameworks</b> </v-mark> helped us better appreciate Rust’s core philosophy.
509+
- <v-mark v-mark.highlight.yellow="4"> <b>Learning together kept our motivation high</b> </v-mark>, and we’ve since kept the momentum going through open-source projects.
510+
- Because our <v-mark v-mark.highlight.yellow="5"> <b>collaborative learning</b></v-mark> sessions were highly effective, we plan to carry that approach forward into our PSD work.
576511

577512
</v-click>
578513

0 commit comments

Comments
 (0)