feat: reorganize workbook into 3 tracks (core, engineering-2026, ecosystems)

This commit is contained in:
soda64 committed 2026-11-30 22:30:53 +01:00
1 parent 82677455b2
commit 2e002b6176
64 files changed
+2295 -196

No files matched your search

@@ -0,0 +1,4 @@
[package]
name = "core09-design-patterns"
version = "0.1.0"
edition = "2021"
@@ -0,0 +1,18 @@
# 09 — Idiomatyczne Wzorce Projektowe w Ruście
Zamiast kopiować 1:1 wzorce z Javy/C++, Rust promuje wzorce wykorzystujące system typów i ownership:
* **Newtype** — osobny typ dla identyfikatorów (zapobiega pomyleniu `UserId` z `OrderId`).
* **Builder** — bezpieczna konstrukcja obiektów z wieloma opcjami.
* **Typestate** — stany obiektu wymuszane na etapie kompilacji (`Draft` -> `Published`).
* **Strategy** — wstrzykiwanie zachowania przez trait.
## Zadania (`src/lib.rs`)
1. `UserId(u64)` i `OrderId(u64)` — wzorzec Newtype.
2. `HttpRequestBuilder` — wzorzec Builder z metodami łańcuchowymi (`method`, `header`, `build`).
3. `Document<Draft>` -> `publish()` -> `Document<Published>` — wzorzec Typestate (`PhantomData`).
4. `DiscountStrategy` + `PercentageDiscount` + `apply_discount` — wzorzec Strategy.
## Uruchomienie testów
```bash
make test PKG=core09-design-patterns
```
@@ -0,0 +1,132 @@
#![allow(unused_variables, dead_code, unused_mut)]
use std::marker::PhantomData;
// --- 1. Newtype ---
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UserId(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OrderId(pub u64);
pub fn format_audit_entry(user: UserId, order: OrderId) -> String {
todo!("Zwróć \"user:<id> order:<id>\"")
}
// --- 2. Builder ---
#[derive(Debug, PartialEq)]
pub struct HttpRequest {
pub url: String,
pub method: String,
pub timeout_ms: u64,
}
pub struct HttpRequestBuilder {
url: String,
method: String,
timeout_ms: u64,
}
impl HttpRequestBuilder {
pub fn new(url: impl Into<String>) -> Self {
todo!("Utwórz builder z domyślną metodą \"GET\" i timeout_ms = 3000")
}
pub fn method(mut self, method: impl Into<String>) -> Self {
todo!("Ustaw method i zwróć self")
}
pub fn timeout_ms(mut self, timeout_ms: u64) -> Self {
todo!("Ustaw timeout_ms i zwróć self")
}
pub fn build(self) -> HttpRequest {
todo!("Zbuduj HttpRequest")
}
}
// --- 3. Typestate ---
pub struct Draft;
pub struct Published;
pub struct Document<State> {
title: String,
_state: PhantomData<State>,
}
impl Document<Draft> {
pub fn new(title: impl Into<String>) -> Self {
todo!("Utwórz dokument w stanie Draft")
}
pub fn publish(self) -> Document<Published> {
todo!("Przejdź ze stanu Draft do Published")
}
}
impl Document<Published> {
pub fn title(&self) -> &str {
&self.title
}
}
// --- 4. Strategy ---
pub trait DiscountStrategy {
fn discount(&self, price_cents: u64) -> u64;
}
pub struct PercentageDiscount {
pub percent: u64,
}
impl DiscountStrategy for PercentageDiscount {
fn discount(&self, price_cents: u64) -> u64 {
todo!("Oblicz rabat: price_cents * self.percent / 100")
}
}
pub fn final_price(price_cents: u64, strategy: &dyn DiscountStrategy) -> u64 {
todo!("Zwróć cenę po rabacie (saturating_sub)")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn newtype_formats_audit_entry() {
assert_eq!(
format_audit_entry(UserId(7), OrderId(42)),
"user:7 order:42"
);
}
#[test]
fn builder_constructs_request() {
let req = HttpRequestBuilder::new("https://example.com")
.method("POST")
.timeout_ms(5000)
.build();
assert_eq!(
req,
HttpRequest {
url: String::from("https://example.com"),
method: String::from("POST"),
timeout_ms: 5000,
}
);
}
#[test]
fn typestate_transitions_draft_to_published() {
let draft = Document::new("Rust Patterns");
let published = draft.publish();
assert_eq!(published.title(), "Rust Patterns");
}
#[test]
fn strategy_calculates_discounted_price() {
let strategy = PercentageDiscount { percent: 20 };
assert_eq!(final_price(1000, &strategy), 800);
}
}