]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Remove dependency on core from util
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 3 Aug 2023 11:37:25 +0000 (13:37 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Thu, 3 Aug 2023 11:54:38 +0000 (13:54 +0200)
src/Config.cpp
src/util/DirEntry.cpp
src/util/LockFile.cpp
src/util/environment.cpp
src/util/environment.hpp
unittest/test_util_environment.cpp

index 16dfbd5f3c8944934ded5e1c5596b4f32a1c831e..021be48888e74afdbd946b08bf4c311f31bd01be 100644 (file)
@@ -978,7 +978,8 @@ Config::set_item(const std::string& key,
     return;
   }
 
-  std::string value = util::expand_environment_variables(unexpanded_value);
+  std::string value = util::value_or_throw<core::Error>(
+    util::expand_environment_variables(unexpanded_value));
 
   switch (it->second.item) {
   case ConfigItem::absolute_paths_in_stderr:
index 80ed955ef345c16f77d82045908c2d8a17a27538..c6335dd150e7827551175b780653725961e36a5d 100644 (file)
@@ -21,7 +21,6 @@
 #include <Finalizer.hpp>
 #include <Logging.hpp>
 #include <Win32Util.hpp>
-#include <core/exceptions.hpp>
 #include <fmtmacros.hpp>
 #include <util/wincompat.hpp>
 
index f08340cdcfcc4cddb9f618aca73f2a01ce0a5ec6..139b17dad554940a59b5cbbe719532cf974d6e6d 100644 (file)
@@ -24,7 +24,6 @@
 #include "fmtmacros.hpp"
 
 #include <assertions.hpp>
-#include <core/exceptions.hpp>
 #include <util/DirEntry.hpp>
 #include <util/file.hpp>
 #include <util/filesystem.hpp>
index 336ed252ea445b6482533bca6361d8d343c5bc04..57e0470ae3ba5de29d659b7134260bc3d81741e5 100644 (file)
 
 #include "environment.hpp"
 
-#include <core/exceptions.hpp>
 #include <fmtmacros.hpp>
 #include <util/wincompat.hpp>
 
 namespace util {
 
-std::string
+tl::expected<std::string, std::string>
 expand_environment_variables(const std::string& str)
 {
   std::string result;
@@ -52,7 +51,8 @@ expand_environment_variables(const std::string& str)
         ++right;
       }
       if (curly && *right != '}') {
-        throw core::Error(FMT("syntax error: missing '}}' after \"{}\"", left));
+        return tl::unexpected(
+          FMT("syntax error: missing '}}' after \"{}\"", left));
       }
       if (right == left) {
         // Special case: don't consider a single $ the left of a variable.
@@ -62,7 +62,8 @@ expand_environment_variables(const std::string& str)
         std::string name(left, right - left);
         const char* value = getenv(name.c_str());
         if (!value) {
-          throw core::Error(FMT("environment variable \"{}\" not set", name));
+          return tl::unexpected(
+            FMT("environment variable \"{}\" not set", name));
         }
         result += value;
         if (!curly) {
index 29a99de599a55156811458ef897f1106d2bd11b9..c874b22333507c4faee50c58791eb347f9a2ebcd 100644 (file)
 
 #pragma once
 
+#include <third_party/tl/expected.hpp>
+
 #include <string>
 
 namespace util {
 
 // Expand all instances of $VAR or ${VAR}, where VAR is an environment variable,
-// in `str`. Throws `core::Error` if one of the environment variables.
-[[nodiscard]] std::string expand_environment_variables(const std::string& str);
+// in `str`.
+tl::expected<std::string, std::string>
+expand_environment_variables(const std::string& str);
 
 // Set environment variable `name` to `value`.
 void setenv(const std::string& name, const std::string& value);
index 06654672aaaf5173d9392aa784a62e53148aa552..1819ab86b3d3fa754a6be1bcab85259ee99880bb 100644 (file)
@@ -35,14 +35,10 @@ TEST_CASE("util::expand_environment_variables")
   CHECK(util::expand_environment_variables("x$FOO") == "xbar");
   CHECK(util::expand_environment_variables("${FOO}x") == "barx");
 
-  DOCTEST_GCC_SUPPRESS_WARNING_PUSH
-  DOCTEST_GCC_SUPPRESS_WARNING("-Wunused-result")
-  CHECK_THROWS_WITH(
-    (void)util::expand_environment_variables("$surelydoesntexist"),
-    "environment variable \"surelydoesntexist\" not set");
-  CHECK_THROWS_WITH((void)util::expand_environment_variables("${FOO"),
-                    "syntax error: missing '}' after \"FOO\"");
-  DOCTEST_GCC_SUPPRESS_WARNING_POP
+  CHECK(util::expand_environment_variables("$surelydoesntexist").error()
+        == "environment variable \"surelydoesntexist\" not set");
+  CHECK(util::expand_environment_variables("${FOO").error()
+        == "syntax error: missing '}' after \"FOO\"");
 }
 
 TEST_SUITE_END();