Browse Source

Exercism: Semi structured logs

master
Julio Biason 3 years ago
parent
commit
1b8b710494
  1. 17
      rust/semi-structured-logs/.exercism/config.json
  2. 1
      rust/semi-structured-logs/.exercism/metadata.json
  3. 7
      rust/semi-structured-logs/Cargo.lock
  4. 9
      rust/semi-structured-logs/Cargo.toml
  5. 85
      rust/semi-structured-logs/HELP.md
  6. 13
      rust/semi-structured-logs/HINTS.md
  7. 64
      rust/semi-structured-logs/README.md
  8. 33
      rust/semi-structured-logs/src/lib.rs
  9. 45
      rust/semi-structured-logs/tests/semi-structured-logs.rs

17
rust/semi-structured-logs/.exercism/config.json

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

1
rust/semi-structured-logs/.exercism/metadata.json

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

7
rust/semi-structured-logs/Cargo.lock generated

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "semi_structured_logs"
version = "0.1.0"

9
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]

85
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

13
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

64
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

33
rust/semi-structured-logs/src/lib.rs

@ -0,0 +1,33 @@
// This stub file contains items which aren't used yet; feel free to remove this module attribute
// to enable stricter warnings.
#![allow(unused)]
/// various log levels
#[derive(Clone, PartialEq, Debug)]
pub enum LogLevel {
Debug,
Info,
Warning,
Error,
}
impl std::fmt::Display for LogLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name = format!("{:?}", self);
write!(f, "{}", name.to_uppercase())
}
}
/// primary function for emitting logs
pub fn log(level: LogLevel, message: &str) -> String {
format!("[{}]: {}", level, message)
}
pub fn info(message: &str) -> String {
log(LogLevel::Info, message)
}
pub fn warn(message: &str) -> String {
log(LogLevel::Warning, message)
}
pub fn error(message: &str) -> String {
log(LogLevel::Error, message)
}

45
rust/semi-structured-logs/tests/semi-structured-logs.rs

@ -0,0 +1,45 @@
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]
fn add_a_variant() {
assert_eq!(
log(LogLevel::Debug, "reached line 123"),
"[DEBUG]: reached line 123",
);
}
Loading…
Cancel
Save