Skip to content

Commit

Permalink
Merge pull request #5 from dcodesdev/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dcodesdev authored May 6, 2024
2 parents d22275d + a4e1c1f commit a003ab9
Show file tree
Hide file tree
Showing 18 changed files with 326 additions and 19 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/crates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
pull_request:
branches: [main]

env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

jobs:
test:
runs-on: ubuntu-latest
Expand All @@ -19,5 +22,9 @@ jobs:
toolchain: stable
override: true

- name: Test
- name: Cargo test
run: cargo test

- name: Cargo publish
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: cargo publish || true
69 changes: 67 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"challenges",
"challenges/hello-world",
"challenges/character-counting-string",
"challenges/fibonacci",
Expand All @@ -8,8 +9,8 @@ members = [
"challenges/the-from-trait",
"challenges/animal-sanctuary-registry",
"challenges/median-and-mode",
"challenges/graceful-error-handling",

"crates/cli",
]

resolver = "2"
2 changes: 2 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
![Rustfinity.com](/images/rustfinity-header.png)

# Rustfinity

A repository for all the publicly available content for Rustfinity.
Expand Down
9 changes: 9 additions & 0 deletions challenges/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "challenges"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0.200", features = ["derive"] }
serde_json = "1.0.116"
toml = "0.8.12"
12 changes: 12 additions & 0 deletions challenges/challenges.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,17 @@
"tags": [],
"created_at": "2024-05-03T00:00:00Z",
"updated_at": "2024-05-03T00:00:00Z"
},
{
"id": 9,
"title": "Graceful error handling",
"slug": "graceful-error-handling",
"short_description": "Implement graceful error handling in Rust using the Result type.",
"language": "RUST",
"difficulty": "EASY",
"track": "RUST_BASICS",
"tags": [],
"created_at": "2024-05-06T00:00:00Z",
"updated_at": "2024-05-06T00:00:00Z"
}
]
2 changes: 1 addition & 1 deletion challenges/fizz-buzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "fizzbuzz"
name = "fizz-buzz"
version = "0.1.0"
edition = "2021"

Expand Down
6 changes: 6 additions & 0 deletions challenges/graceful-error-handling/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "graceful-error-handling"
version = "0.1.0"
edition = "2021"

[dependencies]
32 changes: 32 additions & 0 deletions challenges/graceful-error-handling/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
One of the most loved features of Rust is the way it lets you handle errors. The `Result` type is a powerful tool that allows you to handle errors in a way that is both safe and expressive. In this challenge, you will be working with the `Result<T, E>` type to handle errors in a graceful way.

The `Result<T, E>` itself is an enum that has two variants: `Ok(T)` and `Err(E)`. The `Ok` variant is used to represent a successful computation that returns a value of type `T`. The `Err` variant is used to represent an error that returns a value of type `E`.

When you have a function that can fail, you can use the `Result` type to return the result of the computation. If the computation is successful, you can return the success variant of `Result` with the value of the computation. If the computation fails, you can return the error variant of `Result` with an error message that explains what went wrong.

## Your task

In this challenges, you're given a function, `parse_percentage(input: &str) -> Result<u8, String>` that takes a string as input and returns a `Result` type. The function should parse the input string as a percentage and return the percentage as a `u8` if the input is valid. If the input is invalid, the function should return an error message as a `String`.

Parsing from a string to a number can fail for many reasons. For example, the input string may not be a valid number, or it may be a valid number but not a valid percentage. Your task is to handle these errors gracefully and return an error message that explains what went wrong.

Complete the function, if the parsing was successful return a success variant of the `Result`, if there was an error in parsing, return an error variant of the `Result` with an error message.

## Requirements

- If the parse was successful, the function should return the success variant of `Result` with the percentage as a `u8`.
- If the number was out of range (not between 0 and 100), the function should return the error with `String` "Percentage out of range".
- If the string was not a valid number, the function should return the error with `String` "Invalid input".

## Example

```rust
let result = parse_percentage("50");
assert_eq!(result, Ok(50));

let result = parse_percentage("101");
assert_eq!(result, Err("Percentage out of range".to_string()));

let result = parse_percentage("abc");
assert_eq!(result, Err("Invalid input".to_string()));
```
9 changes: 9 additions & 0 deletions challenges/graceful-error-handling/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod tests;

pub fn parse_percentage(input: &str) -> Result<u8, String> {
match input.parse::<u8>() {
Ok(percentage) if percentage <= 100 => Ok(percentage),
Ok(_) => Err(String::from("Percentage out of range")),
Err(_) => Err(String::from("Invalid input")),
}
}
3 changes: 3 additions & 0 deletions challenges/graceful-error-handling/src/starter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn parse_percentage(input: &str) -> Result<u8, String> {
// TODO: Implement the function here
}
28 changes: 28 additions & 0 deletions challenges/graceful-error-handling/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[cfg(test)]
mod tests {
use crate::parse_percentage;

#[test]
fn test_parse_percentage_valid() {
assert_eq!(parse_percentage("75"), Ok(75));
}

#[test]
fn test_parse_percentage_out_of_range() {
assert_eq!(
parse_percentage("150"),
Err(String::from("Percentage out of range"))
);
}

#[test]
fn test_parse_percentage_non_numeric() {
assert_eq!(parse_percentage("abc"), Err(String::from("Invalid input")));
}

#[test]
fn test_parse_percentage_edge_cases() {
assert_eq!(parse_percentage("0"), Ok(0));
assert_eq!(parse_percentage("100"), Ok(100));
}
}
Loading

0 comments on commit a003ab9

Please sign in to comment.