feat: reorganize workbook into 3 tracks (core, engineering-2026, ecosystems)
This commit is contained in:
1 parent
82677455b2
commit
2e002b6176
64 files changed
+2295
-196
No files matched your search
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "eng04-best-practices"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
@@ -0,0 +1,16 @@
|
||||
# 04 — Dobre Praktyki w Ruście (Errors, Display, From, Doctests)
|
||||
|
||||
## Zakres
|
||||
* Implementacja standardowych traitów `fmt::Display` i `std::error::Error` (fundament tego, co generuje `thiserror`)
|
||||
* Konwersja błędów przez `From<E>` dla czystego operatora `?`
|
||||
* Dokumentacja publicznego API i **doctesty** (testy wewnątrz komentarzy `///`)
|
||||
|
||||
## Zadania (`src/lib.rs`)
|
||||
1. Zaimplementuj `fmt::Display` i `std::error::Error` dla `ConfigError`.
|
||||
2. Zaimplementuj `From<ParseIntError>` dla `ConfigError`.
|
||||
3. Zaimplementuj `parse_timeout_secs(raw: &str) -> Result<u64, ConfigError>` (odrzuca puste wejście jako `Missing`, błędy parsowania przez `?`, a wartości `0` lub `> 300` jako `OutOfRange(u64)`).
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=eng04-best-practices
|
||||
```
|
||||
@@ -0,0 +1,50 @@
|
||||
#![allow(unused_variables, dead_code)]
|
||||
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::num::ParseIntError;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum ConfigError {
|
||||
Missing,
|
||||
InvalidNumber(String),
|
||||
OutOfRange(u64),
|
||||
}
|
||||
|
||||
impl fmt::Display for ConfigError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
todo!("Sformatuj czytelny komunikat błędu dla każdego wariantu ConfigError")
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for ConfigError {}
|
||||
|
||||
impl From<ParseIntError> for ConfigError {
|
||||
fn from(err: ParseIntError) -> Self {
|
||||
todo!("Zamień ParseIntError na ConfigError::InvalidNumber(err.to_string())")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_timeout_secs(raw: &str) -> Result<u64, ConfigError> {
|
||||
todo!("Trim -> Missing jeśli pusty -> parse::<u64>()? -> OutOfRange jeśli 0 lub > 300")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn validates_timeout_range_and_formatting() {
|
||||
assert_eq!(parse_timeout_secs("30"), Ok(30));
|
||||
assert_eq!(parse_timeout_secs(" "), Err(ConfigError::Missing));
|
||||
assert_eq!(parse_timeout_secs("0"), Err(ConfigError::OutOfRange(0)));
|
||||
assert_eq!(parse_timeout_secs("301"), Err(ConfigError::OutOfRange(301)));
|
||||
assert!(matches!(
|
||||
parse_timeout_secs("abc"),
|
||||
Err(ConfigError::InvalidNumber(_))
|
||||
));
|
||||
|
||||
let msg = ConfigError::Missing.to_string();
|
||||
assert!(!msg.is_empty());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user