diff mbox series

[3/3] suricatta/lua: Return STATE_ERROR instead of nil on !pstate.get()

Message ID 20230517153550.262808-3-christian.storm@siemens.com
State Accepted
Delegated to: Stefano Babic
Headers show
Series [1/3] suricatta/lua: Add IPC types / definitions to spec | expand

Commit Message

Storm, Christian May 17, 2023, 3:35 p.m. UTC
As nil is not of type number and STATE_ERROR is
semantically equivalent, return STATE_ERROR upon
error in suricatta.pstate.get().
This change also makes it unnecessary to return
a success indicator boolean as first return value.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 doc/source/suricatta.rst                  |  4 ++--
 examples/suricatta/swupdate_suricatta.lua |  3 +--
 suricatta/server_lua.c                    | 13 +++----------
 suricatta/suricatta.lua                   |  3 +--
 4 files changed, 7 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/doc/source/suricatta.rst b/doc/source/suricatta.rst
index 01102c3f..9782e591 100644
--- a/doc/source/suricatta.rst
+++ b/doc/source/suricatta.rst
@@ -397,8 +397,8 @@  defaulting to ``ustate``. In addition, it captures the ``update_state_t`` enum v
 
 The function ``suricatta.pstate.save(state)`` requires one of ``suricatta.pstate``'s
 "enum" values as parameter and returns ``true``, or, in case of error, ``nil``.
-The function ``suricatta.pstate.get()`` returns ``true``, or, in case of error, ``nil``,
-plus one of ``suricatta.pstate``'s "enum" values in the former case.
+The function ``suricatta.pstate.get()`` returns one of ``suricatta.pstate``'s
+"enum" values or, in case of error, ``STATE_ERROR``.
 
 
 `suricatta.server`
diff --git a/examples/suricatta/swupdate_suricatta.lua b/examples/suricatta/swupdate_suricatta.lua
index 3f01b0ce..dffc2909 100644
--- a/examples/suricatta/swupdate_suricatta.lua
+++ b/examples/suricatta/swupdate_suricatta.lua
@@ -171,8 +171,7 @@  gs = {
 function has_pending_action(action_id)
     action_id = action_id
     gs.polldelay.current = gs.polldelay.default
-    local _, pstate = suricatta.pstate.get()
-    if pstate == suricatta.pstate.INSTALLED then
+    if suricatta.pstate.get() == suricatta.pstate.INSTALLED then
         suricatta.notify.warn("An installed update is pending testing, not querying server.")
         return action_id, suricatta.status.NO_UPDATE_AVAILABLE
     end
diff --git a/suricatta/server_lua.c b/suricatta/server_lua.c
index 37e4950b..14167fe6 100644
--- a/suricatta/server_lua.c
+++ b/suricatta/server_lua.c
@@ -1458,20 +1458,13 @@  static int lua_bootloader_env_apply(lua_State *L)
 /**
  * @brief Get update state from persistent storage (bootloader).
  *
- * @return [Lua] True, or, in case of error, nil.
- *         [Lua] One of pstate's enum values.
+ * @return [Lua] One of pstate's enum values.
  */
 static int lua_pstate_get(lua_State *L)
 {
 	update_state_t state = get_state();
-	if (is_valid_state(state)) {
-		lua_pushboolean(L, true);
-		lua_pushinteger(L, (int)state);
-	} else {
-		lua_pushnil(L);
-		lua_pushnil(L);
-	}
-	return 2;
+	lua_pushinteger(L, is_valid_state(state) ? (int)state : STATE_ERROR);
+	return 1;
 }
 
 
diff --git a/suricatta/suricatta.lua b/suricatta/suricatta.lua
index c61d483f..9dc1bcfe 100644
--- a/suricatta/suricatta.lua
+++ b/suricatta/suricatta.lua
@@ -130,8 +130,7 @@  suricatta.pstate = {
 
 --- Get the current stored persistent state.
 --
---- @return boolean           # Whether operation was successful or not
---- @return suricatta.pstate  # Persistent state ID number
+--- @return number   # Persistent state ID number, suricatta.pstate.ERROR if unsuccessful
 suricatta.pstate.get = function() end
 
 --- Save persistent state information.