diff options
| author | Richard W.M. Jones <rjones@redhat.com> | 2011-03-07 19:28:30 +0000 |
|---|---|---|
| committer | Richard W.M. Jones <rjones@redhat.com> | 2011-03-07 19:28:30 +0000 |
| commit | 8037da06feea097716ce700f38c0eac0d5411a7c (patch) | |
| tree | 482fa34741bcdf650948eebb99903943137f4e19 /generator/generator_ruby.ml | |
| parent | 7c721e4fd674c409b3eee60fe237d480afa1c5e2 (diff) | |
generator: Introduce error code (errcode) concept.
There was a lot of repeated code to map return types (eg. RErr)
to error cases (eg. -1 or NULL).
This commit introduces an error code type and two functions to
map return types to error codes and error codes to strings.
Diffstat (limited to 'generator/generator_ruby.ml')
| -rw-r--r-- | generator/generator_ruby.ml | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/generator/generator_ruby.ml b/generator/generator_ruby.ml index 6d627b05..49ddf1ff 100644 --- a/generator/generator_ruby.ml +++ b/generator/generator_ruby.ml @@ -184,21 +184,20 @@ static VALUE ruby_guestfs_close (VALUE gv) pr "\n"; ); - let error_code = - match ret with - | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1" - | RInt64 _ -> pr " int64_t r;\n"; "-1" - | RConstString _ | RConstOptString _ -> - pr " const char *r;\n"; "NULL" - | RString _ -> pr " char *r;\n"; "NULL" - | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL" - | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ; "NULL" - | RStructList (_, typ) -> - pr " struct guestfs_%s_list *r;\n" typ; "NULL" - | RBufferOut _ -> - pr " char *r;\n"; - pr " size_t size;\n"; - "NULL" in + (match ret with + | RErr | RInt _ | RBool _ -> pr " int r;\n" + | RInt64 _ -> pr " int64_t r;\n" + | RConstString _ | RConstOptString _ -> + pr " const char *r;\n" + | RString _ -> pr " char *r;\n" + | RStringList _ | RHashtable _ -> pr " char **r;\n" + | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ + | RStructList (_, typ) -> + pr " struct guestfs_%s_list *r;\n" typ + | RBufferOut _ -> + pr " char *r;\n"; + pr " size_t size;\n" + ); pr "\n"; if optargs = [] then @@ -217,8 +216,15 @@ static VALUE ruby_guestfs_close (VALUE gv) pr " free (%s);\n" n ) args; - pr " if (r == %s)\n" error_code; - pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n"; + (match errcode_of_ret ret with + | `CannotReturnError -> () + | `ErrorIsMinusOne -> + pr " if (r == -1)\n"; + pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n" + | `ErrorIsNULL -> + pr " if (r == NULL)\n"; + pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n" + ); pr "\n"; (match ret with |
