builtin: add a proper str() method to IError (#9379)

pull/9369/head
spaceface 2021-03-20 00:55:16 +01:00 committed by GitHub
parent 59f95170b3
commit 3f9e921c95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 8 deletions

View File

@ -16,6 +16,14 @@ pub:
code int
}
pub fn (err IError) str() string {
return match err {
None__ { 'none' }
Error { err.msg }
else { '$err.type_name(): $err.msg' }
}
}
const none__ = IError(&None__{})
struct None__ {

View File

@ -151,7 +151,7 @@ fn (mut g Gen) gen_str_for_option(typ table.Type, styp string, str_fn_name strin
g.auto_str_funcs.writeln('\t\tres = ${parent_str_fn_name}(*($sym.cname*)it.data);')
}
g.auto_str_funcs.writeln('\t} else {')
g.auto_str_funcs.writeln('\t\tres = _STR("error: %.*s\\000", 2, indent_IError_str(it.err, indent_count));')
g.auto_str_funcs.writeln('\t\tres = _STR("error: %.*s\\000", 2, IError_str(it.err));')
g.auto_str_funcs.writeln('\t}')
g.auto_str_funcs.writeln('\treturn _STR("Option(%.*s\\000)", 2, res);')
g.auto_str_funcs.writeln('}')

View File

@ -296,12 +296,7 @@ fn create_option_err() ?string {
}
fn test_option_err() {
assert '$create_option_err()' == "
Option(error: IError(Error{
msg: 'this is an error'
code: 0
}))
".trim_space()
assert '$create_option_err()' == "Option(error: this is an error)"
}
fn create_option_none() ?string {
@ -309,7 +304,7 @@ fn create_option_none() ?string {
}
fn test_option_none() {
assert '$create_option_none()' == 'Option(error: IError(none))'
assert '$create_option_none()' == 'Option(error: none)'
}
fn create_option_string() ?string {