feat: ✨ enforce rules by defining restrictive custom types
This commit is contained in:
parent
889c95e58f
commit
ffb8e8a525
3 changed files with 66 additions and 0 deletions
6
25-validation_types/Cargo.toml
Normal file
6
25-validation_types/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "validation_types"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
41
25-validation_types/src/lib.rs
Normal file
41
25-validation_types/src/lib.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
// This is our type to enforce some rules
|
||||||
|
// Since struct itself is public and word inside of it private
|
||||||
|
// we're able to create TahinliWord everywhere but
|
||||||
|
// can't modify it's value (word) outside of this module
|
||||||
|
// This will allow us to apply some rules
|
||||||
|
pub struct TahinliWord {
|
||||||
|
word: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TahinliWord {
|
||||||
|
// This's basically a validator that enforces TahinliWord rules
|
||||||
|
// Since this is private I'm only using it in this module for clean code
|
||||||
|
fn validator(word: &String) -> bool {
|
||||||
|
match word.as_str() {
|
||||||
|
"Tahinli" | "tahinli" | "TAHINLI" => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is one and only function that allow user to create new TahinliWord
|
||||||
|
// Thanks to this situation we're able to check whatever we want
|
||||||
|
pub fn new(word: String) -> Self {
|
||||||
|
TahinliWord::validator(&word);
|
||||||
|
TahinliWord { word }
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is a getter method that only exposes it's value
|
||||||
|
pub fn get(&self) -> &String {
|
||||||
|
&self.word
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is a setter method that's the only way to edit existing TahinliWord
|
||||||
|
// This gives us power to do whatever check we want
|
||||||
|
pub fn set(&mut self, word: String) {
|
||||||
|
if TahinliWord::validator(&word) {
|
||||||
|
self.word = word;
|
||||||
|
} else {
|
||||||
|
panic!("\"{}\" is not a Tahinli Word", word);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
25-validation_types/src/main.rs
Normal file
19
25-validation_types/src/main.rs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
use validation_types::TahinliWord;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
|
||||||
|
// This will create new TahinliWord type
|
||||||
|
let mut tahinli_word = TahinliWord::new("Tahinli".to_string());
|
||||||
|
// This will print like normal string
|
||||||
|
println!("{}", tahinli_word.get());
|
||||||
|
|
||||||
|
// Borrow checker not allow to manipulate it
|
||||||
|
// It's read only value
|
||||||
|
// let mut try_to_manipulate = tahinli_word.get();
|
||||||
|
// try_to_manipulate.clear();
|
||||||
|
|
||||||
|
// This will try to bypass Tahinli Word rules and panic
|
||||||
|
tahinli_word.set("This is not a Tahinli Word".to_string());
|
||||||
|
println!("Code won't run this line :/");
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue