Skip to content

Commit 16ec04e

Browse files
committed
Change the format of SampleIdentifiers and don't make a subdir per locale
1 parent 7a24a22 commit 16ec04e

File tree

2 files changed

+17
-56
lines changed

2 files changed

+17
-56
lines changed

crates/cli/src/commands/templates.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
55
// Please see LICENSE files in the repository root for full details.
66

7-
use std::{collections::BTreeSet, fmt::Write, process::ExitCode};
7+
use std::{fmt::Write, process::ExitCode};
88

99
use anyhow::{Context as _, bail};
1010
use camino::Utf8PathBuf;
@@ -92,19 +92,6 @@ impl Options {
9292
.with_context(|| format!("could not create {out_dir}"))?;
9393
}
9494

95-
let all_locales: BTreeSet<&str> = all_renders
96-
.iter()
97-
.filter_map(|((_, sample_identifier), _)| {
98-
sample_identifier.locale.as_deref()
99-
})
100-
.collect();
101-
for locale in all_locales {
102-
let locale_dir = out_dir.join(locale);
103-
tokio::fs::create_dir(&locale_dir)
104-
.await
105-
.with_context(|| format!("could not create {locale_dir}"))?;
106-
}
107-
10895
for ((template, sample_identifier), template_render) in &all_renders {
10996
let (template_filename_base, template_ext) =
11097
template.rsplit_once('.').unwrap_or((template, "txt"));
@@ -115,20 +102,13 @@ impl Options {
115102
// - `-session2-sample1`
116103
let sample_suffix = {
117104
let mut s = String::new();
118-
if let Some(session_index) = sample_identifier.session_index {
119-
write!(s, "-session{session_index}")?;
105+
for (k, v) in &sample_identifier.components {
106+
write!(s, "-{k}:{v}")?;
120107
}
121-
write!(s, "-sample{}", sample_identifier.index)?;
122108
s
123109
};
124110

125-
let locale_dir = if let Some(locale) = &sample_identifier.locale {
126-
out_dir.join(locale)
127-
} else {
128-
out_dir.clone()
129-
};
130-
131-
let render_path = locale_dir.join(format!(
111+
let render_path = out_dir.join(format!(
132112
"{template_filename_base}{sample_suffix}.{template_ext}"
133113
));
134114

crates/templates/src/context.rs

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -117,48 +117,29 @@ pub trait TemplateContext: Serialize {
117117

118118
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
119119
pub struct SampleIdentifier {
120-
/// A stable locale identifier.
121-
pub locale: Option<String>,
122-
123-
/// A stable identifier for the session that was used in this sample.
124-
pub session_index: Option<usize>,
125-
126-
/// A stable positional index of the sample for this context.
127-
pub index: usize,
120+
pub components: Vec<(&'static str, String)>,
128121
}
129122

130123
impl SampleIdentifier {
131-
pub fn with_locale(&self, locale: String) -> Self {
132-
SampleIdentifier {
133-
locale: Some(locale),
134-
session_index: self.session_index,
135-
index: self.index,
124+
pub fn from_index(index: usize) -> Self {
125+
Self {
126+
components: Vec::default(),
136127
}
128+
.with_appended("index", format!("{index}"))
137129
}
138130

139-
pub fn with_session_index(self, session_index: usize) -> Self {
140-
SampleIdentifier {
141-
locale: self.locale,
142-
session_index: Some(session_index),
143-
index: self.index,
144-
}
131+
pub fn with_appended(&self, kind: &'static str, locale: String) -> Self {
132+
let mut new = self.clone();
133+
new.components.push((kind, locale));
134+
new
145135
}
146136
}
147137

148138
pub(crate) fn sample_list<T: TemplateContext>(samples: Vec<T>) -> BTreeMap<SampleIdentifier, T> {
149139
samples
150140
.into_iter()
151141
.enumerate()
152-
.map(|(index, sample)| {
153-
(
154-
SampleIdentifier {
155-
locale: None,
156-
session_index: None,
157-
index,
158-
},
159-
sample,
160-
)
161-
})
142+
.map(|(index, sample)| (SampleIdentifier::from_index(index), sample))
162143
.collect()
163144
}
164145

@@ -215,7 +196,7 @@ impl<T: TemplateContext> TemplateContext for WithLanguage<T> {
215196
.into_iter()
216197
.map(|(sample_id, sample)| {
217198
(
218-
sample_id.with_locale(locale.to_string()),
199+
sample_id.with_appended("locale", locale.to_string()),
219200
WithLanguage {
220201
lang: locale.to_string(),
221202
inner: sample,
@@ -286,7 +267,7 @@ impl<T: TemplateContext> TemplateContext for WithSession<T> {
286267
.into_iter()
287268
.map(move |(k, inner)| {
288269
(
289-
k.with_session_index(session_index),
270+
k.with_appended("browser-session", session_index.to_string()),
290271
WithSession {
291272
current_session: session.clone(),
292273
inner,
@@ -327,7 +308,7 @@ impl<T: TemplateContext> TemplateContext for WithOptionalSession<T> {
327308
.map(move |(k, inner)| {
328309
(
329310
if session.is_some() {
330-
k.with_session_index(session_index)
311+
k.with_appended("browser-session", session_index.to_string())
331312
} else {
332313
k
333314
},

0 commit comments

Comments
 (0)