renox-admin 1.2.1

An admin panel generated from a Renox app's models: lists, forms, filters, bulk actions and exports.
Documentation
//! A record's details on its view page: what [`AdminResource::entries`]
//! returns, drawn with the UI kit's `infolist` and `entry`.
//!
//! [`AdminResource::entries`]: crate::AdminResource::entries

use std::collections::BTreeMap;

use renox::grid::{Column, Kind};
use serde::Serialize;

/// One labelled value on a record's view page. `format` is one of the
/// kit's `entry` formats: `date`, `datetime`, `since`, `money`, `quantity`, `number`,
/// `bool`, `color`, `image`, `markdown`, or none for text.
///
/// ```
/// use renox_admin::Entry;
///
/// let entries = vec![
///     Entry::text("name", "Name").copyable(),
///     Entry::new("status", "Status").labels([("live", "Live")]).badge(),
///     Entry::new("price", "Price").format("money"),
///     Entry::new("notes", "Notes").format("markdown").span_full(),
/// ];
/// # assert_eq!(entries.len(), 4);
/// ```
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Entry {
    key: String,
    label: String,
    format: Option<String>,
    labels: BTreeMap<String, String>,
    badge: bool,
    copyable: bool,
    span: Option<String>,
    currency_from: Option<String>,
    scale: u32,
    unit_from: Option<String>,
}

impl Entry {
    /// The record's `key`, shown as text under `label` until `format` says
    /// otherwise.
    pub fn new(key: &str, label: &str) -> Self {
        Self {
            key: key.to_owned(),
            label: label.to_owned(),
            format: None,
            labels: BTreeMap::new(),
            badge: false,
            copyable: false,
            span: None,
            currency_from: None,
            scale: 0,
            unit_from: None,
        }
    }

    /// The record's `key`, as text.
    pub fn text(key: &str, label: &str) -> Self {
        Self::new(key, label)
    }

    /// How the value is shown: `date`, `datetime`, `since`, `money` (an
    /// amount in the smallest unit, like the grid's money columns),
    /// `number`, `bool`, `color`, `image` or `markdown`.
    pub fn format(mut self, format: &str) -> Self {
        self.format = Some(format.to_owned());
        self
    }

    /// Names for values (`("live", "Live")`), as a select's options.
    pub fn labels<V: Into<String>, L: Into<String>>(
        mut self,
        labels: impl IntoIterator<Item = (V, L)>,
    ) -> Self {
        self.labels = labels
            .into_iter()
            .map(|(value, label)| (value.into(), label.into()))
            .collect();
        self
    }

    /// For a `money` entry: shows the amount in the currency the record
    /// keeps in its `key` field (an ISO 4217 code such as `total_currency`),
    /// with that currency's symbol and decimals. Without it the entry uses
    /// `APP_CURRENCY`.
    ///
    /// ```
    /// use renox_admin::Entry;
    ///
    /// let total = Entry::new("total", "Total")
    ///     .format("money")
    ///     .currency_from("total_currency");
    /// # let _ = total;
    /// ```
    pub fn currency_from(mut self, key: &str) -> Self {
        self.currency_from = Some(key.to_owned());
        self
    }

    /// For a `quantity` entry: the decimals of its `Quantity<SCALE>` (at
    /// most 9), so `2.5` shows as `2.500` with `3`.
    ///
    /// ```
    /// use renox_admin::Entry;
    ///
    /// let stock = Entry::new("stock", "Stock").format("quantity").scale(3);
    /// # let _ = stock;
    /// ```
    pub fn scale(mut self, scale: u32) -> Self {
        self.scale = scale.min(9);
        self
    }

    /// For a `quantity` entry: shows the quantity with the unit the record
    /// keeps in its `key` field (such as `stock_unit`), after the value.
    ///
    /// ```
    /// use renox_admin::Entry;
    ///
    /// let stock = Entry::new("stock", "Stock")
    ///     .format("quantity")
    ///     .scale(3)
    ///     .unit_from("stock_unit");
    /// # let _ = stock;
    /// ```
    pub fn unit_from(mut self, key: &str) -> Self {
        self.unit_from = Some(key.to_owned());
        self
    }

    /// Shows the value as a badge.
    pub fn badge(mut self) -> Self {
        self.badge = true;
        self
    }

    /// Adds a copy button.
    pub fn copyable(mut self) -> Self {
        self.copyable = true;
        self
    }

    /// Takes the list's whole width.
    pub fn span_full(mut self) -> Self {
        self.span = Some("full".into());
        self
    }

    /// The entry a grid column suggests: its key, its heading and a format
    /// that fits its kind (`None` for `custom` columns, which only the page
    /// can draw).
    pub fn from_column(column: &Column) -> Option<Self> {
        let entry = Self::new(column.key(), column.label());
        let entry = match column.kind() {
            Kind::Custom => return None,
            Kind::Number => entry.format("number"),
            Kind::Money => {
                let entry = entry.format("money");
                match column.currency_column() {
                    Some(key) => entry.currency_from(key),
                    None => entry,
                }
            }
            Kind::Quantity => {
                let entry = entry.format("quantity").scale(column.quantity_scale());
                match column.unit_column() {
                    Some(key) => entry.unit_from(key),
                    None => entry,
                }
            }
            Kind::Date => entry.format("date"),
            Kind::DateTime => entry.format("datetime"),
            Kind::Bool => entry.format("bool"),
            Kind::Image => entry.format("image"),
            Kind::Color => entry.format("color"),
            Kind::Select | Kind::Tags => entry.labels(column.options().iter().cloned()).badge(),
            _ => entry,
        };
        Some(entry)
    }

    /// The entry's key.
    pub fn key(&self) -> &str {
        &self.key
    }

    /// Runs the entry's label and its value names through `translate`.
    pub(crate) fn translate(&mut self, translate: &dyn Fn(&str) -> String) {
        self.label = translate(&self.label);
        for label in self.labels.values_mut() {
            *label = translate(label);
        }
    }
}