Change the format of SampleIdentifiers and don't make a subdir per locale
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
// Please see LICENSE files in the repository root for full details.
|
||||
|
||||
use std::{collections::BTreeSet, fmt::Write, process::ExitCode};
|
||||
use std::{fmt::Write, process::ExitCode};
|
||||
|
||||
use anyhow::{Context as _, bail};
|
||||
use camino::Utf8PathBuf;
|
||||
@@ -92,19 +92,6 @@ impl Options {
|
||||
.with_context(|| format!("could not create {out_dir}"))?;
|
||||
}
|
||||
|
||||
let all_locales: BTreeSet<&str> = all_renders
|
||||
.iter()
|
||||
.filter_map(|((_, sample_identifier), _)| {
|
||||
sample_identifier.locale.as_deref()
|
||||
})
|
||||
.collect();
|
||||
for locale in all_locales {
|
||||
let locale_dir = out_dir.join(locale);
|
||||
tokio::fs::create_dir(&locale_dir)
|
||||
.await
|
||||
.with_context(|| format!("could not create {locale_dir}"))?;
|
||||
}
|
||||
|
||||
for ((template, sample_identifier), template_render) in &all_renders {
|
||||
let (template_filename_base, template_ext) =
|
||||
template.rsplit_once('.').unwrap_or((template, "txt"));
|
||||
@@ -115,20 +102,13 @@ impl Options {
|
||||
// - `-session2-sample1`
|
||||
let sample_suffix = {
|
||||
let mut s = String::new();
|
||||
if let Some(session_index) = sample_identifier.session_index {
|
||||
write!(s, "-session{session_index}")?;
|
||||
for (k, v) in &sample_identifier.components {
|
||||
write!(s, "-{k}:{v}")?;
|
||||
}
|
||||
write!(s, "-sample{}", sample_identifier.index)?;
|
||||
s
|
||||
};
|
||||
|
||||
let locale_dir = if let Some(locale) = &sample_identifier.locale {
|
||||
out_dir.join(locale)
|
||||
} else {
|
||||
out_dir.clone()
|
||||
};
|
||||
|
||||
let render_path = locale_dir.join(format!(
|
||||
let render_path = out_dir.join(format!(
|
||||
"{template_filename_base}{sample_suffix}.{template_ext}"
|
||||
));
|
||||
|
||||
|
||||
@@ -117,31 +117,21 @@ pub trait TemplateContext: Serialize {
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct SampleIdentifier {
|
||||
/// A stable locale identifier.
|
||||
pub locale: Option<String>,
|
||||
|
||||
/// A stable identifier for the session that was used in this sample.
|
||||
pub session_index: Option<usize>,
|
||||
|
||||
/// A stable positional index of the sample for this context.
|
||||
pub index: usize,
|
||||
pub components: Vec<(&'static str, String)>,
|
||||
}
|
||||
|
||||
impl SampleIdentifier {
|
||||
pub fn with_locale(&self, locale: String) -> Self {
|
||||
SampleIdentifier {
|
||||
locale: Some(locale),
|
||||
session_index: self.session_index,
|
||||
index: self.index,
|
||||
pub fn from_index(index: usize) -> Self {
|
||||
Self {
|
||||
components: Vec::default(),
|
||||
}
|
||||
.with_appended("index", format!("{index}"))
|
||||
}
|
||||
|
||||
pub fn with_session_index(self, session_index: usize) -> Self {
|
||||
SampleIdentifier {
|
||||
locale: self.locale,
|
||||
session_index: Some(session_index),
|
||||
index: self.index,
|
||||
}
|
||||
pub fn with_appended(&self, kind: &'static str, locale: String) -> Self {
|
||||
let mut new = self.clone();
|
||||
new.components.push((kind, locale));
|
||||
new
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,16 +139,7 @@ pub(crate) fn sample_list<T: TemplateContext>(samples: Vec<T>) -> BTreeMap<Sampl
|
||||
samples
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(index, sample)| {
|
||||
(
|
||||
SampleIdentifier {
|
||||
locale: None,
|
||||
session_index: None,
|
||||
index,
|
||||
},
|
||||
sample,
|
||||
)
|
||||
})
|
||||
.map(|(index, sample)| (SampleIdentifier::from_index(index), sample))
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -215,7 +196,7 @@ impl<T: TemplateContext> TemplateContext for WithLanguage<T> {
|
||||
.into_iter()
|
||||
.map(|(sample_id, sample)| {
|
||||
(
|
||||
sample_id.with_locale(locale.to_string()),
|
||||
sample_id.with_appended("locale", locale.to_string()),
|
||||
WithLanguage {
|
||||
lang: locale.to_string(),
|
||||
inner: sample,
|
||||
@@ -286,7 +267,7 @@ impl<T: TemplateContext> TemplateContext for WithSession<T> {
|
||||
.into_iter()
|
||||
.map(move |(k, inner)| {
|
||||
(
|
||||
k.with_session_index(session_index),
|
||||
k.with_appended("browser-session", session_index.to_string()),
|
||||
WithSession {
|
||||
current_session: session.clone(),
|
||||
inner,
|
||||
@@ -327,7 +308,7 @@ impl<T: TemplateContext> TemplateContext for WithOptionalSession<T> {
|
||||
.map(move |(k, inner)| {
|
||||
(
|
||||
if session.is_some() {
|
||||
k.with_session_index(session_index)
|
||||
k.with_appended("browser-session", session_index.to_string())
|
||||
} else {
|
||||
k
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user