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!")
fmglee.sprintf("Number %d, float %f, string %s", with: [fmglee.D(99), fmglee.F(12.9), fmglee.S("Hello!")])
|> should.equal("Number 99, float 12.9, string Hello!")
fmglee.sprintf("%,.2f", [fmglee.F(1234.554)])
|> should.equal("1,234.55")
Types
pub type Fmt {
S(String)
D(Int)
F(Float)
}
Constructors
-
S(String)
Wrap a
String
for use withsprintf(_, using: [..])
-
D(Int)
Wrap an
Int
for use withsprintf(_, using: [..])
-
F(Float)
Wrap a
Float
for use withfmt(_, using: [..])
Errors returned by fmtglee
pub type FmtError {
IncorrectValueType(expected: String, got: Fmt)
TooManyValues
NotEnoughValues
InvalidFloat(String)
InvalidInt(String)
InvalidFloatFormatSpecifier(String)
}
Constructors
-
IncorrectValueType(expected: String, got: Fmt)
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. -
InvalidFloat(String)
Returned when a number has no floating point.
-
InvalidInt(String)
Returned when an invalid Integer value is provided to a Float formatter.
-
InvalidFloatFormatSpecifier(String)
Returned when a float format specifier cannot be parsed. Can also occur when a
Float
value is provided alongside aString
orInt
placeholder.
Functions
pub fn build(formatter: Formatter) -> String
Compile a Formatter into a string. Panics where
try_build(formatter)
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
new("Hello, %s. You are %d years old.")
|> s(name)
|> d(age)
|> printlnf
pub fn print(s: String, with v: List(Fmt)) -> Nil
Compile the format string and print the output.
Panics if sprintf(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 sprintf(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 sprintf(s: String, with v: List(Fmt)) -> String
Format a string and a list of Fmt
values.
Will panic where try_sprintf(s, v)
returns an
FmtError
.
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_sprintf(
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 mismatch between
the placeholder and the value, or if the number of placeholders
does not match the number of values given.
pub fn try_write(
s: String,
with v: List(Fmt),
using writer: fn(String) -> a,
) -> Result(a, FmtError)
Write a string and a list of Fmt
to the provided Writer
.
Errors when the format string is incompatible with the provided
Fmt
values.
pub fn try_writef(
formatter: Formatter,
using writer: fn(String) -> a,
) -> Result(a, FmtError)
Writer a Formatter
to the provided writer. The Formatter
will be built using try_sprintf
which will error if the
formatter is invalid.
pub fn write(
s: String,
with v: List(Fmt),
using writer: fn(String) -> a,
) -> a
Write a string and list of Fmt
to the provided Writer
.