completed 05 ticket v2, started 06 tms
Some checks failed
CI / build (push) Failing after 1m54s
CI / gravity (push) Has been skipped
CI / is_fresh (push) Successful in 31s
CI / formatter (push) Failing after 7m2s

This commit is contained in:
2025-03-17 12:14:28 +05:30
parent 8413518a84
commit 2fa61e7715
9 changed files with 126 additions and 3 deletions

6
Cargo.lock generated
View File

@@ -1442,6 +1442,9 @@ version = "0.1.0"
[[package]] [[package]]
name = "outro_04" name = "outro_04"
version = "0.1.0" version = "0.1.0"
dependencies = [
"thiserror",
]
[[package]] [[package]]
name = "outro_08" name = "outro_08"
@@ -2320,6 +2323,9 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]] [[package]]
name = "tryfrom" name = "tryfrom"
version = "0.1.0" version = "0.1.0"
dependencies = [
"thiserror",
]
[[package]] [[package]]
name = "tungstenite" name = "tungstenite"

View File

@@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
thiserror="*"

View File

@@ -8,6 +8,36 @@ enum Status {
Done, Done,
} }
// use thiserror::Error;
#[derive(Debug, thiserror::Error)]
#[error("{reason}, parsing failed.")]
struct ParseError {
reason: String,
}
impl TryFrom<String> for Status {
type Error = ParseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
match value.to_lowercase().as_str() {
"todo" => Ok(Status::ToDo),
"done" => Ok(Status::Done),
"inprogress" => Ok(Status::InProgress),
_ => Err(ParseError {
reason: "Not Valid".to_string(),
}),
}
// Ok(Status::Done)
}
}
impl TryFrom<&str> for Status {
type Error = ParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
value.to_string().try_into()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@@ -1,3 +1,5 @@
use status::ParseStatusError;
use crate::status::Status; use crate::status::Status;
// We've seen how to declare modules in one of the earliest exercises, but // We've seen how to declare modules in one of the earliest exercises, but
@@ -23,6 +25,8 @@ pub enum TicketNewError {
DescriptionCannotBeEmpty, DescriptionCannotBeEmpty,
#[error("Description cannot be longer than 500 bytes")] #[error("Description cannot be longer than 500 bytes")]
DescriptionTooLong, DescriptionTooLong,
#[error("{0}")]
InvalidStatus(#[from] ParseStatusError),
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
@@ -46,8 +50,7 @@ impl Ticket {
if description.len() > 500 { if description.len() > 500 {
return Err(TicketNewError::DescriptionTooLong); return Err(TicketNewError::DescriptionTooLong);
} }
let status = Status::try_from(status)?;
// TODO: Parse the status string into a `Status` enum.
Ok(Ticket { Ok(Ticket {
title, title,

View File

@@ -2,3 +2,6 @@
name = "outro_04" name = "outro_04"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies]
thiserror="*"

View File

@@ -2,8 +2,33 @@
// enforcing that the description is not empty and is not longer than 500 bytes. // enforcing that the description is not empty and is not longer than 500 bytes.
// Implement the traits required to make the tests pass too. // Implement the traits required to make the tests pass too.
#[derive(Debug, PartialEq, Clone)]
pub struct TicketDescription(String); pub struct TicketDescription(String);
#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct ParseError(String);
impl TryFrom<String> for TicketDescription {
type Error = ParseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
match value.len() {
0 => Err(ParseError("The description cannot be empty".to_string())),
501.. => Err(ParseError(
"The description cannot be longer than 500 bytes".to_string(),
)),
_ => Ok(TicketDescription(value)),
}
}
}
impl TryFrom<&str> for TicketDescription {
type Error = ParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
value.to_string().try_into()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@@ -1,12 +1,36 @@
// TODO: Implement `TryFrom<String>` and `TryFrom<&str>` for the `Status` enum. // TODO: Implement `TryFrom<String>` and `TryFrom<&str>` for the `Status` enum.
// The parsing should be case-insensitive. // The parsing should be case-insensitive.
#[derive(Debug, PartialEq, Clone)]
pub enum Status { pub enum Status {
ToDo, ToDo,
InProgress, InProgress,
Done, Done,
} }
#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct ParseError(String);
impl TryFrom<String> for Status {
type Error = ParseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
match value.to_lowercase().as_str() {
"todo" => Ok(Status::ToDo),
"done" => Ok(Status::Done),
"inprogress" => Ok(Status::InProgress),
_ => Err(ParseError("Not Valid".to_string())),
}
// Ok(Status::Done)
}
}
impl TryFrom<&str> for Status {
type Error = ParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
value.to_string().try_into()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@@ -2,8 +2,39 @@
// enforcing that the title is not empty and is not longer than 50 bytes. // enforcing that the title is not empty and is not longer than 50 bytes.
// Implement the traits required to make the tests pass too. // Implement the traits required to make the tests pass too.
#[derive(Debug, Clone, PartialEq)]
pub struct TicketTitle(String); pub struct TicketTitle(String);
#[derive(thiserror::Error, Debug)]
#[error("{reason}")]
pub struct InvalidTicketError {
reason: String,
}
impl TryFrom<String> for TicketTitle {
type Error = InvalidTicketError;
fn try_from(value: String) -> Result<Self, Self::Error> {
if value.len() == 0 {
return Err(InvalidTicketError {
reason: "The title cannot be empty".to_string(),
});
} else if value.len() > 50 {
return Err(InvalidTicketError {
reason: "The title cannot be longer than 50 bytes".to_string(),
});
} else {
return Ok(TicketTitle(value));
}
}
}
impl TryFrom<&str> for TicketTitle {
type Error = InvalidTicketError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
value.to_string().try_into()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@@ -1,6 +1,6 @@
fn intro() -> &'static str { fn intro() -> &'static str {
// TODO: fix me 👇 // TODO: fix me 👇
"I'm ready to __!" "I'm ready to build a ticket management system!"
} }
#[cfg(test)] #[cfg(test)]