Skip to content

Commit f6422b2

Browse files
committed
Define a language enum.
1 parent f6d32f5 commit f6422b2

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ tldr = { version = "0", package = "tldr-traits", default-features = false, featu
4848
### Importing the Library
4949

5050
```rust,ignore
51-
use tldr::{Tldr, TldrContext, TldrResult, TldrSummary, ToTldr};
51+
use tldr::{Tldr, TldrContext, TldrLanguage, TldrResult, TldrSummary, ToTldr};
5252
```
5353

5454
### Implementing the Trait
@@ -72,6 +72,7 @@ impl Tldr for Rectangle {
7272

7373
- [`Tldr`](#tldr)
7474
- [`TldrContext`](#tldrcontext)
75+
- [`TldrLanguage`](#tldrlanguage)
7576
- [`TldrResult`](#tldrresult)
7677
- [`TldrSummary`](#tldrsummary)
7778
- [`ToTldr`](#totldr)
@@ -92,9 +93,20 @@ pub trait Tldr<T = String> {
9293

9394
### [`TldrContext`]
9495

95-
```rust
96+
```rust,ignore
9697
pub struct TldrContext {
97-
pub language: String,
98+
pub language: TldrLanguage,
99+
}
100+
```
101+
102+
### [`TldrLanguage`]
103+
104+
```rust,ignore
105+
pub enum TldrLanguage {
106+
#[default]
107+
English,
108+
// ...
109+
Other(String),
98110
}
99111
```
100112

@@ -148,6 +160,7 @@ git clone https://github.com/dryrust/tldr.rs.git
148160

149161
[`Tldr`]: https://docs.rs/tldr-traits/latest/tldr_traits/trait.Tldr.html
150162
[`TldrContext`]: https://docs.rs/tldr-traits/latest/tldr_traits/struct.TldrContext.html
163+
[`TldrLanguage`]: https://docs.rs/tldr-traits/latest/tldr_traits/enum.TldrLanguage.html
151164
[`TldrResult`]: https://docs.rs/tldr-traits/latest/tldr_traits/type.TldrResult.html
152165
[`TldrSummary`]: https://docs.rs/tldr-traits/latest/tldr_traits/struct.TldrSummary.html
153166
[`ToTldr`]: https://docs.rs/tldr-traits/latest/tldr_traits/trait.ToTldr.html

src/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
// This is free and unencumbered software released into the public domain.
22

3-
use alloc::string::String;
3+
use crate::TldrLanguage;
44
use core::str::FromStr;
55

6-
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
6+
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
77
pub struct TldrContext {
8-
pub language: String, // TODO
8+
pub language: TldrLanguage,
99
}
1010

1111
impl FromStr for TldrContext {
1212
type Err = ();
1313

1414
fn from_str(input: &str) -> Result<Self, Self::Err> {
1515
Ok(TldrContext {
16-
language: input.into(),
16+
language: input.parse()?,
1717
})
1818
}
1919
}

src/language.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This is free and unencumbered software released into the public domain.
2+
3+
use alloc::string::String;
4+
use core::{fmt, str::FromStr};
5+
6+
#[non_exhaustive]
7+
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
8+
pub enum TldrLanguage {
9+
#[default]
10+
English,
11+
12+
Other(String),
13+
}
14+
15+
impl FromStr for TldrLanguage {
16+
type Err = ();
17+
18+
fn from_str(input: &str) -> Result<Self, Self::Err> {
19+
Ok(match input {
20+
"en" => TldrLanguage::English,
21+
_ => TldrLanguage::Other(input.into()),
22+
})
23+
}
24+
}
25+
26+
impl fmt::Display for TldrLanguage {
27+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28+
match self {
29+
TldrLanguage::English => write!(f, "en"),
30+
TldrLanguage::Other(lang) => write!(f, "{}", lang),
31+
}
32+
}
33+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ extern crate alloc;
1414
mod context;
1515
pub use context::*;
1616

17+
mod language;
18+
pub use language::*;
19+
1720
mod result;
1821
pub use result::*;
1922

0 commit comments

Comments
 (0)