Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow expressions as file_desc and message_type args #900

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/stdlib/parse_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Expression>,
message_type: Box<dyn Expression>,
value: Box<dyn Expression>,
}

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 {
Expand Down