1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
use super::scope::Scope; use super::{ResolveValue, ResolverError, WriteValue}; use std::borrow::Borrow; use std::fmt; use fluent_syntax::ast; use fluent_syntax::unicode::{unescape_unicode, unescape_unicode_to_string}; use crate::entry::GetEntry; use crate::memoizer::MemoizerKind; use crate::resource::FluentResource; use crate::types::FluentValue; impl<'p> WriteValue for ast::InlineExpression<&'p str> { fn write<'scope, 'errors, W, R, M: MemoizerKind>( &'scope self, w: &mut W, scope: &mut Scope<'scope, 'errors, R, M>, ) -> fmt::Result where W: fmt::Write, R: Borrow<FluentResource>, { match self { Self::StringLiteral { value } => unescape_unicode(w, value), Self::MessageReference { id, attribute } => { if let Some(msg) = scope.bundle.get_entry_message(id.name) { if let Some(attr) = attribute { msg.attributes .iter() .find_map(|a| { if a.id.name == attr.name { Some(scope.track(w, &a.value, self)) } else { None } }) .unwrap_or_else(|| scope.write_ref_error(w, self)) } else { msg.value .as_ref() .map(|value| scope.track(w, value, self)) .unwrap_or_else(|| { scope.add_error(ResolverError::NoValue(id.name.to_string())); w.write_char('{')?; self.write_error(w)?; w.write_char('}') }) } } else { scope.write_ref_error(w, self) } } Self::NumberLiteral { value } => FluentValue::try_number(*value).write(w, scope), Self::TermReference { id, attribute, arguments, } => { let (_, resolved_named_args) = scope.get_arguments(arguments); scope.local_args = Some(resolved_named_args); let result = scope .bundle .get_entry_term(id.name) .and_then(|term| { if let Some(attr) = attribute { term.attributes.iter().find_map(|a| { if a.id.name == attr.name { Some(scope.track(w, &a.value, self)) } else { None } }) } else { Some(scope.track(w, &term.value, self)) } }) .unwrap_or_else(|| scope.write_ref_error(w, self)); scope.local_args = None; result } Self::FunctionReference { id, arguments } => { let (resolved_positional_args, resolved_named_args) = scope.get_arguments(arguments); let func = scope.bundle.get_entry_function(id.name); if let Some(func) = func { let result = func(resolved_positional_args.as_slice(), &resolved_named_args); if let FluentValue::Error = result { self.write_error(w) } else { w.write_str(&result.as_string(scope)) } } else { scope.write_ref_error(w, self) } } Self::VariableReference { id } => { let args = scope.local_args.as_ref().or(scope.args); if let Some(arg) = args.and_then(|args| args.get(id.name)) { arg.write(w, scope) } else { if scope.local_args.is_none() { scope.add_error(self.into()); } w.write_char('{')?; self.write_error(w)?; w.write_char('}') } } Self::Placeable { expression } => expression.write(w, scope), } } fn write_error<W>(&self, w: &mut W) -> fmt::Result where W: fmt::Write, { match self { Self::MessageReference { id, attribute: Some(attribute), } => write!(w, "{}.{}", id.name, attribute.name), Self::MessageReference { id, attribute: None, } => w.write_str(id.name), Self::TermReference { id, attribute: Some(attribute), .. } => write!(w, "-{}.{}", id.name, attribute.name), Self::TermReference { id, attribute: None, .. } => write!(w, "-{}", id.name), Self::FunctionReference { id, .. } => write!(w, "{}()", id.name), Self::VariableReference { id } => write!(w, "${}", id.name), _ => unreachable!(), } } } impl<'p> ResolveValue for ast::InlineExpression<&'p str> { fn resolve<'source, 'errors, R, M: MemoizerKind>( &'source self, scope: &mut Scope<'source, 'errors, R, M>, ) -> FluentValue<'source> where R: Borrow<FluentResource>, { match self { Self::StringLiteral { value } => unescape_unicode_to_string(value).into(), Self::NumberLiteral { value } => FluentValue::try_number(*value), Self::VariableReference { id } => { let args = scope.local_args.as_ref().or(scope.args); if let Some(arg) = args.and_then(|args| args.get(id.name)) { arg.clone() } else { if scope.local_args.is_none() { scope.add_error(self.into()); } FluentValue::Error } } _ => { let mut result = String::new(); self.write(&mut result, scope).expect("Failed to write"); result.into() } } } }