Skip to main content

Crate sea_orm_sluggable

Crate sea_orm_sluggable 

Source
Expand description

Unique, transliterated slugs for SeaORM entities.

Describe the slug once, and it is filled in on every save:

use sea_orm::entity::prelude::*;
use sea_orm_sluggable::{SlugConfig, Sluggable};

impl Sluggable for categories::ActiveModel {
    fn slug_config() -> SlugConfig<Self> {
        SlugConfig::new([categories::Column::Title], categories::Column::Slug)
    }
}

#[async_trait::async_trait]
impl ActiveModelBehavior for categories::ActiveModel {
    async fn before_save<C: ConnectionTrait>(self, db: &C, insert: bool) -> Result<Self, DbErr> {
        // "Rust for PHP developers" -> "rust-for-php-developers",
        // or "rust-for-php-developers-1" when that one is taken.
        Ok(sea_orm_sluggable::fill_slug(self, db, insert).await?)
    }
}

Text is transliterated to ASCII first. The default, AnyAscii, handles any script; enable a language feature (for example uk) for official national rules, or implement Transliterator for your own language.

Modules§

languages
National romanization rules, one module per language, each behind its own feature so apps only compile the languages they use.

Structs§

AnyAscii
Generic transliteration for every script, via the any_ascii crate.
SlugConfig
Which columns a slug is built from and stored in, and when it is generated.
SlugOptions
How slugs are built: separator, length, suffix numbering and transliteration.

Enums§

SlugError
Why a slug could not be generated.

Traits§

Sluggable
Gives an ActiveModel an automatically generated slug.
Transliterator
Turns text in any script into ASCII before it becomes a slug.

Functions§

fill_slug
Fills the slug column of model using Sluggable::slug_config.
fill_slug_with
Same as fill_slug, with an explicit configuration.
find_by_slug
Finds the row of E whose column equals slug.
slugify
Builds a slug from text, without checking the database.
unique_slug
Builds a slug from text that no row of E has in column yet.