From 7331b674ca572b3d33e9f95e40949831f50b7a9c Mon Sep 17 00:00:00 2001 From: Anton Burticica Date: Thu, 20 Jun 2024 16:48:30 +0300 Subject: [PATCH] Allow expressions as file_desc and message_type args --- src/stdlib/parse_proto.rs | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/stdlib/parse_proto.rs b/src/stdlib/parse_proto.rs index 0c55f29307..d1a06b7316 100644 --- a/src/stdlib/parse_proto.rs +++ b/src/stdlib/parse_proto.rs @@ -76,39 +76,38 @@ impl Function for ParseProto { fn compile( &self, - state: &state::TypeState, + _state: &state::TypeState, _ctx: &mut FunctionCompileContext, arguments: ArgumentList, ) -> Compiled { let value = arguments.required("value"); - let desc_file = arguments.required_literal("desc_file", state)?; - let desc_file_str = desc_file - .try_bytes_utf8_lossy() - .expect("descriptor file must be a string"); - let message_type = arguments.required_literal("message_type", state)?; - let message_type_str = message_type - .try_bytes_utf8_lossy() - .expect("message_type must be a string"); - let os_string: OsString = desc_file_str.into_owned().into(); - let path_buf = PathBuf::from(os_string); - let path = Path::new(&path_buf); - let descriptor = - get_message_descriptor(path, &message_type_str).expect("message type not found"); + let desc_file = arguments.required("desc_file"); + let message_type = arguments.required("message_type"); - Ok(ParseProtoFn { descriptor, value }.as_expr()) + Ok(ParseProtoFn { desc_file, message_type, value }.as_expr()) } } #[derive(Debug, Clone)] struct ParseProtoFn { - descriptor: MessageDescriptor, + desc_file: Box, + message_type: Box, value: Box, } impl FunctionExpression for ParseProtoFn { fn resolve(&self, ctx: &mut Context) -> Resolved { let value = self.value.resolve(ctx)?; - parse_proto(&self.descriptor, value) + let desc_file = self.desc_file.resolve(ctx)?; + let desc_file_str = desc_file.try_bytes_utf8_lossy()?; + let message_type = self.message_type.resolve(ctx)?; + let message_type_str = message_type.try_bytes_utf8_lossy()?; + let os_string: OsString = desc_file_str.into_owned().into(); + let path_buf = PathBuf::from(os_string); + let path = Path::new(&path_buf); + let descriptor: MessageDescriptor = + get_message_descriptor(path, &message_type_str).expect("message type not found"); + parse_proto(&descriptor, value) } fn type_def(&self, _: &state::TypeState) -> TypeDef {