-
Notifications
You must be signed in to change notification settings - Fork 197
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
c#: Add cli option to generate Result types #1115
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: James Sturtevant <[email protected]>
9e0e77c
to
d77a265
Compare
|
||
/// Generates code for wit Result types instead of exceptions | ||
#[cfg_attr(feature = "clap", arg(long))] | ||
pub with_wit_results: bool, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
open to name suggestions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM; thanks! Just a few whitespace and grammar nits.
@@ -30,6 +30,10 @@ pub struct Opts { | |||
/// Skip generating `cabi_realloc`, `WasmImportLinkageAttribute`, and component type files | |||
#[cfg_attr(feature = "clap", arg(long))] | |||
pub skip_support_files: bool, | |||
|
|||
/// Generates code for wit Result types instead of exceptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Generates code for wit Result types instead of exceptions | |
/// Generate code for WIT `Result` types instead of exceptions |
var result = imports.test.results.TestInterop.EnumError(a); | ||
if (result.IsOk) { | ||
return Result<float, ITest.E>.Ok(result.AsOk); | ||
}else{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
}else{ | |
} else { |
uwrite!( | ||
self.src, | ||
"\ | ||
if ({previous}.IsOk) {{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Would you mind adjusting the indentation for this and the other uwrite
expressions that follow? E.g. put the if
under the "
on the previous line or similar and indent the var
on the next line four columns past that, etc. It's a bit jarring to see it indented way to the right.
}} else {{ | ||
throw new {exception_name}({var_name}.AsErr!, {level}); | ||
}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
}} else {{ | |
throw new {exception_name}({var_name}.AsErr!, {level}); | |
}} | |
}} | |
else | |
{{ | |
throw new {exception_name}({var_name}.AsErr!, {level}); | |
}} |
uwrite!( | ||
self.src, | ||
"\ | ||
if ({previous}.IsOk) {{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the C# I think we are trying to go more the style of {
on the next line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ({previous}.IsOk) {{ | |
if ({previous}.IsOk) | |
{{ |
let tail = oks.iter().map(|_| ")").collect::<Vec<_>>().concat(); | ||
cases.push(format!( | ||
"\ | ||
case {index}: {{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case {index}: {{ | |
case {index}: | |
{{ |
payload_is_void = result.ok.is_none(); | ||
} | ||
if !self.results.is_empty() { | ||
self.src.push_str("try {\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.src.push_str("try {\n"); | |
self.src.push_str(\" | |
try | |
{\n"); |
Not sure I've got the rust syntax right here.
let cases = cases.join("\n"); | ||
uwriteln!( | ||
self.src, | ||
r#"}} catch (WitException e) {{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r#"}} catch (WitException e) {{ | |
r#"}} | |
catch (WitException e) | |
{{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks,
uwriteln!( | ||
self.src, | ||
r#"}} catch (WitException e) {{ | ||
switch (e.NestingLevel) {{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switch (e.NestingLevel) {{ | |
switch (e.NestingLevel) | |
{{ |
Adds option to generate Result types instead of handling Results via Exceptions: #1042