Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=cd3cf73c31f6b0e18677ce92ce0e6f866b0596d5ea1fc540212c422930a730a0
Description
Part of the Jane Street's PPX rewriters collection.
Added to opam-repository:
README
ppx_custom_printf
Extensions to printf-style format-strings for user-defined string conversion.
ppx_custom_printf is a ppx rewriter that allows the use of user-defined string conversion functions in format strings (that is, strings passed to printf, sprintf, etc.).
No new syntax is introduced. Instead a previously ill-typed use of the ! operator is re-purposed.
Basic Usage
The basic usage is as follows:
printf !"The time is %{Time} and the timezone is %{Time.Zone}."
time zoneThe ppx rewriter will turn the !-string into a format of type (Time.t -> Time.Zone.t -> unit, unit, string) format. This is done by embedding the Time.to_string and Time.Zone.to_string functions into the format, using the low-level format mechanism of the stdlib.
In general, specifiers like %{<Module-path>} produce a call to Module-path.to_string. The module path can even be empty, in which case the generated code calls to_string.
Note that you have to prepend the format string with a !, so that the ppx rewriter knows to operate on it.
Sexps
The syntax %{sexp:<type>} is also supported. For example:
printf !"The time is %{sexp:Time.t}." timeThe time argument will be turned into a string using:
fun x -> Sexplib.Sexp.to_string_hum ([%sexp_of: Time.t] x)This supports arbitrary type expressions.
You can use Sexplib.Sexp.to_string_mach instead of Sexplib.Sexp.to_string_hum by using %{sexp#mach:<type>}
Using functions other than M.to_string
The format specifier %{<Module-path>.<lowercase_identifier>} corresponds to that function. So, for example:
printf !"The date is %{Core.Date.to_string_iso8601_basic}" datewill turn date to a string using the following code:
fun x -> Core.Date.to_string_iso8601_basic xFurther, the format specifier %{<Module-path>#<lowercase_identifier>} corresponds to the function <Module_path>.to_string_<lowercase_identifier>. So, for example:
printf !"The date is %{Core.Date#american}" datewill turn date to a string using:
fun x -> Core.Date.to_string_american xSubformats disallowed
In a regular format string, you can use format specifiers of the form %{<spec>%} and %(<spec>%) where <spec> is another format specifier.
Using these specifiers is disallowed in format strings that are processed with custom-printf.
Dependencies (5)
-
ppxlib
>= "0.28.0" -
dune
>= "3.11.0" -
ppx_sexp_conv
>= "v0.17" & < "v0.18~" -
base
>= "v0.17" & < "v0.18~" -
ocaml
>= "5.1.0"
Dev Dependencies
None
Used by (8)
Conflicts
None
Build status
Results of building ppx_custom_printf with each compiler on check.ci.ocaml.org, run of against opam-repository 7cdc698. All results for this package.
Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.
| Version | OCaml 4.11 | OCaml 4.14 | OCaml 5.2 | OCaml 5.4 | OCaml 5.5 |
|---|---|---|---|---|---|
| v0.17.0 (this version) | n/a | n/a | good | good | good |
| v0.16.0 | n/a | good | good | n/a | n/a |
| v0.15.0 | good | good | good | n/a | n/a |
- good Package successfully built
- partial One of the dependencies failed to build
- bad Package failed to build
- n/a Package is not available in this environment
- internal Internal failure