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 prefix: &str,
167 suffix: &str,
168 wrap: Option<&citum_schema::template::WrapConfig>,
169) -> String {
170 let Some(wrap) = wrap else {
171 return format_role_term(term, fmt, effective_rendering, options, prefix, suffix);
172 };
173 let term = normalized_role_term(term, effective_rendering, options);
174 let content = fmt.text(&term);
175 let content = fmt.inner_affix(
176 wrap.inner_prefix.as_deref().unwrap_or_default(),
177 content,
178 wrap.inner_suffix.as_deref().unwrap_or_default(),
179 );
180 let marks = crate::render::format::QuoteMarks::from(&options.locale.grammar_options);
181 let content = fmt.wrap_punctuation(&wrap.punctuation, content, &marks);
182 format!("{}{content}{}", fmt.text(prefix), fmt.text(suffix))
183}
184
185fn apply_integral_subsequent_form(
188 component: &mut TemplateContributor,
189 hints: &ProcHints,
190 options: &RenderOptions<'_>,
191) {
192 if options.context != RenderContext::Citation {
193 return;
194 }
195 if !matches!(options.mode, citum_schema::citation::CitationMode::Integral) {
196 return;
197 }
198 if !component.contributor.contains(&ContributorRole::Author) {
199 return;
200 }
201 if !matches!(
202 hints.integral_name_state,
203 Some(citum_schema::citation::IntegralNameState::Subsequent)
204 ) {
205 return;
206 }
207 let Some(memory) = options.config.integral_name_memory.as_ref() else {
208 return;
209 };
210 component.form = match memory.resolve().subsequent_form {
211 SubsequentNameForm::Short => ContributorForm::Short,
212 SubsequentNameForm::FamilyOnly => ContributorForm::FamilyOnly,
213 };
214}
215
216fn format_contributor_names(
218 component: &TemplateContributor,
219 role: &ContributorRole,
220 names_vec: &[crate::reference::FlatName],
221 effective_rendering: &citum_schema::template::Rendering,
222 options: &RenderOptions<'_>,
223 hints: &ProcHints,
224) -> String {
225 let effective_name_order = component.name_order.as_ref().or_else(|| {
226 options
227 .config
228 .contributors
229 .as_ref()?
230 .effective_role_name_order(role)
231 });
232 let effective_shorten = component
233 .shorten
234 .as_ref()
235 .or_else(|| options.config.contributors.as_ref()?.shorten.as_ref());
236
237 let effective_name_form = component.name_form.or(effective_rendering.name_form);
242
243 let name_overrides = names::NamesOverrides {
244 name_order: effective_name_order,
245 sort_separator: component.sort_separator.as_ref(),
246 shorten: effective_shorten,
247 and: component.and.as_ref(),
248 initialize_with: effective_rendering.initialize_with.as_ref(),
249 name_form: effective_name_form,
250 };
251 names::format_names(names_vec, &component.form, options, &name_overrides, hints)
252}
253
254impl ComponentValues for TemplateContributor {
255 #[allow(
256 clippy::too_many_lines,
257 reason = "large match statement for contributor role dispatch"
258 )]
259 fn values<F: crate::render::format::OutputFormat<Output = String>>(
260 &self,
261 reference: &Reference,
262 hints: &ProcHints,
263 options: &RenderOptions<'_>,
264 ) -> Option<ProcValues<F::Output>> {
265 let fmt = F::default();
266
267 let mut component = self.clone();
268 let effective_rendering = self.rendering.clone();
269
270 apply_integral_subsequent_form(&mut component, hints, options);
272
273 if effective_rendering.suppress == Some(true) {
275 return None;
276 }
277
278 let Some(role) = component.contributor.as_single().cloned() else {
279 return merged::values::<F>(
280 &component,
281 reference,
282 hints,
283 options,
284 &effective_rendering,
285 &fmt,
286 );
287 };
288
289 if merged::is_role_suppressed(reference, &role, &options.config) {
290 return None;
291 }
292
293 let substitute = citum_schema::options::SubstituteConfig::resolve_or_default(
295 options.config.substitute.as_ref(),
296 );
297
298 if matches!(role, ContributorRole::Author) {
302 if options.suppress_author {
303 return None;
304 }
305 return substitute::resolve_author_substitute::<F>(
306 &component,
307 hints,
308 options,
309 reference,
310 &effective_rendering,
311 &fmt,
312 substitute.as_ref(),
313 );
314 }
315
316 let contributor = contributor_for_role(reference, &role);
317
318 if substitute::is_role_suppressed_by_substitute(&role, substitute.as_ref(), reference) {
322 return None;
323 }
324
325 let names_vec = if let Some(contrib) = contributor {
327 substitute::resolve_multilingual_for_contrib(&contrib, options)
328 } else {
329 Vec::new()
330 };
331
332 if names_vec.is_empty() {
334 return substitute::resolve_role_substitute::<F>(
335 &role,
336 &component,
337 hints,
338 options,
339 reference,
340 &effective_rendering,
341 &fmt,
342 substitute.as_ref(),
343 );
344 }
345
346 let formatted = format_contributor_names(
347 &component,
348 &role,
349 &names_vec,
350 &effective_rendering,
351 options,
352 hints,
353 );
354
355 let role_omitted = is_role_label_omitted(options, &role);
356 let (role_prefix, role_suffix) =
357 labels::resolve_role_labels::<F>(labels::RoleLabelContext {
358 component: &component,
359 role: &role,
360 reference,
361 names_count: names_vec.len(),
362 effective_rendering: &effective_rendering,
363 options,
364 fmt: &fmt,
365 role_omitted,
366 });
367
368 let is_pre_formatted = role_prefix.is_some() || role_suffix.is_some();
369 let formatted = crate::values::apply_abbreviation(formatted, options.abbreviation_map);
370 let final_value = if is_pre_formatted {
371 fmt.text(&formatted)
372 } else {
373 formatted
374 };
375
376 Some(ProcValues {
377 value: final_value,
378 prefix: role_prefix,
379 suffix: role_suffix,
380 url: crate::values::resolve_effective_url(
381 component.links.as_ref(),
382 options.config.links.as_ref(),
383 reference,
384 citum_schema::options::LinkAnchor::Component,
385 ),
386 substituted_key: None,
387 pre_formatted: is_pre_formatted,
388 })
389 }
390}