Skip to main content

citum_engine/values/
type_label.rs

1/*
2SPDX-License-Identifier: MIT OR Apache-2.0
3SPDX-FileCopyrightText: © 2023-2026 Bruce D'Arcus and Citum contributors
4*/
5
6//! Rendering logic for the `type-label` component: a localized description
7//! of a reference's own type (e.g. "Dataset", "Classical work"), with a
8//! `genre`/`medium` fallback before the locale term lookup.
9//!
10//! See `docs/specs/TYPE_CLASSIFICATION_CENTRALIZATION.md`.
11
12use crate::reference::Reference;
13use crate::values::{ComponentValues, ProcHints, ProcValues, RenderOptions};
14use citum_schema::locale::TermForm;
15use citum_schema::template::TemplateTypeLabel;
16
17impl ComponentValues for TemplateTypeLabel {
18    fn values<F: crate::render::format::OutputFormat<Output = String>>(
19        &self,
20        reference: &Reference,
21        _hints: &ProcHints,
22        options: &RenderOptions<'_>,
23    ) -> Option<ProcValues<F::Output>> {
24        let effective_rendering = self.rendering.clone();
25
26        let mut value = resolve_type_label_text(reference, options)?;
27
28        if crate::values::should_strip_periods(&effective_rendering, options) {
29            value = crate::values::strip_trailing_periods(&value);
30        }
31
32        if let Some(tc) = effective_rendering.text_case {
33            value = crate::values::text_case::apply_text_case(&value, tc);
34        }
35
36        if value.is_empty() {
37            None
38        } else {
39            Some(ProcValues {
40                value,
41                pre_formatted: false,
42                ..Default::default()
43            })
44        }
45    }
46}
47
48/// Resolve the reference-type label text: `genre` (unless it merely
49/// restates `ref_type`), else `medium`, else a locale term keyed by
50/// `ref_type`.
51fn resolve_type_label_text(reference: &Reference, options: &RenderOptions<'_>) -> Option<String> {
52    let ref_type = reference.ref_type();
53
54    if let Some(genre) = reference.genre().filter(|genre| *genre != ref_type) {
55        return Some(options.locale.lookup_genre(&genre));
56    }
57
58    if let Some(medium) = reference.medium() {
59        return Some(options.locale.lookup_medium(&medium));
60    }
61
62    options
63        .locale
64        .resolved_type_term(&ref_type, &TermForm::Long)
65}