32 lines
786 B
Rust
32 lines
786 B
Rust
pub trait Indentable<T> {
|
|
fn indent(&self, spaces: T) -> String;
|
|
}
|
|
|
|
impl Indentable<usize> for &str {
|
|
fn indent(&self, spaces: usize) -> String {
|
|
let indent_str = " ".repeat(spaces);
|
|
self.lines()
|
|
.map(|line| format!("{}{}", indent_str, line))
|
|
.collect::<Vec<String>>()
|
|
.join("\n")
|
|
}
|
|
}
|
|
|
|
impl Indentable<Option<usize>> for String {
|
|
fn indent(&self, spaces: Option<usize>) -> String {
|
|
self.as_str().indent(spaces.unwrap_or(0))
|
|
}
|
|
}
|
|
|
|
impl Indentable<usize> for String {
|
|
fn indent(&self, spaces: usize) -> String {
|
|
self.as_str().indent(spaces)
|
|
}
|
|
}
|
|
|
|
impl Indentable<Option<usize>> for &str {
|
|
fn indent(&self, spaces: Option<usize>) -> String {
|
|
self.indent(spaces.unwrap_or(0))
|
|
}
|
|
}
|