1pub(crate) mod labels;
12pub(crate) mod merged;
13pub mod names;
14pub(crate) mod substitute;
15
16use crate::reference::Reference;
17use crate::values::{ComponentValues, ProcHints, ProcValues, RenderContext, RenderOptions};
18use citum_schema::options::SubsequentNameForm;
19use citum_schema::template::{ContributorForm, ContributorRole, TemplateContributor};
20
21#[cfg(test)]
22pub(crate) use names::{NameFormatContext, format_single_name};
23pub use names::{NamesOverrides, format_contributors_short, format_names};
24
25pub(super) fn contributor_for_role(
31 reference: &Reference,
32 role: &ContributorRole,
33) -> Option<citum_schema::reference::Contributor> {
34 match role {
35 ContributorRole::Author => reference.author(),
36 ContributorRole::Editor => reference.editor(),
37 ContributorRole::Translator => reference.translator(),
38 _ => contributor_role_to_reference_role(role).and_then(|role| reference.contributor(role)),
39 }
40}
41
42pub(crate) fn contributor_role_to_reference_role(
44 role: &ContributorRole,
45) -> Option<citum_schema::reference::ContributorRole> {
46 match role {
47 ContributorRole::Author => Some(citum_schema::reference::ContributorRole::Author),
48 ContributorRole::Editor => Some(citum_schema::reference::ContributorRole::Editor),
49 ContributorRole::Translator => Some(citum_schema::reference::ContributorRole::Translator),
50 ContributorRole::Recipient => Some(citum_schema::reference::ContributorRole::Recipient),
51 ContributorRole::Chair => Some(citum_schema::reference::ContributorRole::Unknown(
52 "chair".to_string(),
53 )),
54 ContributorRole::Interviewer => Some(citum_schema::reference::ContributorRole::Interviewer),
55 ContributorRole::Guest => Some(citum_schema::reference::ContributorRole::Guest),
56 ContributorRole::Performer => Some(citum_schema::reference::ContributorRole::Performer),
57 ContributorRole::Director => Some(citum_schema::reference::ContributorRole::Director),
58 ContributorRole::Composer => Some(citum_schema::reference::ContributorRole::Composer),
59 ContributorRole::Writer => Some(citum_schema::reference::ContributorRole::Writer),
60 ContributorRole::Producer => Some(citum_schema::reference::ContributorRole::Producer),
61 ContributorRole::Illustrator => Some(citum_schema::reference::ContributorRole::Illustrator),
62 ContributorRole::Inventor => Some(citum_schema::reference::ContributorRole::Unknown(
63 "inventor".to_string(),
64 )),
65 ContributorRole::Counsel => Some(citum_schema::reference::ContributorRole::Unknown(
66 "counsel".to_string(),
67 )),
68 ContributorRole::CollectionEditor => Some(
69 citum_schema::reference::ContributorRole::Unknown("collection-editor".to_string()),
70 ),
71 ContributorRole::ContainerAuthor => Some(
72 citum_schema::reference::ContributorRole::Unknown("container-author".to_string()),
73 ),
74 ContributorRole::EditorialDirector => Some(
75 citum_schema::reference::ContributorRole::Unknown("editorial-director".to_string()),
76 ),
77 ContributorRole::TextualEditor => Some(citum_schema::reference::ContributorRole::Unknown(
78 "textual-editor".to_string(),
79 )),
80 ContributorRole::OriginalAuthor => Some(citum_schema::reference::ContributorRole::Unknown(
81 "original-author".to_string(),
82 )),
83 ContributorRole::ReviewedAuthor => Some(citum_schema::reference::ContributorRole::Unknown(
84 "reviewed-author".to_string(),
85 )),
86 ContributorRole::Unknown(role) => Some(match role.as_str() {
87 "compiler" => citum_schema::reference::ContributorRole::Compiler,
88 "performer" => citum_schema::reference::ContributorRole::Performer,
89 "narrator" => citum_schema::reference::ContributorRole::Narrator,
90 "host" => citum_schema::reference::ContributorRole::Host,
91 "producer" | "executive-producer" => citum_schema::reference::ContributorRole::Producer,
92 "writer" => citum_schema::reference::ContributorRole::Writer,
93 _ => citum_schema::reference::ContributorRole::Unknown(role.clone()),
94 }),
95 ContributorRole::Interviewee | ContributorRole::Publisher => None,
96 _ => None,
97 }
98}
99
100pub(super) fn is_role_label_omitted(options: &RenderOptions<'_>, role: &ContributorRole) -> bool {
104 options
105 .config
106 .contributors
107 .as_ref()
108 .and_then(|c| c.role.as_ref())
109 .is_some_and(|role_opts| {
110 role_opts
111 .omit
112 .iter()
113 .any(|entry| entry.eq_ignore_ascii_case(role.as_str()))
114 })
115}
116
117pub(super) fn format_role_term<F: crate::render::format::OutputFormat<Output = String>>(
122 term: &str,
123 fmt: &F,
124 effective_rendering: &citum_schema::template::Rendering,
125 options: &RenderOptions<'_>,
126 prefix: &str,
127 suffix: &str,
128) -> String {
129 let term_str = normalized_role_term(term, effective_rendering, options);
130 fmt.text(&format!("{prefix}{term_str}{suffix}"))
131}
132
133fn normalized_role_term(
134 term: &str,
135 effective_rendering: &citum_schema::template::Rendering,
136 options: &RenderOptions<'_>,
137) -> String {
138 let term_str = if crate::values::should_strip_periods(effective_rendering, options) {
139 crate::values::strip_trailing_periods(term)
140 } else {
141 term.to_string()
142 };
143 match effective_rendering.text_case {
149 Some(citum_schema::options::titles::TextCase::CapitalizeFirst) => {
150 crate::values::text_case::capitalize_first_word(&term_str)
151 }
152 _ => term_str,
153 }
154}
155
156pub(super) fn format_wrapped_role_term<F: crate::render::format::OutputFormat<Output = String>>(
162 term: &str,
163 fmt: &F,
164 effective_rendering: &citum_schema::template::Rendering,
165 options: &RenderOptions<'_>,
166 affixes: (&str, &str),
167 wrap: Option<&citum_schema::template::WrapConfig>,
168 item_language: Option<&str>,
169) -> String {
170 let (prefix, suffix) = affixes;
171 let Some(wrap) = wrap else {
172 return format_role_term(term, fmt, effective_rendering, options, prefix, suffix);
173 };
174 let term = normalized_role_term(term, effective_rendering, options);
175 let content = fmt.text(&term);
176 let content = fmt.inner_affix(
177 wrap.inner_prefix.as_deref().unwrap_or_default(),
178 content,
179 wrap.inner_suffix.as_deref().unwrap_or_default(),
180 );
181 let marks = crate::render::format::QuoteMarks::from(&options.locale.grammar_options);
182 let (script, realization) = crate::values::punctuation_realization_context(
183 item_language,
184 options.config.multilingual.as_ref(),
185 );
186 let content = fmt.wrap_punctuation(&wrap.punctuation, content, &marks, script, realization);
187 format!("{}{content}{}", fmt.text(prefix), fmt.text(suffix))
188}
189
190fn apply_integral_subsequent_form(
193 component: &mut TemplateContributor,
194 hints: &ProcHints,
195 options: &RenderOptions<'_>,
196) {
197 if options.context != RenderContext::Citation {
198 return;
199 }
200 if !matches!(options.mode, citum_schema::citation::CitationMode::Integral) {
201 return;
202 }
203 if !component.contributor.contains(&ContributorRole::Author) {
204 return;
205 }
206 if !matches!(
207 hints.integral_name_state,
208 Some(citum_schema::citation::IntegralNameState::Subsequent)
209 ) {
210 return;
211 }
212 let Some(memory) = options.config.integral_name_memory.as_ref() else {
213 return;
214 };
215 component.form = match memory.resolve().subsequent_form {
216 SubsequentNameForm::Short => ContributorForm::Short,
217 SubsequentNameForm::FamilyOnly => ContributorForm::FamilyOnly,
218 };
219}
220
221fn format_contributor_names(
223 component: &TemplateContributor,
224 role: &ContributorRole,
225 names_vec: &[crate::reference::FlatName],
226 reference: &Reference,
227 effective_rendering: &citum_schema::template::Rendering,
228 options: &RenderOptions<'_>,
229 hints: &ProcHints,
230) -> String {
231 let effective_name_order = component.name_order.as_ref().or_else(|| {
232 options
233 .config
234 .contributors
235 .as_ref()?
236 .effective_role_name_order(role)
237 });
238 let effective_shorten = component
239 .shorten
240 .as_ref()
241 .or_else(|| options.config.contributors.as_ref()?.shorten.as_ref());
242
243 let effective_name_form = component.name_form.or(effective_rendering.name_form);
248
249 let name_overrides = names::NamesOverrides {
250 name_order: effective_name_order,
251 sort_separator: component.sort_separator.as_ref(),
252 delimiter: component.delimiter.as_ref(),
253 shorten: effective_shorten,
254 and: component.and.as_ref(),
255 initialize_with: effective_rendering.initialize_with.as_ref(),
256 name_form: effective_name_form,
257 strip_periods: effective_rendering.strip_periods,
258 item_language: crate::values::effective_item_language(reference),
259 };
260 names::format_names(names_vec, &component.form, options, &name_overrides, hints)
261}
262
263impl ComponentValues for TemplateContributor {
264 #[allow(
265 clippy::too_many_lines,
266 reason = "large match statement for contributor role dispatch"
267 )]
268 fn values<F: crate::render::format::OutputFormat<Output = String>>(
269 &self,
270 reference: &Reference,
271 hints: &ProcHints,
272 options: &RenderOptions<'_>,
273 ) -> Option<ProcValues<F::Output>> {
274 let fmt = F::default();
275
276 let mut component = self.clone();
277 let effective_rendering = self.rendering.clone();
278
279 apply_integral_subsequent_form(&mut component, hints, options);
281
282 if effective_rendering.suppress == Some(true) {
284 return None;
285 }
286
287 let Some(role) = component.contributor.as_single().cloned() else {
288 return merged::values::<F>(
289 &component,
290 reference,
291 hints,
292 options,
293 &effective_rendering,
294 &fmt,
295 );
296 };
297
298 if merged::is_role_suppressed(reference, &role, &options.config) {
299 return None;
300 }
301
302 let substitute = citum_schema::options::SubstituteConfig::resolve_or_default(
304 options.config.substitute.as_ref(),
305 );
306
307 if matches!(role, ContributorRole::Author) {
311 if options.suppress_author {
312 return None;
313 }
314 return substitute::resolve_author_substitute::<F>(
315 &component,
316 hints,
317 options,
318 reference,
319 &effective_rendering,
320 &fmt,
321 substitute.as_ref(),
322 );
323 }
324
325 let contributor = contributor_for_role(reference, &role);
326
327 if substitute::is_role_suppressed_by_substitute(&role, substitute.as_ref(), reference) {
331 return None;
332 }
333
334 let names_vec = if let Some(contrib) = contributor {
336 substitute::resolve_multilingual_for_contrib(&contrib, options)
337 } else {
338 Vec::new()
339 };
340
341 if names_vec.is_empty() {
343 return substitute::resolve_role_substitute::<F>(
344 &role,
345 &component,
346 hints,
347 options,
348 reference,
349 &effective_rendering,
350 &fmt,
351 substitute.as_ref(),
352 );
353 }
354
355 let formatted = format_contributor_names(
356 &component,
357 &role,
358 &names_vec,
359 reference,
360 &effective_rendering,
361 options,
362 hints,
363 );
364
365 let role_omitted = is_role_label_omitted(options, &role);
366 let (role_prefix, role_suffix) =
367 labels::resolve_role_labels::<F>(labels::RoleLabelContext {
368 component: &component,
369 role: &role,
370 reference,
371 names_count: names_vec.len(),
372 effective_rendering: &effective_rendering,
373 options,
374 fmt: &fmt,
375 role_omitted,
376 });
377
378 let is_pre_formatted = role_prefix.is_some() || role_suffix.is_some();
379 let formatted = crate::values::apply_abbreviation(formatted, options.abbreviation_map);
380 let final_value = if is_pre_formatted {
381 fmt.text(&formatted)
382 } else {
383 formatted
384 };
385
386 Some(ProcValues {
387 value: final_value,
388 prefix: role_prefix,
389 suffix: role_suffix,
390 url: crate::values::resolve_effective_url(
391 component.links.as_ref(),
392 options.config.links.as_ref(),
393 reference,
394 citum_schema::options::LinkAnchor::Component,
395 ),
396 substituted_key: None,
397 pre_formatted: is_pre_formatted,
398 })
399 }
400}