#![warn(missing_docs)]
#![warn(rustdoc::missing_doc_code_examples)]
use std::fmt::{Debug, Write};
pub trait WriteStringExt
{
fn write(&mut self, str:&str) -> &mut Self;
}
impl WriteStringExt for String{
fn write(&mut self ,str: &str) ->&mut String {
write!(self, "{}", str).expect("failed to push to string!");
self
}
}
pub trait WriteLnStringExt{
fn writeln(&mut self, str:&str) -> &mut Self;
}
impl WriteLnStringExt for String{
fn writeln(&mut self, str: &str) ->&mut String {
writeln!(self, "{}", str).expect("failed to push to string!");
self
}
}
pub trait WriteDebugStringExt<T>
where T: Debug
{
fn write_debug(&mut self, t: T) -> &mut String;
}
impl<T> WriteDebugStringExt<T> for String
where T: Debug
{
fn write_debug(&mut self, t: T) -> &mut String {
write!(self, "{:?}",t).expect("failed to push to string!");
self
}
}
pub trait WriteLnDebugStringExt<T> where T: Debug{
fn writeln_debug(&mut self, t: T) -> &mut String;
}
impl<T> WriteLnDebugStringExt<T> for String
where T: Debug
{
fn writeln_debug(&mut self, t: T) -> &mut String {
writeln!(self, "{:?}",t).expect("failed to push to string!");
self
}
}
#[cfg(test)]
mod tests{
use crate::WriteStringExt;
use crate::WriteLnStringExt;
use crate::WriteDebugStringExt;
use crate::WriteLnDebugStringExt;
#[test]
pub fn write_test(){
let mut string = String::new();
string.write("hello")
.write(" world!");
assert_eq!("hello world!".to_string(), string);
}
#[test]
pub fn writeln_test(){
let mut string = String::new();
string.writeln("hello")
.writeln("world!");
assert_eq!("hello\nworld!\n".to_string(), string);
}
#[test]
pub fn write_debug(){
let mut string = String::new();
let item = (1,'a');
string.write_debug(item)
.write_debug(item);
assert_eq!("(1, 'a')(1, 'a')".to_string(), string);
}
#[test]
pub fn writeln_debug(){
let mut string = String::new();
let item = (1,'a');
string.writeln_debug(item)
.writeln_debug(item);
assert_eq!("(1, 'a')\n(1, 'a')\n".to_string(), string);
}
#[test]
pub fn multiple(){
let pink = (255, 27, 141);
let yellow = (255, 218, 0);
let blue = (27, 179, 255);
let mut string = String::new();
string.writeln("hello world!")
.writeln("The pan flag's colours are (in rgb): ")
.write("pink: ").writeln_debug(pink)
.write("yellow: ").writeln_debug(yellow)
.write("blue: ").write_debug(blue);
assert_eq!("hello world!\nThe pan flag's colours are (in rgb): \npink: (255, 27, 141)\nyellow: (255, 218, 0)\nblue: (27, 179, 255)", string);
}
}