fmglee
fmglee
A string formatting library for gleam.
Valid format specifiers are
- %s - String
- %d - Int
- %f - Float
Examples
import fmglee
import gleeunit/should
fmglee.new("Number %d, float %f, string %s")
|> fmglee.d(99)
|> fmglee.f(12.9)
|> fmglee.s("Hello!")
|> fmglee.build
|> should.equal("Number 99, float 12.9, string Hello!")
fmt.fmt("Number %d, float %f, string %s", with: [fmt.D(99), fmt.F(12.9), fmt.S("Hello!")])
|> should.equal("Number 99, float 12.9, string Hello!")
Types
pub type Fmt {
S(String)
D(Int)
F(Float)
}
Constructors
-
S(String)
Wrap a
String
for use withfmt(_, using: [..])
-
D(Int)
Wrap an
Int
for use withfmt(_, using: [..])
-
F(Float)
Wrap a
Float
for use withfmt(_, using: [..])
Errors returned by fmtglee
pub type FmtError {
IncorrectValueType
TooManyValues
NotEnoughValues
}
Constructors
-
IncorrectValueType
The current value does not match the current placeholder. Returned when the order of
Fmt
s is not the same as the order of placeholders. -
TooManyValues
Returned when there are more
Fmt
s than placeholders in the string. -
NotEnoughValues
Returned when there are less
Fmt
s than placeholders in the string.
Functions
pub fn build(formatter: Formatter) -> String
Compile a Formatter into a string. Panics where
try_build(formatter)
returns an FmtError
.
pub fn fmt(s: String, with v: List(Fmt)) -> String
Format a string and a list of Fmt
values.
Will panic where try_fmt(s, v)
returns an
FmtError
.
pub fn new(s: String) -> Formatter
Create a new Formatter
.
Examples
new("Hello, %s. You are %d years old.")
|> s(name)
|> d(age)
|> build
|> io.println
pub fn print(s: String, with v: List(Fmt)) -> Nil
Compile the format string and print the output.
Panics if fmt(s, v)
would panic with the
provided arguments.
pub fn printf(formatter: Formatter) -> Nil
Print the output of a compiled Formatter
.
Panics if the Formatter
is invalid.
pub fn println(s: String, with v: List(Fmt)) -> Nil
Compile the format string and print the output
with a newline. Panics if fmt(s, v)
would
panic with the provided arguments.
pub fn printlnf(formatter: Formatter) -> Nil
Print the output of a compiled Formatter
with a newline. Panics if the Formatter
is invalid.
pub fn try_build(
formatter: Formatter,
) -> Result(String, FmtError)
Compile a Formatter
into a string. Errors when the
number of provided values does not match the number
of placeholders, or if an invalid type was provided
for a placeholder.
pub fn try_fmt(
s: String,
with v: List(Fmt),
) -> Result(String, FmtError)
Format a string and a list of Fmt
values. Substitutes
placeholders from left to right with values in the
list of Fmt. Errors if there is a type missmatch between
the placeholder and the value, or if the number of placeholders
does not match the number of values given.
pub fn write(
s: String,
with v: List(Fmt),
using writer: fn(String) -> Nil,
) -> Nil
Write a string and list of Fmt
to the provided Writer
.