Skip to content

Commit 826103c

Browse files
committed
Add validation
1 parent 21cb6ae commit 826103c

File tree

1 file changed

+19
-1
lines changed
  • exercises/03_ticket_v1/02_validation/src

1 file changed

+19
-1
lines changed

exercises/03_ticket_v1/02_validation/src/lib.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::thread::panicking;
2+
13
struct Ticket {
24
title: String,
35
description: String,
@@ -18,7 +20,23 @@ impl Ticket {
1820
// as well as some `String` methods. Use the documentation of Rust's standard library
1921
// to find the most appropriate options -> https://doc.rust-lang.org/std/string/struct.String.html
2022
fn new(title: String, description: String, status: String) -> Self {
21-
todo!();
23+
match status.as_str() {
24+
"To-Do" | "In Progress" | "Done" => (),
25+
_ => panic!("Only `To-Do`, `In Progress`, and `Done` statuses are allowed"),
26+
}
27+
if title.is_empty() {
28+
panic!("Title cannot be empty")
29+
}
30+
if description.is_empty() {
31+
panic!("Description cannot be empty!")
32+
}
33+
if title.bytes().count() > 50 {
34+
panic!("Title cannot be longer than 50 bytes")
35+
}
36+
if description.bytes().count() > 500 {
37+
panic!("Description cannot be longer than 500 bytes")
38+
}
39+
2240
Self {
2341
title,
2442
description,

0 commit comments

Comments
 (0)