| 64 | type Args = InterpolationArgs; |
| 65 | |
| 66 | fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> { |
| 67 | let conversion: PyObjectRef = if let Some(s) = args.conversion { |
| 68 | let has_flag = s |
| 69 | .as_bytes() |
| 70 | .iter() |
| 71 | .exactly_one() |
| 72 | .ok() |
| 73 | .is_some_and(|s| matches!(*s, b's' | b'r' | b'a')); |
| 74 | if !has_flag { |
| 75 | return Err(vm.new_value_error( |
| 76 | "Interpolation() argument 'conversion' must be one of 's', 'a' or 'r'", |
| 77 | )); |
| 78 | } |
| 79 | s.into() |
| 80 | } else { |
| 81 | vm.ctx.none() |
| 82 | }; |
| 83 | |
| 84 | let expression = args |
| 85 | .expression |
| 86 | .unwrap_or_else(|| vm.ctx.empty_str.to_owned()); |
| 87 | let format_spec = args |
| 88 | .format_spec |
| 89 | .unwrap_or_else(|| vm.ctx.empty_str.to_owned()); |
| 90 | |
| 91 | Ok(PyInterpolation { |
| 92 | value: args.value, |
| 93 | expression, |
| 94 | conversion, |
| 95 | format_spec, |
| 96 | }) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #[derive(FromArgs)] |