doido_core/concerns.rs
1//! The concern/mixin pattern (Rails `ActiveSupport::Concern`).
2//!
3//! A concern is a trait that provides shared behavior via default methods; a
4//! type "includes" it by implementing the small required surface. [`Sluggable`]
5//! is a worked example: give it a source string, get a URL slug for free.
6
7/// A reusable concern: derive a URL slug from a source string.
8pub trait Sluggable {
9 /// The text a slug is derived from (e.g. a title).
10 fn sluggable_source(&self) -> &str;
11
12 /// The slug: lowercased, whitespace collapsed to single dashes.
13 fn slug(&self) -> String {
14 self.sluggable_source()
15 .to_lowercase()
16 .split_whitespace()
17 .collect::<Vec<_>>()
18 .join("-")
19 }
20}