Skip to content

Commit

Permalink
added docs and callout
Browse files Browse the repository at this point in the history
  • Loading branch information
jewlexx committed Oct 29, 2024
1 parent 4f83e0e commit 54cb593
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
description = "Simple library to pluralize words"
description = "Simple library to pluralize English words"
edition = "2021"
include = ["src/**/*", "LICENSE", "README.md"]
license = "Apache-2.0"
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

A simple library to pluralize words.

> [!NOTE]
> This library is intended for the English language.
> It may work with other languages that follow the convention of adding an 's' to the end of words,
> but for most languages a custom implementation will be required.
## Usage

```rust
Expand Down
14 changes: 14 additions & 0 deletions src/form.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[allow(clippy::module_name_repetitions)]
/// An enum determining the plural form of a word.
///
/// This currently supports two forms:
///
/// - `AppendS`: Appends an `s` to the end of the word.
/// - `Custom`: A custom form provided by the user.
///
/// This may change in future hence the `#[non_exhaustive]` attribute.
pub enum PluralForm {
/// Append an 's' to the end of the word
AppendS,
/// A custom form provided by the user
///
/// This will simply be passed to the formatter.
Custom(String),
}

Expand Down
13 changes: 13 additions & 0 deletions src/plural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,53 @@ use std::fmt::Display;
use super::PluralForm;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[must_use]
/// Provides a wrapper that implements the [`Pluralize`] trait.
pub struct Plural<'w> {
pub(crate) word: &'w str,
pub(crate) form: PluralForm,
}

impl<'w> Plural<'w> {
/// Construct a new [`Plural`] instance.
pub const fn new(word: &'w str) -> Self {
Self {
word,
form: PluralForm::const_default(),
}
}

/// Construct a new [`Plural`] instance with a custom form.
pub const fn new_with_form(word: &'w str, form: PluralForm) -> Self {
Self { word, form }
}

/// Assign a plural form to the [`Plural`] instance.
pub fn with_form(self, form: PluralForm) -> Self {
Self {
word: self.word,
form,
}
}

#[must_use]
/// Get the word that is being pluralized.
pub const fn word(&self) -> &str {
self.word
}

#[must_use]
/// Determine the plural form of the word.
pub const fn form(&self) -> &PluralForm {
&self.form
}
}

/// Trait providing methods for the pluralization of words.
///
/// This does not handle the implementation itself.
pub trait Pluralize {
/// Get the plural form of the word.
fn plural(&self) -> String;
}

Expand Down

0 comments on commit 54cb593

Please sign in to comment.