diff --git a/crates/cli/src/commands/templates.rs b/crates/cli/src/commands/templates.rs index d841333d7..296680cfd 100644 --- a/crates/cli/src/commands/templates.rs +++ b/crates/cli/src/commands/templates.rs @@ -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}" )); diff --git a/crates/templates/src/context.rs b/crates/templates/src/context.rs index 18a86bd91..597683a03 100644 --- a/crates/templates/src/context.rs +++ b/crates/templates/src/context.rs @@ -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, - - /// A stable identifier for the session that was used in this sample. - pub session_index: Option, - - /// 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(samples: Vec) -> BTreeMap TemplateContext for WithLanguage { .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 TemplateContext for WithSession { .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 TemplateContext for WithOptionalSession { .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 },