(lhs: Expression, rhs: Expression)
| 55 | } |
| 56 | |
| 57 | pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result<Expression, ParsingError> { |
| 58 | use crate::parser::ast::{Constant, ConstantValue}; |
| 59 | match (lhs, rhs) { |
| 60 | (Expression::Constant(lhs), Expression::Constant(rhs)) => { |
| 61 | let node = Node { |
| 62 | start: lhs.node.start, |
| 63 | end: rhs.node.end, |
| 64 | }; |
| 65 | let concatnated_string = match (lhs.value, rhs.value) { |
| 66 | (ConstantValue::Str(_), ConstantValue::Str(_)) => { |
| 67 | Expression::Constant(Box::new(Constant { |
| 68 | node, |
| 69 | value: ConstantValue::Str(QuoteType::Concat), |
| 70 | })) |
| 71 | } |
| 72 | (ConstantValue::Bytes, ConstantValue::Bytes) => { |
| 73 | Expression::Constant(Box::new(Constant { |
| 74 | node, |
| 75 | value: ConstantValue::Bytes, |
| 76 | })) |
| 77 | } |
| 78 | (ConstantValue::Bytes, _) => { |
| 79 | panic!("Cannot concat bytes and string"); |
| 80 | } |
| 81 | (_, ConstantValue::Bytes) => { |
| 82 | panic!("Can only concat bytes with other bytes"); |
| 83 | } |
| 84 | _ => panic!("Cannot concat string"), |
| 85 | }; |
| 86 | Ok(concatnated_string) |
| 87 | } |
| 88 | (Expression::JoinedStr(fstring_lhs), Expression::JoinedStr(fstring_rhs)) => { |
| 89 | let mut values = fstring_lhs.values; |
| 90 | values.extend(fstring_rhs.values); |
| 91 | Ok(Expression::JoinedStr(Box::new(JoinedStr { |
| 92 | node: Node { |
| 93 | start: fstring_lhs.node.start, |
| 94 | end: fstring_rhs.node.end, |
| 95 | }, |
| 96 | values, |
| 97 | }))) |
| 98 | } |
| 99 | (Expression::JoinedStr(fstring_lhs), Expression::Constant(const_rhs)) => { |
| 100 | let mut values = fstring_lhs.values; |
| 101 | match const_rhs.value { |
| 102 | ConstantValue::Str(s) => { |
| 103 | values.push(Expression::Constant(Box::new(Constant { |
| 104 | node: Node { |
| 105 | start: const_rhs.node.start, |
| 106 | end: const_rhs.node.end, |
| 107 | }, |
| 108 | value: ConstantValue::Str(s), |
| 109 | }))); |
| 110 | } |
| 111 | ConstantValue::Bytes => { |
| 112 | panic!("Cannot concat string and bytes"); |
| 113 | } |
| 114 | _ => panic!("Cannot concat string"), |
no test coverage detected