6.7. kink/CONFIG_FUN_RUNNER

Runs a config function to get a map.

Example

:CONFIG_FUN_RUNNER.require_from('kink/')

:open_file <- {(:Config.opt)
  :config = Config.just{ {} }
  :Map = _run_config_fun($config)

  :Append? = Map.get('append')
  stdout.print('Append? = {}'.format(Append?.repr))

  Map.get_opt('buffer').with_elems(
    { stdout.print(', No buffer') }
    {(:Buf_size)
      stdout.print(', Buffer size = {}'.format(Buf_size))
    }
  )

  Map.get_opt('newline').with_elems(
    { stdout.print_line(', No newline') }
    {(:Newline)
      stdout.print_line(', Newline = {}'.format(Newline.repr))
    }
  )
}

:_run_config_fun <- CONFIG_FUN_RUNNER.new{(:Rc)
  Rc.no_arg('append')
  Rc.opt_arg('buffer' { 2048 })
  Rc.req_arg('newline')
}

open_file
# => Append? = false, No buffer, No newline

open_file{(:C)
  C.append
  C.buffer(1024)
  C.newline("\n")
}
# => Append? = true, Buffer size = 1024, Newline = "\n"

open_file{(:C)
  C.buffer
}
# => Append? = false, Buffer size = 2048, No newline

6.7.1. CONFIG_FUN_RUNNER.new($config)

`new` returns a runner function. The runner function will take a config function, and return a `map` of the configuration.

Behavior of the runner function

You can pass a config function as the argument of the runner function, The config value, which is passed to the config function, will have methods specified by $config.

The runner function returns a `map`. The keys of the map will be `str` values of the names of config methods. Equivalence of keys is determined by equality of `str` values. See methods of type `config_fun_runner_config` for entries of the map.

Precondition

$config must be a function which takes a `config_fun_runner_config` value.

6.7.2. type config_fun_runner_config

`config_fun_runner_config` is a config value for CONFIG_FUN_RUNNER.new.

6.7.2.1. Rc.req_arg(Method_name ...[$validate={}])

`req_arg` specifies that the config value will have a config method with the specified `Method_name`. The config method will take a mandatory argument.

If the config method is not called

If the config method is not called, the map will not have an entry with key `Method_name`.

When the config method is called

The config method calls $validate with the argument of the config method. If the argument is not acceptable, $validate must raise an exception.

If $validate does not raise an exception, an entry is set to the map, with `Method_name` as the key, and the argument of the config method as the value.

Preconditions

`Method_name` must be a `str`.

$validate must be a function which takes an argument.

6.7.2.2. Rc.req_fun(Method_name)

`req_fun` specifies that a config value will have a method with the specified `Method_name`. The config method will take a function as a mandatory argument.

If the config method is not called

If the config method is not called, the map will not have an entry with key `Method_name`.

When the config method is called

The config method validates that the argument is a function.

Besides validation, the behavior of the config method is same with one added by `req_arg`.

Precondition

`Method_name` must be a `str`.

6.7.2.3. Rc.no_arg(Method_name)

`no_arg` specifies that the config value will have a config method with the specified `Method_name`. The config method will take no argument.

If the config method is not called

If the config method is not called, the map will have an entry with `Method_name` as the key, and false as the value.

When the config method is called

When the config method is called, an entry is set to the map with `Method_name` as the key, and true as the value.

Precondition

`Method_name` must be a `str`.

6.7.2.4. Rc.opt_arg(Method_name $make_default ...[$validate={}])

`opt_arg` specifies that a config value will have a config method with the specified `Method_name`. The config method will take an optional argument.

If the config method is not called

If the config method is not called, the map will not have an entry with key `Method_name`.

If an argument is not passed to the config method

If the config method is called with no argument, the config method calls $make_default with no argument.

Then, an entry is set to the map, with `Method_name` as the key, and the result of $make_default as the value.

If an argument is passed to the config method

If the config method is called with an argument, the config method calls $validate with the argument. If the argument is not acceptable, $validate must raise an exception.

If $validate does not raise an exception, an entry is set to the map, with `Method_name` as the key, and the argument of the config method as the value.

Preconditions

`Method_name` must be a `str`.

$make_default must be a thunk.

$validate must be a function which takes an argument.

6.7.2.5. Rc.multi_args(Method_name ...[$validate={}])

`multi_args` specifies that a config value will have a config method with the specified `Method_name`.

The config method can be called multiple times, with arbitrary number of arguments. The arguments are stored in a vector `M` of vectors.

If the config method is not called

If the config method is not called, the map will have an entry with `Method_name` as the key, and an empty vector [], which is `M`, as the value.

When the config method is called

The config method calls $validate with the arguments of the config method. If the arguments are not acceptable, $validate must raise an exception.

If $validate does not raise an exception, a vector of the arguments is pushed to the vector `M` at the back.

Precondition

`Method_name` must be a `str`.

$validate must be a function which takes an argument.

Example

:CONFIG_FUN_RUNNER.require_from('kink/')
:STR.require_from('kink/')

:run <- CONFIG_FUN_RUNNER.new{(:C)
  C.multi_args('attr'){(:Name :Val)
    STR.is?(Name) || raise('Name must be a str, but was {}'.format(Name.repr))
  }
}

:Map <- run{(:C)
  C.attr('loglevel' 'INFO')
  C.attr('timeout' 42)
}
stdout.print_line(Map.get('attr').repr) # => [["loglevel" "INFO"] ["timeout" 42]]