LCOV - code coverage report
Current view: top level - src/cli - json_formatter.cpp (source / functions) Coverage Total Hit
Test: HPActor Coverage Lines: 62.3 % 53 33
Test Date: 2026-05-20 02:24:49 Functions: 55.6 % 9 5
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : #include <hpactor/cli/json_formatter.hpp>
       2                 :             : 
       3                 :             : namespace hpactor {
       4                 :             : namespace cli {
       5                 :             : 
       6                 :           9 : std::string JsonFormatter::escape(const std::string& s) {
       7                 :           9 :     std::string out;
       8                 :           9 :     out.reserve(s.size() + 2);
       9                 :           9 :     out += '"';
      10                 :          66 :     for (char c : s) {
      11                 :          57 :         switch (c) {
      12                 :           0 :         case '"':  out += "\\\""; break;
      13                 :           0 :         case '\\': out += "\\\\"; break;
      14                 :           0 :         case '\n': out += "\\n";  break;
      15                 :           0 :         case '\t': out += "\\t";  break;
      16                 :          57 :         default:   out += c;
      17                 :             :         }
      18                 :             :     }
      19                 :           9 :     out += '"';
      20                 :           9 :     return out;
      21                 :             : }
      22                 :             : 
      23                 :           0 : void JsonFormatter::header(const std::string& /*title*/) {}  // no-op
      24                 :             : 
      25                 :           1 : void JsonFormatter::table(const std::vector<std::string>& columns,
      26                 :             :                           const std::vector<std::vector<std::string>>& rows) {
      27                 :           1 :     buf_ += "[";
      28                 :           2 :     for (size_t r = 0; r < rows.size(); ++r) {
      29                 :           1 :         if (r > 0) buf_ += ",";
      30                 :           1 :         buf_ += "{";
      31                 :           3 :         for (size_t c = 0; c < columns.size() && c < rows[r].size(); ++c) {
      32                 :           2 :             if (c > 0) buf_ += ",";
      33                 :           2 :             buf_ += escape(columns[c]) + ":" + escape(rows[r][c]);
      34                 :             :         }
      35                 :           1 :         buf_ += "}";
      36                 :             :     }
      37                 :           1 :     buf_ += "]";
      38                 :           1 : }
      39                 :             : 
      40                 :           1 : void JsonFormatter::key_value(const std::map<std::string, std::string>& pairs) {
      41                 :           1 :     buf_ += "{";
      42                 :           1 :     bool first = true;
      43                 :           3 :     for (const auto& kv : pairs) {
      44                 :           2 :         if (!first) buf_ += ",";
      45                 :           2 :         first = false;
      46                 :           2 :         buf_ += escape(kv.first) + ":" + escape(kv.second);
      47                 :             :     }
      48                 :           1 :     buf_ += "}";
      49                 :           1 : }
      50                 :             : 
      51                 :           0 : void JsonFormatter::tree(const TreeNode& root) {
      52                 :           0 :     buf_ += "{" + escape("name") + ":" + escape(root.name) + ","
      53                 :           0 :          + escape("description") + ":" + escape(root.description) + ","
      54                 :           0 :          + escape("children") + ":";
      55                 :           0 :     json_tree(root);
      56                 :           0 :     buf_ += "}";
      57                 :           0 : }
      58                 :             : 
      59                 :           0 : void JsonFormatter::json_tree(const TreeNode& node) {
      60                 :           0 :     buf_ += "[";
      61                 :           0 :     for (size_t i = 0; i < node.children.size(); ++i) {
      62                 :           0 :         if (i > 0) buf_ += ",";
      63                 :           0 :         tree(node.children[i]);
      64                 :             :     }
      65                 :           0 :     buf_ += "]";
      66                 :           0 : }
      67                 :             : 
      68                 :           0 : void JsonFormatter::raw(const std::string& text) { buf_ += escape(text); }
      69                 :             : 
      70                 :           1 : void JsonFormatter::error(const std::string& message) {
      71                 :           1 :     buf_ += "{\"error\":" + escape(message) + "}";
      72                 :           1 : }
      73                 :             : 
      74                 :           3 : std::string JsonFormatter::finalize() { return std::move(buf_); }
      75                 :             : 
      76                 :             : }  // namespace cli
      77                 :             : }  // namespace hpactor
        

Generated by: LCOV version 2.0-1