diff --git a/vlib/builtin/option.v b/vlib/builtin/option.v index 1c301ac848..ede9b50d7a 100644 --- a/vlib/builtin/option.v +++ b/vlib/builtin/option.v @@ -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__ { diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 56ceea703b..99684a45ee 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -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('}') diff --git a/vlib/v/tests/str_gen_test.v b/vlib/v/tests/str_gen_test.v index 0a50c3db35..dcefa26511 100644 --- a/vlib/v/tests/str_gen_test.v +++ b/vlib/v/tests/str_gen_test.v @@ -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 {