/*
  This file is part of TALER
  (C) 2026 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file src/util/contract_version_parse.c
 * @brief parser for the contract version
 * @author Christian Grothoff
 */
#include "platform.h"
#include <string.h>
#include "taler/taler_merchant_util.h"

/**
 * Parse contract version of given JSON contract terms.
 *
 * @param cls closure, unused parameter
 * @param root the JSON object representing data
 * @param[out] spec where to write the data
 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
 */
static enum GNUNET_GenericReturnValue
parse_contract_version (void *cls,
                        json_t *root,
                        struct GNUNET_JSON_Specification *spec)
{
  enum TALER_MERCHANT_ContractVersion *res
    = (enum TALER_MERCHANT_ContractVersion *) spec->ptr;

  (void) cls;
  if (json_is_integer (root))
  {
    json_int_t version = json_integer_value (root);

    switch (version)
    {
    case 0:
      *res = TALER_MERCHANT_CONTRACT_VERSION_0;
      return GNUNET_OK;
    case 1:
      *res = TALER_MERCHANT_CONTRACT_VERSION_1;
      return GNUNET_OK;
    }

    GNUNET_break_op (0);
    return GNUNET_SYSERR;
  }

  if (json_is_null (root))
  {
    *res = TALER_MERCHANT_CONTRACT_VERSION_0;
    return GNUNET_OK;
  }

  GNUNET_break_op (0);
  return GNUNET_SYSERR;
}


struct GNUNET_JSON_Specification
TALER_MERCHANT_spec_contract_version (
  const char *name,
  enum TALER_MERCHANT_ContractVersion *version)
{
  struct GNUNET_JSON_Specification ret = {
    .parser = &parse_contract_version,
    .field = name,
    .ptr = version
  };

  *version = TALER_MERCHANT_CONTRACT_VERSION_0;
  return ret;
}
