4.7. kink/CONFIG_FUN_RUNNER

Read simple configuration from a config function as a map.

Example:

:CONFIG_FUN_RUNNER.require_from('kink/')

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

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

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

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

:_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

4.7.1. CONFIG_FUN_RUNNER.new($runner_config_fun)

`new` makes a function to get simple setup from config functions. Methods of config values are specified by $runner_config_fun.

Precondition:

• $runner_config_fun must be a function which takes a `runner_config` value.

`new` returns a function. Let's call this a runner function. The runner function takes a config function, which takes a config value containing the methods specified by $runner_config_fun. The runner function calls the config function with a config value, and returns a result map. See methods of `runner_config` type for entries of the result map.

4.7.2. type runner_config

Config value for CONFIG_FUN_RUNNER.new.

4.7.2.1. Rc.no_arg(Method_name)

`no_arg` specifies that a config value has a method with the specified `Method_name`, and the method does not take an argument.

Precondition:

• `Method_name` must be a str.

When the created method is called for a config value, an unspecified value is set to the result map associated with `Method_name` as the key.

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

`req_arg` specifies that a config value has a method with the specified `Method_name`, and the method takes an argument.

Preconditions:

• `Method_name` must be a str.

• $validate must be a fun which takes an argument.

When the created method is called for a config value, the argument of the method is set to the result map, associated with `Method_name` as the key.

$validate is called with the method argument before the value is set, so that it can validate the argument. If the method argument is not acceptable, $validate can raise an exception. The result of $validate is ignored.

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

`opt_arg` specifies that a config value has a method with the specified `Method_name`, and the method takes an optional argument.

Preconditions:

• `Method_name` must be a str

• $make_default must be a thunk.

• $validate must be a fun which takes an argument.

When the created method is called with an argument, the argument is set to the result map, associated with `Method_name` as the key. If the method is called without an argument, a value created by $make_default is set to the result map, associated with `Method_name` as the key.

$validate is called with the method argument before the value is set, so that it can validate the argument. If the method argument is not acceptable, $validate can raise an exception. The result of $validate is ignored.