exercism enums basic 1

This commit is contained in:
LuKe Tidd 2023-01-10 08:30:19 -05:00
parent 804501654b
commit aea4ade81b
Signed by: luke
GPG Key ID: 75D6600BEF4E8E8F
10 changed files with 294 additions and 0 deletions

11
exercism/exercism_notes Executable file
View File

@ -0,0 +1,11 @@
site: https://exercism.org/tracks/rust/exercises/
* download exercise:
exercism download --exercise=<exercise-slug> --track=<track-slug>
example: exercism download --exercise=semi-structured-logs --track=rust
* submit solution:
exercism submit <file>
example: exercism submit semi-structured-logs/src/lib.rs

View File

@ -0,0 +1,18 @@
{
"authors": [
"efx"
],
"files": {
"solution": [
"src/lib.rs",
"Cargo.toml"
],
"test": [
"tests/semi-structured-logs.rs"
],
"exemplar": [
".meta/exemplar.rs"
]
},
"blurb": "Learn enums while building a logging utility."
}

View File

@ -0,0 +1 @@
{"track":"rust","exercise":"semi-structured-logs","id":"2e750e14b86e466caba76fda27e561db","url":"https://exercism.org/tracks/rust/exercises/semi-structured-logs","handle":"habys","is_requester":true,"auto_approve":false}

View File

@ -0,0 +1,8 @@
# Generated by Cargo
# will have compiled files and executables
/target/
**/*.rs.bk
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock

View File

@ -0,0 +1,9 @@
[package]
name = "semi_structured_logs"
version = "0.1.0"
edition = "2021"
[features]
add-a-variant = []
[dependencies]

View File

@ -0,0 +1,85 @@
# Help
## Running the tests
Execute the tests with:
```bash
$ cargo test
```
All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests` directory
and remove the `#[ignore]` flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]` flag above it.
Continue, until you pass every test.
If you wish to run _only ignored_ tests without editing the tests source file, use:
```bash
$ cargo test -- --ignored
```
If you are using Rust 1.51 or later, you can run _all_ tests with
```bash
$ cargo test -- --include-ignored
```
To run a specific test, for example `some_test`, you can use:
```bash
$ cargo test some_test
```
If the specific test is ignored, use:
```bash
$ cargo test some_test -- --ignored
```
To learn more about Rust tests refer to the online [test documentation][rust-tests].
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
## Submitting your solution
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
## Rust Installation
Refer to the [exercism help page][help-page] for Rust installation and learning
resources.
## Submitting the solution
Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
## Feedback, Issues, Pull Requests
The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
If you want to know more about Exercism, take a look at the [contribution guide].
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
[help-page]: https://exercism.org/tracks/rust/learning
[github]: https://github.com/exercism/rust
[contribution guide]: https://exercism.org/docs/community/contributors

View File

@ -0,0 +1,13 @@
# Hints
## General
- [Rust By Example - Enums][rbe-enums]
- [cheats.rs - Basic Types][cheats-types]
## 1. Emit semi-structured messages
- `match` comes in handy when working with enums. In this case, see how you might use it when figuring how what kind of log message to generate.
[rbe-enums]: https://doc.rust-lang.org/stable/rust-by-example/custom_types/enum.html#enums
[cheats-types]: https://cheats.rs/#basic-types

View File

@ -0,0 +1,64 @@
# Semi Structured Logs
Welcome to Semi Structured Logs on Exercism's Rust Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
## Introduction
Enums, short for enumerations, are a type that limits all possible values of some data. The possible values of an `enum` are called variants. Enums also work well with `match` and other control flow operators to help you express intent in your Rust programs.
## Instructions
In this exercise you'll be generating semi-structured log messages.
## 1. Emit semi-structured messages
You'll start with some stubbed functions and the following enum:
```rust
#[derive(Clone, PartialEq, Debug)]
pub enum LogLevel {
Info,
Warning,
Error,
}
```
Your goal is to emit a log message as follows: `"[<LEVEL>]: <MESSAGE>"`.
You'll need to implement functions that correspond with log levels.
For example, the below snippet demonstrates an expected output for the `log` function.
```rust
log(LogLevel::Error, "Stack overflow")
// Returns: "[ERROR]: Stack overflow"
```
And for `info`:
```rust
info("Timezone changed")
// Returns: "[INFO]: Timezone changed"
```
Have fun!
## 2. Optional further practice
There is a feature-gated test in this suite.
Feature gates disable compilation entirely for certain sections of your program.
They will be covered later.
For now just know that there is a test which is only built and run when you use a special testing invocation:
```sh
cargo test --features add-a-variant
```
This test is meant to show you how to add a variant to your enum.
## Source
### Created by
- @efx

View File

@ -0,0 +1,32 @@
// This stub file contains items that aren't used yet; feel free to remove this module attribute
// to enable stricter warnings.
/// various log levels
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum LogLevel {
Debug,
Info,
Warning,
Error,
}
/// primary function for emitting logs
pub fn log(level: LogLevel, message: &str) -> String {
match level {
LogLevel::Debug => debug(&message),
LogLevel::Info => info(&message),
LogLevel::Warning => warn(&message),
LogLevel::Error => error(&message),
}
}
pub fn debug(message: &str) -> String {
format!("[DEBUG]: {message}")
}
pub fn info(message: &str) -> String {
format!("[INFO]: {message}")
}
pub fn warn(message: &str) -> String {
format!("[WARNING]: {message}")
}
pub fn error(message: &str) -> String {
format!("[ERROR]: {message}")
}

View File

@ -0,0 +1,53 @@
use semi_structured_logs::{error, info, log, warn, LogLevel};
#[test]
fn emits_info() {
assert_eq!(info("Timezone changed"), "[INFO]: Timezone changed");
}
#[test]
//#[ignore]
fn emits_warning() {
assert_eq!(warn("Timezone not set"), "[WARNING]: Timezone not set");
}
#[test]
//#[ignore]
fn emits_error() {
assert_eq!(error("Disk full"), "[ERROR]: Disk full");
}
#[test]
//#[ignore]
fn log_emits_info() {
assert_eq!(
log(LogLevel::Info, "Timezone changed"),
"[INFO]: Timezone changed"
);
}
#[test]
//#[ignore]
fn log_emits_warning() {
assert_eq!(
log(LogLevel::Warning, "Timezone not set"),
"[WARNING]: Timezone not set"
);
}
#[test]
//#[ignore]
fn log_emits_error() {
assert_eq!(log(LogLevel::Error, "Disk full"), "[ERROR]: Disk full");
}
#[test]
#[cfg(feature = "add-a-variant")]
//#[ignore]
fn add_a_variant() {
// this test won't even compile until the enum is complete, which is why it is feature-gated.
assert_eq!(
log(LogLevel::Debug, "reached line 123"),
"[DEBUG]: reached line 123",
);
}