# Decoders in dune

**URL:** <https://discuss.ocaml.org/t/decoders-in-dune/15068>\
**Category:** Learning\
**Tags:** dune\
**Created:** [August 1, 2024, 5:14pm UTC](https://discuss.ocaml.org/t/decoders-in-dune/15068 "2024-08-01T17:14:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_A\_1](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@M\_A\_1](https://discuss.ocaml.org/u/M_A_1)\
**Post date:** [August 1, 2024, 5:14pm UTC](https://discuss.ocaml.org/t/decoders-in-dune/15068/1 "2024-08-01T17:14:52Z")

</div>

I am currently working on some project that involves Ocaml’s build system Dune. However, in my work, there is some tasks that require taking a file (mainly its path) which contains some text written in a S-expression format. For example, for those familiar with defining and building rules with dune,

`(rule (deps (file a) (glob *.cmi)) (action cat a))`

By investigating the dune codebase, I have understood that there is a decoder (which is actually a monad) that is defined for a type 'a, so that when this decoder is a ran on a certain S-expression it returns the object of type 'a defined in this S-expression (if it is a valid one).

My question is how exactly one could use such a defined decoder on custom file (that is not a dune file that is automatically parsed by dune) in order to extract the information? I assume that one should first parse the file in order to extract the S-expression, but I don’t know if that is automatically done by the decoder.`Preformatted text`

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [August 1, 2024, 10:51pm UTC](https://discuss.ocaml.org/t/decoders-in-dune/15068/2 "2024-08-01T22:51:59Z")

</div>

> [@M\_A\_1](#):
>
> My question is how exactly one could use such a defined decoder on custom file […] in order to extract the information?

Broadly speaking, the file can be parsed using the module `Parser`:

> <https://github.com/ocaml/dune/blob/0a8af6e1c8eb701e43cefa92bcfd131ec89ed6d7/src/dune_sexp/parser.mli#L15>

This will yield an `Ast.t` or `Ast.t list` (assuming you pass `Single` or `Many` for the `~mode` argument). Then a decoder can be run over this value by calling the only function available with the required signature:

> <https://github.com/ocaml/dune/blob/0a8af6e1c8eb701e43cefa92bcfd131ec89ed6d7/src/dune_sexp/decoder.mli#L35>

Cheers,  
Nicolas
