1#![allow(internal_features)]
3#![doc(rust_logo)]
4#![feature(rustc_attrs)]
5#![feature(rustdoc_internals)]
6use std::borrow::Cow;
9use std::error::Error;
10use std::path::Path;
11use std::sync::{Arc, LazyLock};
12use std::{fmt, fs, io};
13
14use fluent_bundle::FluentResource;
15pub use fluent_bundle::types::FluentType;
16pub use fluent_bundle::{self, FluentArgs, FluentError, FluentValue};
17use fluent_syntax::parser::ParserError;
18use intl_memoizer::concurrent::IntlLangMemoizer;
19use rustc_data_structures::sync::{DynSend, IntoDynSyncSend};
20use rustc_macros::{Decodable, Encodable};
21use rustc_span::Span;
22use tracing::{instrument, trace};
23pub use unic_langid::{LanguageIdentifier, langid};
24
25mod diagnostic_impls;
26pub use diagnostic_impls::DiagArgFromDisplay;
27
28pub type FluentBundle =
29 IntoDynSyncSend<fluent_bundle::bundle::FluentBundle<FluentResource, IntlLangMemoizer>>;
30
31fn new_bundle(locales: Vec<LanguageIdentifier>) -> FluentBundle {
32 IntoDynSyncSend(fluent_bundle::bundle::FluentBundle::new_concurrent(locales))
33}
34
35#[derive(Debug)]
36pub enum TranslationBundleError {
37 ReadFtl(io::Error),
39 ParseFtl(ParserError),
41 AddResource(FluentError),
43 MissingLocale,
45 ReadLocalesDir(io::Error),
47 ReadLocalesDirEntry(io::Error),
49 LocaleIsNotDir,
51}
52
53impl fmt::Display for TranslationBundleError {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 match self {
56 TranslationBundleError::ReadFtl(e) => write!(f, "could not read ftl file: {e}"),
57 TranslationBundleError::ParseFtl(e) => {
58 write!(f, "could not parse ftl file: {e}")
59 }
60 TranslationBundleError::AddResource(e) => write!(f, "failed to add resource: {e}"),
61 TranslationBundleError::MissingLocale => write!(f, "missing locale directory"),
62 TranslationBundleError::ReadLocalesDir(e) => {
63 write!(f, "could not read locales dir: {e}")
64 }
65 TranslationBundleError::ReadLocalesDirEntry(e) => {
66 write!(f, "could not read locales dir entry: {e}")
67 }
68 TranslationBundleError::LocaleIsNotDir => {
69 write!(f, "`$sysroot/share/locales/$locale` is not a directory")
70 }
71 }
72 }
73}
74
75impl Error for TranslationBundleError {
76 fn source(&self) -> Option<&(dyn Error + 'static)> {
77 match self {
78 TranslationBundleError::ReadFtl(e) => Some(e),
79 TranslationBundleError::ParseFtl(e) => Some(e),
80 TranslationBundleError::AddResource(e) => Some(e),
81 TranslationBundleError::MissingLocale => None,
82 TranslationBundleError::ReadLocalesDir(e) => Some(e),
83 TranslationBundleError::ReadLocalesDirEntry(e) => Some(e),
84 TranslationBundleError::LocaleIsNotDir => None,
85 }
86 }
87}
88
89impl From<(FluentResource, Vec<ParserError>)> for TranslationBundleError {
90 fn from((_, mut errs): (FluentResource, Vec<ParserError>)) -> Self {
91 TranslationBundleError::ParseFtl(errs.pop().expect("failed ftl parse with no errors"))
92 }
93}
94
95impl From<Vec<FluentError>> for TranslationBundleError {
96 fn from(mut errs: Vec<FluentError>) -> Self {
97 TranslationBundleError::AddResource(
98 errs.pop().expect("failed adding resource to bundle with no errors"),
99 )
100 }
101}
102
103#[instrument(level = "trace")]
109pub fn fluent_bundle(
110 sysroot_candidates: &[&Path],
111 requested_locale: Option<LanguageIdentifier>,
112 additional_ftl_path: Option<&Path>,
113 with_directionality_markers: bool,
114) -> Result<Option<Arc<FluentBundle>>, TranslationBundleError> {
115 if requested_locale.is_none() && additional_ftl_path.is_none() {
116 return Ok(None);
117 }
118
119 let fallback_locale = langid!("en-US");
120 let requested_fallback_locale = requested_locale.as_ref() == Some(&fallback_locale);
121 trace!(?requested_fallback_locale);
122 if requested_fallback_locale && additional_ftl_path.is_none() {
123 return Ok(None);
124 }
125 let locale = requested_locale.clone().unwrap_or(fallback_locale);
128 trace!(?locale);
129 let mut bundle = new_bundle(vec![locale]);
130
131 register_functions(&mut bundle);
133
134 bundle.set_use_isolating(with_directionality_markers);
140
141 if let Some(requested_locale) = requested_locale {
143 let mut found_resources = false;
144 for sysroot in sysroot_candidates {
145 let mut sysroot = sysroot.to_path_buf();
146 sysroot.push("share");
147 sysroot.push("locale");
148 sysroot.push(requested_locale.to_string());
149 trace!(?sysroot);
150
151 if !sysroot.exists() {
152 trace!("skipping");
153 continue;
154 }
155
156 if !sysroot.is_dir() {
157 return Err(TranslationBundleError::LocaleIsNotDir);
158 }
159
160 for entry in sysroot.read_dir().map_err(TranslationBundleError::ReadLocalesDir)? {
161 let entry = entry.map_err(TranslationBundleError::ReadLocalesDirEntry)?;
162 let path = entry.path();
163 trace!(?path);
164 if path.extension().and_then(|s| s.to_str()) != Some("ftl") {
165 trace!("skipping");
166 continue;
167 }
168
169 let resource_str =
170 fs::read_to_string(path).map_err(TranslationBundleError::ReadFtl)?;
171 let resource =
172 FluentResource::try_new(resource_str).map_err(TranslationBundleError::from)?;
173 trace!(?resource);
174 bundle.add_resource(resource).map_err(TranslationBundleError::from)?;
175 found_resources = true;
176 }
177 }
178
179 if !found_resources {
180 return Err(TranslationBundleError::MissingLocale);
181 }
182 }
183
184 if let Some(additional_ftl_path) = additional_ftl_path {
185 let resource_str =
186 fs::read_to_string(additional_ftl_path).map_err(TranslationBundleError::ReadFtl)?;
187 let resource =
188 FluentResource::try_new(resource_str).map_err(TranslationBundleError::from)?;
189 trace!(?resource);
190 bundle.add_resource_overriding(resource);
191 }
192
193 let bundle = Arc::new(bundle);
194 Ok(Some(bundle))
195}
196
197fn register_functions(bundle: &mut FluentBundle) {
198 bundle
199 .add_function("STREQ", |positional, _named| match positional {
200 [FluentValue::String(a), FluentValue::String(b)] => format!("{}", (a == b)).into(),
201 _ => FluentValue::Error,
202 })
203 .expect("Failed to add a function to the bundle.");
204}
205
206pub type LazyFallbackBundle =
209 Arc<LazyLock<FluentBundle, Box<dyn FnOnce() -> FluentBundle + DynSend>>>;
210
211#[instrument(level = "trace", skip(resources))]
213pub fn fallback_fluent_bundle(
214 resources: Vec<&'static str>,
215 with_directionality_markers: bool,
216) -> LazyFallbackBundle {
217 Arc::new(LazyLock::new(Box::new(move || {
218 let mut fallback_bundle = new_bundle(vec![langid!("en-US")]);
219
220 register_functions(&mut fallback_bundle);
221
222 fallback_bundle.set_use_isolating(with_directionality_markers);
224
225 for resource in resources {
226 let resource = FluentResource::try_new(resource.to_string())
227 .expect("failed to parse fallback fluent resource");
228 fallback_bundle.add_resource_overriding(resource);
229 }
230
231 fallback_bundle
232 })))
233}
234
235type FluentId = Cow<'static, str>;
237
238#[rustc_diagnostic_item = "SubdiagMessage"]
246pub enum SubdiagMessage {
247 Str(Cow<'static, str>),
249 Translated(Cow<'static, str>),
256 FluentIdentifier(FluentId),
259 FluentAttr(FluentId),
265}
266
267impl From<String> for SubdiagMessage {
268 fn from(s: String) -> Self {
269 SubdiagMessage::Str(Cow::Owned(s))
270 }
271}
272impl From<&'static str> for SubdiagMessage {
273 fn from(s: &'static str) -> Self {
274 SubdiagMessage::Str(Cow::Borrowed(s))
275 }
276}
277impl From<Cow<'static, str>> for SubdiagMessage {
278 fn from(s: Cow<'static, str>) -> Self {
279 SubdiagMessage::Str(s)
280 }
281}
282
283#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
288#[rustc_diagnostic_item = "DiagMessage"]
289pub enum DiagMessage {
290 Str(Cow<'static, str>),
292 Translated(Cow<'static, str>),
299 FluentIdentifier(FluentId, Option<FluentId>),
305}
306
307impl DiagMessage {
308 pub fn with_subdiagnostic_message(&self, sub: SubdiagMessage) -> Self {
314 let attr = match sub {
315 SubdiagMessage::Str(s) => return DiagMessage::Str(s),
316 SubdiagMessage::Translated(s) => return DiagMessage::Translated(s),
317 SubdiagMessage::FluentIdentifier(id) => {
318 return DiagMessage::FluentIdentifier(id, None);
319 }
320 SubdiagMessage::FluentAttr(attr) => attr,
321 };
322
323 match self {
324 DiagMessage::Str(s) => DiagMessage::Str(s.clone()),
325 DiagMessage::Translated(s) => DiagMessage::Translated(s.clone()),
326 DiagMessage::FluentIdentifier(id, _) => {
327 DiagMessage::FluentIdentifier(id.clone(), Some(attr))
328 }
329 }
330 }
331
332 pub fn as_str(&self) -> Option<&str> {
333 match self {
334 DiagMessage::Translated(s) | DiagMessage::Str(s) => Some(s),
335 DiagMessage::FluentIdentifier(_, _) => None,
336 }
337 }
338}
339
340impl From<String> for DiagMessage {
341 fn from(s: String) -> Self {
342 DiagMessage::Str(Cow::Owned(s))
343 }
344}
345impl From<&'static str> for DiagMessage {
346 fn from(s: &'static str) -> Self {
347 DiagMessage::Str(Cow::Borrowed(s))
348 }
349}
350impl From<Cow<'static, str>> for DiagMessage {
351 fn from(s: Cow<'static, str>) -> Self {
352 DiagMessage::Str(s)
353 }
354}
355
356impl From<DiagMessage> for SubdiagMessage {
362 fn from(val: DiagMessage) -> Self {
363 match val {
364 DiagMessage::Str(s) => SubdiagMessage::Str(s),
365 DiagMessage::Translated(s) => SubdiagMessage::Translated(s),
366 DiagMessage::FluentIdentifier(id, None) => SubdiagMessage::FluentIdentifier(id),
367 DiagMessage::FluentIdentifier(_, Some(attr)) => SubdiagMessage::FluentAttr(attr),
370 }
371 }
372}
373
374#[derive(Clone, Debug)]
376pub struct SpanLabel {
377 pub span: Span,
379
380 pub is_primary: bool,
383
384 pub label: Option<DiagMessage>,
386}
387
388#[derive(Clone, Debug, Hash, PartialEq, Eq, Encodable, Decodable)]
397pub struct MultiSpan {
398 primary_spans: Vec<Span>,
399 span_labels: Vec<(Span, DiagMessage)>,
400}
401
402impl MultiSpan {
403 #[inline]
404 pub fn new() -> MultiSpan {
405 MultiSpan { primary_spans: vec![], span_labels: vec![] }
406 }
407
408 pub fn from_span(primary_span: Span) -> MultiSpan {
409 MultiSpan { primary_spans: vec![primary_span], span_labels: vec![] }
410 }
411
412 pub fn from_spans(mut vec: Vec<Span>) -> MultiSpan {
413 vec.sort();
414 MultiSpan { primary_spans: vec, span_labels: vec![] }
415 }
416
417 pub fn push_span_label(&mut self, span: Span, label: impl Into<DiagMessage>) {
418 self.span_labels.push((span, label.into()));
419 }
420
421 pub fn primary_span(&self) -> Option<Span> {
423 self.primary_spans.first().cloned()
424 }
425
426 pub fn primary_spans(&self) -> &[Span] {
428 &self.primary_spans
429 }
430
431 pub fn has_primary_spans(&self) -> bool {
433 !self.is_dummy()
434 }
435
436 pub fn is_dummy(&self) -> bool {
438 self.primary_spans.iter().all(|sp| sp.is_dummy())
439 }
440
441 pub fn replace(&mut self, before: Span, after: Span) -> bool {
444 let mut replacements_occurred = false;
445 for primary_span in &mut self.primary_spans {
446 if *primary_span == before {
447 *primary_span = after;
448 replacements_occurred = true;
449 }
450 }
451 for span_label in &mut self.span_labels {
452 if span_label.0 == before {
453 span_label.0 = after;
454 replacements_occurred = true;
455 }
456 }
457 replacements_occurred
458 }
459
460 pub fn pop_span_label(&mut self) -> Option<(Span, DiagMessage)> {
461 self.span_labels.pop()
462 }
463
464 pub fn span_labels(&self) -> Vec<SpanLabel> {
470 let is_primary = |span| self.primary_spans.contains(&span);
471
472 let mut span_labels = self
473 .span_labels
474 .iter()
475 .map(|&(span, ref label)| SpanLabel {
476 span,
477 is_primary: is_primary(span),
478 label: Some(label.clone()),
479 })
480 .collect::<Vec<_>>();
481
482 for &span in &self.primary_spans {
483 if !span_labels.iter().any(|sl| sl.span == span) {
484 span_labels.push(SpanLabel { span, is_primary: true, label: None });
485 }
486 }
487
488 span_labels
489 }
490
491 pub fn has_span_labels(&self) -> bool {
493 self.span_labels.iter().any(|(sp, _)| !sp.is_dummy())
494 }
495
496 pub fn clone_ignoring_labels(&self) -> Self {
501 Self { primary_spans: self.primary_spans.clone(), ..MultiSpan::new() }
502 }
503}
504
505impl From<Span> for MultiSpan {
506 fn from(span: Span) -> MultiSpan {
507 MultiSpan::from_span(span)
508 }
509}
510
511impl From<Vec<Span>> for MultiSpan {
512 fn from(spans: Vec<Span>) -> MultiSpan {
513 MultiSpan::from_spans(spans)
514 }
515}
516
517fn icu_locale_from_unic_langid(lang: LanguageIdentifier) -> Option<icu_locale::Locale> {
518 icu_locale::Locale::try_from_str(&lang.to_string()).ok()
519}
520
521pub fn fluent_value_from_str_list_sep_by_and(l: Vec<Cow<'_, str>>) -> FluentValue<'_> {
522 #[derive(Clone, PartialEq, Debug)]
524 struct FluentStrListSepByAnd(Vec<String>);
525
526 impl FluentType for FluentStrListSepByAnd {
527 fn duplicate(&self) -> Box<dyn FluentType + Send> {
528 Box::new(self.clone())
529 }
530
531 fn as_string(&self, intls: &intl_memoizer::IntlLangMemoizer) -> Cow<'static, str> {
532 let result = intls
533 .with_try_get::<MemoizableListFormatter, _, _>((), |list_formatter| {
534 list_formatter.format_to_string(self.0.iter())
535 })
536 .unwrap();
537 Cow::Owned(result)
538 }
539
540 fn as_string_threadsafe(
541 &self,
542 intls: &intl_memoizer::concurrent::IntlLangMemoizer,
543 ) -> Cow<'static, str> {
544 let result = intls
545 .with_try_get::<MemoizableListFormatter, _, _>((), |list_formatter| {
546 list_formatter.format_to_string(self.0.iter())
547 })
548 .unwrap();
549 Cow::Owned(result)
550 }
551 }
552
553 struct MemoizableListFormatter(icu_list::ListFormatter);
554
555 impl std::ops::Deref for MemoizableListFormatter {
556 type Target = icu_list::ListFormatter;
557 fn deref(&self) -> &Self::Target {
558 &self.0
559 }
560 }
561
562 impl intl_memoizer::Memoizable for MemoizableListFormatter {
563 type Args = ();
564 type Error = ();
565
566 fn construct(lang: LanguageIdentifier, _args: Self::Args) -> Result<Self, Self::Error>
567 where
568 Self: Sized,
569 {
570 let locale = icu_locale_from_unic_langid(lang)
571 .unwrap_or_else(|| rustc_baked_icu_data::supported_locales::EN);
572 let list_formatter = icu_list::ListFormatter::try_new_and_unstable(
573 &rustc_baked_icu_data::BakedDataProvider,
574 locale.into(),
575 icu_list::options::ListFormatterOptions::default()
576 .with_length(icu_list::options::ListLength::Wide),
577 )
578 .expect("Failed to create list formatter");
579
580 Ok(MemoizableListFormatter(list_formatter))
581 }
582 }
583
584 let l = l.into_iter().map(|x| x.into_owned()).collect();
585
586 FluentValue::Custom(Box::new(FluentStrListSepByAnd(l)))
587}
588
589pub type DiagArg<'iter> = (&'iter DiagArgName, &'iter DiagArgValue);
593
594pub type DiagArgName = Cow<'static, str>;
596
597#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
600pub enum DiagArgValue {
601 Str(Cow<'static, str>),
602 Number(i32),
606 StrListSepByAnd(Vec<Cow<'static, str>>),
607}
608
609pub trait IntoDiagArg {
614 fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue;
621}
622
623impl IntoDiagArg for DiagArgValue {
624 fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
625 self
626 }
627}
628
629impl From<DiagArgValue> for FluentValue<'static> {
630 fn from(val: DiagArgValue) -> Self {
631 match val {
632 DiagArgValue::Str(s) => From::from(s),
633 DiagArgValue::Number(n) => From::from(n),
634 DiagArgValue::StrListSepByAnd(l) => fluent_value_from_str_list_sep_by_and(l),
635 }
636 }
637}