diff --git a/src/code_block.rs b/src/code_block.rs index 8aae4fe2..bbd8c5ba 100644 --- a/src/code_block.rs +++ b/src/code_block.rs @@ -30,13 +30,14 @@ impl CodeBlock { } } - pub fn indent(&mut self) -> &mut Self { + pub fn indent(mut self) -> Self { self.content = self.content.replace('\n', "\n "); self } - pub fn add_block(&mut self, s: &T) { - self.write(&format!("\n{s}\n")); + pub fn add_block(&mut self, block: impl Into) { + let block: CodeBlock = block.into(); + self.write(&format!("\n{block}\n")); } pub fn is_empty(&self) -> bool { @@ -80,24 +81,13 @@ impl fmt::Display for CodeBlock { } } -/// Converts an iterator of strings into a CodeBlock. Each string is separated by a single newline. -impl FromIterator for CodeBlock { - fn from_iter>(iter: T) -> Self { - let mut code = CodeBlock::default(); - for i in iter { - code.writeln(&i); - } - code - } -} - /// Converts an iterator of CodeBlocks into a single CodeBlock. Each of the individual CodeBlocks /// are stringified and spaced with an empty line between them. impl FromIterator for CodeBlock { fn from_iter>(iter: T) -> Self { let mut code = CodeBlock::default(); - for i in iter { - code.add_block(&i); + for block in iter { + code.add_block(block); } code } @@ -118,10 +108,3 @@ impl From<&str> for CodeBlock { CodeBlock { content: s.to_owned() } } } - -impl From for String { - fn from(code: CodeBlock) -> Self { - // Do not return `code.content` here as we want the format function to be applied first - code.to_string() - } -}