add types to some more Fluent variables

This commit is contained in:
Damien Elmes 2021-03-26 16:52:54 +10:00
parent 8cc6758eb1
commit 3d366d5264
3 changed files with 22 additions and 12 deletions

View file

@ -13,7 +13,7 @@ modules = json.load(open(strings_json))
class Variable(TypedDict): class Variable(TypedDict):
name: str name: str
kind: Literal["Any", "Int"] kind: Literal["Any", "Int", "String", "Float"]
def legacy_enum() -> str: def legacy_enum() -> str:
@ -60,8 +60,12 @@ def get_arg_types(args: List[Variable]) -> str:
def arg_kind(arg: Variable) -> str: def arg_kind(arg: Variable) -> str:
if arg["kind"] == "Int": if arg["kind"] == "Int":
return "int" return "int"
else: elif arg["kind"] == "Any":
return "FluentVariable" return "FluentVariable"
elif arg["kind"] == "Float":
return "float"
else:
return "str"
def get_args(args: List[Variable]) -> str: def get_args(args: List[Variable]) -> str:

View file

@ -1090,7 +1090,7 @@ class DownloaderInstaller(QObject):
label=tr.addons_downloading_adbd_kb02fkb( label=tr.addons_downloading_adbd_kb02fkb(
part=len(self.log) + 1, part=len(self.log) + 1,
total=len(self.ids), total=len(self.ids),
kilobytes=self.dl_bytes / 1024, kilobytes=self.dl_bytes // 1024,
) )
) )

View file

@ -30,9 +30,8 @@ pub struct Variable {
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Serialize)] #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Serialize)]
enum VariableKind { enum VariableKind {
Int, Int,
// Float, Float,
// Number, String,
// String,
Any, Any,
} }
@ -110,10 +109,12 @@ impl Visitor {
} }
} }
fn visit_inline_expression(&mut self, expr: &InlineExpression<&str>) { fn visit_inline_expression(&mut self, expr: &InlineExpression<&str>, in_select: bool) {
match expr { match expr {
InlineExpression::VariableReference { id } => { InlineExpression::VariableReference { id } => {
if !in_select {
write!(self.text, "{{${}}}", id.name).unwrap(); write!(self.text, "{{${}}}", id.name).unwrap();
}
self.variables.insert(id.name.to_string()); self.variables.insert(id.name.to_string());
} }
InlineExpression::Placeable { expression } => { InlineExpression::Placeable { expression } => {
@ -126,19 +127,24 @@ impl Visitor {
fn visit_expression(&mut self, expression: &Expression<&str>) { fn visit_expression(&mut self, expression: &Expression<&str>) {
match expression { match expression {
Expression::SelectExpression { selector, variants } => { Expression::SelectExpression { selector, variants } => {
self.visit_inline_expression(&selector); self.visit_inline_expression(&selector, true);
self.visit_pattern(&variants.last().unwrap().value) self.visit_pattern(&variants.last().unwrap().value)
} }
Expression::InlineExpression(expr) => self.visit_inline_expression(expr), Expression::InlineExpression(expr) => self.visit_inline_expression(expr, false),
} }
} }
} }
impl From<String> for Variable { impl From<String> for Variable {
fn from(name: String) -> Self { fn from(name: String) -> Self {
// rather than adding more items here as we add new strings, we should probably
// try to either reuse existing ones, or consider some sort of Hungarian notation
let kind = match name.as_str() { let kind = match name.as_str() {
"cards" | "notes" | "count" | "amount" => VariableKind::Int, "cards" | "notes" | "count" | "amount" | "reviews" | "total" | "selected"
_ => VariableKind::Any, | "kilobytes" => VariableKind::Int,
"average-seconds" => VariableKind::Float,
"val" | "found" | "expected" | "part" => VariableKind::Any,
_ => VariableKind::String,
}; };
Variable { name, kind } Variable { name, kind }
} }