std.fmt.Formatter
type pub Formatter
The default formatter to use when formatting an object.
Fields
maximum_depth
let pub @maximum_depth: Int
The maximum object depth before object formatting stops.
Static methods
new
Show source codeHide source code
fn pub static new -> Formatter {
Formatter(buffer: StringBuffer.new, nesting: 0, maximum_depth: 10)
}
fn pub static new -> Formatter
Returns a new Formatter
with its default settings.
Instance methods
array
Show source codeHide source code
fn pub mut array -> ArrayFormatter {
write('[')
ArrayFormatter(formatter: self, fields: 0)
}
fn pub mut array -> ArrayFormatter
Returns a ArrayFormatter
to make formatting array-like values easy.
Examples
import std.fmt (Formatter)
let fmt = Formatter.new
fmt.array.value(10).value(20).finish
fmt.into_string # => '[10, 20]'
descend
Show source codeHide source code
fn pub mut descend(block: fn) {
if @nesting >= @maximum_depth {
write(PLACEHOLDER)
return
}
@nesting += 1
block.call
@nesting -= 1
}
fn pub mut descend(block: fn)
Descends into a child object, running the supplied block if our nesting is not too great.
If nesting is too great, a placeholder value is added to the buffer, and the supplied block is not executed.
into_string
Show source codeHide source code
fn pub move into_string -> String {
@buffer.into_string
}
fn pub move into_string -> String
Moves self
into a String
, containing the formatted data.
object
Show source codeHide source code
fn pub mut object(name: String) -> ObjectFormatter {
let named = name.size > 0
if named { write(name) }
ObjectFormatter(formatter: self, named: named, fields: 0)
}
fn pub mut object(name: String) -> ObjectFormatter
Returns a ObjectFormatter
to make formatting regular objects easy.
The name
argument can be used as the type name of the value.
Examples
import std.fmt (Formatter)
let fmt = Formatter.new
fmt.object('Person').field('name', 'Alice').field('age', 42).finish
fmt.into_string # => 'Person(name: "Alice", age: 42)'
tuple
Show source codeHide source code
fn pub mut tuple(name: String) -> TupleFormatter {
let named = name.size > 0
if named { write(name) }
TupleFormatter(formatter: self, named: named, fields: 0)
}
fn pub mut tuple(name: String) -> TupleFormatter
Returns a TupleFormatter
to make formatting tuple-like values easy.
The name
argument can be used as the type name of the value. When
formatting actual tuples, this can be set to an empty String
to omit
adding a name.
Examples
import std.fmt (Formatter)
let fmt = Formatter.new
fmt.tuple('').field(10).field(20).finish
fmt.into_string # => '(10, 20)'
write
Show source codeHide source code
fn pub mut write(string: String) {
@buffer.push(string)
}
fn pub mut write(string: String)
Writes the given String
into the underlying buffer.
Implemented traits
IntoString
impl IntoString for Formatter