diff options
author | Andinus <andinus@nand.sh> | 2021-09-29 09:43:51 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2021-09-29 09:43:51 +0530 |
commit | d8a4756b35ce3576bc5027e166903610558cfeab (patch) | |
tree | 5b49c90e6428fe6914f2e9505d73faa1c80dd978 /rust/semi-structured-logs | |
parent | 45fab565a31a535324a335fa96df8f010d855b5c (diff) | |
download | exercism-d8a4756b35ce3576bc5027e166903610558cfeab.tar.gz |
Rust: Semi Structured Logs: Add Solution
Diffstat (limited to 'rust/semi-structured-logs')
-rw-r--r-- | rust/semi-structured-logs/Cargo.lock | 5 | ||||
-rw-r--r-- | rust/semi-structured-logs/Cargo.toml | 9 | ||||
-rw-r--r-- | rust/semi-structured-logs/HELP.md | 85 | ||||
-rw-r--r-- | rust/semi-structured-logs/HINTS.md | 13 | ||||
-rw-r--r-- | rust/semi-structured-logs/README.md | 64 | ||||
-rw-r--r-- | rust/semi-structured-logs/src/lib.rs | 25 | ||||
-rw-r--r-- | rust/semi-structured-logs/tests/semi-structured-logs.rs | 47 |
7 files changed, 248 insertions, 0 deletions
diff --git a/rust/semi-structured-logs/Cargo.lock b/rust/semi-structured-logs/Cargo.lock new file mode 100644 index 0000000..4db2138 --- /dev/null +++ b/rust/semi-structured-logs/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "semi_structured_logs" +version = "0.1.0" diff --git a/rust/semi-structured-logs/Cargo.toml b/rust/semi-structured-logs/Cargo.toml new file mode 100644 index 0000000..0cbd800 --- /dev/null +++ b/rust/semi-structured-logs/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "semi_structured_logs" +version = "0.1.0" +edition = "2018" + +[features] +add-a-variant = [] + +[dependencies] diff --git a/rust/semi-structured-logs/HELP.md b/rust/semi-structured-logs/HELP.md new file mode 100644 index 0000000..b4252f8 --- /dev/null +++ b/rust/semi-structured-logs/HELP.md @@ -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` 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 support channel on gitter](https://gitter.im/exercism/support) +- 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.io/tracks/rust/learning +[github]: https://github.com/exercism/rust +[contribution guide]: https://exercism.io/docs/community/contributors \ No newline at end of file diff --git a/rust/semi-structured-logs/HINTS.md b/rust/semi-structured-logs/HINTS.md new file mode 100644 index 0000000..1804e14 --- /dev/null +++ b/rust/semi-structured-logs/HINTS.md @@ -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 \ No newline at end of file diff --git a/rust/semi-structured-logs/README.md b/rust/semi-structured-logs/README.md new file mode 100644 index 0000000..955299c --- /dev/null +++ b/rust/semi-structured-logs/README.md @@ -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 \ No newline at end of file diff --git a/rust/semi-structured-logs/src/lib.rs b/rust/semi-structured-logs/src/lib.rs new file mode 100644 index 0000000..4f06874 --- /dev/null +++ b/rust/semi-structured-logs/src/lib.rs @@ -0,0 +1,25 @@ +pub enum LogLevel { + Info, + Warning, + Error, +} + +pub fn log(level: LogLevel, message: &str) -> String { + match level { + LogLevel::Info => info(message), + LogLevel::Warning => warn(message), + LogLevel::Error => error(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) +} diff --git a/rust/semi-structured-logs/tests/semi-structured-logs.rs b/rust/semi-structured-logs/tests/semi-structured-logs.rs new file mode 100644 index 0000000..fc45208 --- /dev/null +++ b/rust/semi-structured-logs/tests/semi-structured-logs.rs @@ -0,0 +1,47 @@ +use semi_structured_logs::{error, info, log, warn, LogLevel}; + +#[test] +fn emits_info() { + assert_eq!(info("Timezone changed"), "[INFO]: Timezone changed"); +} + +#[test] +fn emits_warning() { + assert_eq!(warn("Timezone not set"), "[WARNING]: Timezone not set"); +} + +#[test] +fn emits_error() { + assert_eq!(error("Disk full"), "[ERROR]: Disk full"); +} + +#[test] +fn log_emits_info() { + assert_eq!( + log(LogLevel::Info, "Timezone changed"), + "[INFO]: Timezone changed" + ); +} + +#[test] +fn log_emits_warning() { + assert_eq!( + log(LogLevel::Warning, "Timezone not set"), + "[WARNING]: Timezone not set" + ); +} + +#[test] +fn log_emits_error() { + assert_eq!(log(LogLevel::Error, "Disk full"), "[ERROR]: Disk full"); +} + +#[test] +#[cfg(feature = "add-a-variant")] +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", + ); +} |