summaryrefslogtreecommitdiffstats
path: root/third_party/rust/fluent-bundle/src/args.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/fluent-bundle/src/args.rs
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/fluent-bundle/src/args.rs')
-rw-r--r--third_party/rust/fluent-bundle/src/args.rs74
1 files changed, 74 insertions, 0 deletions
diff --git a/third_party/rust/fluent-bundle/src/args.rs b/third_party/rust/fluent-bundle/src/args.rs
new file mode 100644
index 0000000000..368d70e4ed
--- /dev/null
+++ b/third_party/rust/fluent-bundle/src/args.rs
@@ -0,0 +1,74 @@
+use std::borrow::Cow;
+use std::iter::FromIterator;
+
+use crate::types::FluentValue;
+
+/// A map of arguments passed from the code to
+/// the localization to be used for message
+/// formatting.
+#[derive(Debug, Default)]
+pub struct FluentArgs<'args>(Vec<(Cow<'args, str>, FluentValue<'args>)>);
+
+impl<'args> FluentArgs<'args> {
+ pub fn new() -> Self {
+ Self(vec![])
+ }
+
+ pub fn with_capacity(capacity: usize) -> Self {
+ Self(Vec::with_capacity(capacity))
+ }
+
+ pub fn get(&self, key: &str) -> Option<&FluentValue<'args>> {
+ self.0.iter().find(|(k, _)| key == *k).map(|(_, v)| v)
+ }
+
+ pub fn add<K>(&mut self, key: K, value: FluentValue<'args>)
+ where
+ K: Into<Cow<'args, str>>,
+ {
+ self.0.push((key.into(), value));
+ }
+
+ pub fn iter(&self) -> impl Iterator<Item = (&str, &FluentValue)> {
+ self.0.iter().map(|(k, v)| (k.as_ref(), v))
+ }
+}
+
+impl<'args> FromIterator<(&'args str, FluentValue<'args>)> for FluentArgs<'args> {
+ fn from_iter<I>(iter: I) -> Self
+ where
+ I: IntoIterator<Item = (&'args str, FluentValue<'args>)>,
+ {
+ let mut c = FluentArgs::new();
+
+ for (k, v) in iter {
+ c.add(k, v);
+ }
+
+ c
+ }
+}
+
+impl<'args> FromIterator<(String, FluentValue<'args>)> for FluentArgs<'args> {
+ fn from_iter<I>(iter: I) -> Self
+ where
+ I: IntoIterator<Item = (String, FluentValue<'args>)>,
+ {
+ let mut c = FluentArgs::new();
+
+ for (k, v) in iter {
+ c.add(k, v);
+ }
+
+ c
+ }
+}
+
+impl<'args> IntoIterator for FluentArgs<'args> {
+ type Item = (Cow<'args, str>, FluentValue<'args>);
+ type IntoIter = std::vec::IntoIter<Self::Item>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.0.into_iter()
+ }
+}