Commit bbb8b767 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add expr_to_enum

parent d2e15b85
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -12,9 +12,7 @@
 * limitations under the License.
 */
#pragma once
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <unordered_map>
#include "geometry.h"

namespace fviz {
+11 −0
Original line number Diff line number Diff line
@@ -59,6 +59,17 @@ ReturnCode expr_to_color_opt(
    const Expr* expr,
    std::optional<Color>* var);

template <typename T>
ReturnCode expr_to_enum(
    const Expr* expr,
    const std::unordered_map<std::string, T> value_defs,
    T* value);

template <typename T>
ExprConv expr_to_enum_fn(
    T* value,
    const std::unordered_map<std::string, T> value_defs);

template <typename T>
ReturnCode expr_tov(
    const Expr* expr,
+35 −0
Original line number Diff line number Diff line
@@ -15,6 +15,41 @@

namespace fviz {

template <typename T>
ReturnCode expr_to_enum(
    const Expr* expr,
    const std::unordered_map<std::string, T> value_defs,
    T* value) {
  if (!expr || !expr_is_value(expr)) {
    return ReturnCode::errorf(
        "EARG",
        "argument error; expected a list, got: $0",
        "..."); // FIXME
  }

  const auto& value_def = value_defs.find(expr_get_value(expr));
  if (value_def == value_defs.end()) {
    return ReturnCode::errorf(
        "EPARSE",
        "invalid value '$0'",
        expr_get_value(expr));
  }

  *value = value_def->second;
  return ReturnCode::success();
}

template <typename T>
ExprConv expr_to_enum_fn(
    T* value,
    const std::unordered_map<std::string, T> value_defs) {
  return bind(
      &expr_to_enum<T>,
      std::placeholders::_1,
      value_defs,
      value);
}

template <typename T>
ReturnCode expr_tov(
    const Expr* expr,
+7 −1
Original line number Diff line number Diff line
@@ -231,7 +231,13 @@ ReturnCode build(
    {"ymax", bind(&expr_to_float64_opt, _1, &config->scale_y.max)},
    {"yscale", bind(&scale_configure_kind, _1, &config->scale_y)},
    {"yscale-padding", bind(&expr_to_float64, _1, &config->scale_y.padding)},
    //{"direction", bind(&expr_to_direction, _1, &config->direction)},
    {
      "direction",
      expr_to_enum_fn<Direction>(&config->direction, {
        { "horizontal", Direction::HORIZONTAL },
        { "vertical", Direction::VERTICAL },
      })
    },
    {"color", expr_tov_fn<Color>(bind(&expr_to_color, _1, _2), &config->colors)},
    {"colors", expr_tov_fn<Color>(bind(&expr_to_color, _1, _2), &config->colors)},
  });
+11 −5
Original line number Diff line number Diff line
@@ -29,6 +29,10 @@ using namespace std::placeholders;

namespace fviz::elements::chart::bars {

static const double kDefaultBarSizePT = 10;
static const double kDefaultLabelPaddingHorizEM = 0.6;
static const double kDefaultLabelPaddingVertEM = 0.6;

struct PlotBarsConfig {
  PlotBarsConfig();
  Direction direction;
@@ -48,10 +52,6 @@ struct PlotBarsConfig {
  Color label_color;
};

static const double kDefaultBarSizePT = 10;
static const double kDefaultLabelPaddingHorizEM = 0.6;
static const double kDefaultLabelPaddingVertEM = 0.6;

PlotBarsConfig::PlotBarsConfig() :
    direction(Direction::VERTICAL) {}

@@ -335,7 +335,13 @@ ReturnCode build(
    {"yscale-padding", bind(&expr_to_float64, _1, &config->scale_y.padding)},
    {"size", bind(&expr_to_measures, _1, &config->sizes)},
    {"sizes", bind(&expr_to_measures, _1, &config->sizes)},
    //{"direction", bind(&expr_to_direction, _1, &config->direction)},
    {
      "direction",
      expr_to_enum_fn<Direction>(&config->direction, {
        { "horizontal", Direction::HORIZONTAL },
        { "vertical", Direction::VERTICAL },
      })
    },
    {"color", expr_tov_fn<Color>(bind(&expr_to_color, _1, _2), &config->colors)},
    {"colors", expr_tov_fn<Color>(bind(&expr_to_color, _1, _2), &config->colors)},
    {"labels", bind(&expr_to_strings, _1, &config->labels)},