(
name: String,
args: Vec<rq::Expr>,
ctx: &mut Context,
)
| 41 | } |
| 42 | |
| 43 | pub(super) fn translate_operator( |
| 44 | name: String, |
| 45 | args: Vec<rq::Expr>, |
| 46 | ctx: &mut Context, |
| 47 | ) -> Result<SourceExpr> { |
| 48 | let (func_def, binding_strength, window_frame, coalesce) = |
| 49 | find_operator_impl(&name, ctx.dialect_enum).unwrap(); |
| 50 | let parent_binding_strength = binding_strength.unwrap_or(100); |
| 51 | |
| 52 | let params = func_def |
| 53 | .named_params |
| 54 | .iter() |
| 55 | .chain(func_def.params.iter()) |
| 56 | .map(|x| x.name.split('.').next_back().unwrap_or(x.name.as_str())); |
| 57 | |
| 58 | let args: HashMap<&str, _> = zip(params, args).collect(); |
| 59 | |
| 60 | // body can only be an s-string |
| 61 | let body = match &func_def.body.kind { |
| 62 | pl::ExprKind::Literal(pl::Literal::Null) => { |
| 63 | return Err(Error::new_simple(format!( |
| 64 | "operator {} is not supported for dialect {}", |
| 65 | name, ctx.dialect_enum |
| 66 | ))) |
| 67 | } |
| 68 | pl::ExprKind::SString(items) => items, |
| 69 | _ => panic!("Bad RQ operator implementation. Expected s-string or null"), |
| 70 | }; |
| 71 | |
| 72 | let mut text = String::new(); |
| 73 | |
| 74 | for item in body { |
| 75 | match item { |
| 76 | pl::InterpolateItem::Expr { expr, format } => { |
| 77 | // s-string exprs can only contain idents |
| 78 | let ident = expr.kind.as_ident(); |
| 79 | let ident = ident.as_ref().unwrap(); |
| 80 | |
| 81 | // lookup args |
| 82 | let arg = args.get(ident.name.as_str()).unwrap().clone(); |
| 83 | |
| 84 | // binding strength |
| 85 | let required_strength = format |
| 86 | .as_ref() |
| 87 | .and_then(|f| f.parse::<i32>().ok()) |
| 88 | .unwrap_or(parent_binding_strength); |
| 89 | |
| 90 | // translate args |
| 91 | let arg = translate_operand( |
| 92 | arg, |
| 93 | false, |
| 94 | required_strength, |
| 95 | super::gen_expr::Associativity::Both, |
| 96 | ctx, |
| 97 | )?; |
| 98 | |
| 99 | text += &arg.into_source(); |
| 100 | } |
no test coverage detected