lngh_strings/lib.rs
1#![warn(missing_docs)]
2#![warn(rustdoc::missing_doc_code_examples)]
3//! A small library with some convenient string helpers.
4//!
5//! ```
6//! use lngh_strings::{WriteStringExt,WriteLnStringExt,WriteDebugStringExt,WriteLnDebugStringExt};
7//!
8//! let pink = (255, 27, 141);
9//! let yellow = (255, 218, 0);
10//! let blue = (27, 179, 255);
11//!
12//! let mut string = String::new();
13//! string.writeln("hello world!")
14//! .writeln("The pan flag's colours are (in rgb):")
15//! .write("pink: ").writeln_debug(pink)
16//! .write("yellow: ").writeln_debug(yellow)
17//! .write("blue: ").write_debug(blue);
18//!
19//!let expected =
20//!r#"hello world!
21//!The pan flag's colours are (in rgb):
22//!pink: (255, 27, 141)
23//!yellow: (255, 218, 0)
24//!blue: (27, 179, 255)"#;
25//! assert_eq!(expected, string);
26//! ```
27use std::fmt::{Debug, Write};
28
29///This an extension trait specifically for appending &str's to strings conveniently.
30pub trait WriteStringExt
31{
32 ///Writes a str to a string. Can be chained.
33 ///Will panic in the circumstance circumstance that write! fails.
34 /// Example:
35 /// ```
36 /// use lngh_strings::WriteStringExt;
37 ///
38 /// let mut string = String::new();
39 /// string.write("hello")
40 /// .write(" world!");
41 /// assert_eq!("hello world!".to_string(), string);
42 /// ```
43 fn write(&mut self, str:&str) -> &mut Self;
44}
45
46
47impl WriteStringExt for String{
48 fn write(&mut self ,str: &str) ->&mut String {
49 write!(self, "{}", str).expect("failed to push to string!");
50 self
51 }
52}
53
54///This an extension trait specifically for appending to strings conveniently.
55pub trait WriteLnStringExt{
56 ///Writes a str to a string. Can be chained.
57 ///Will panic in the circumstance circumstance that writeln! fails.
58 /// Example:
59 /// ```
60 /// use lngh_strings::WriteLnStringExt;
61 ///
62 /// let mut string = String::new();
63 /// string.writeln("hello")
64 /// .writeln("world!");
65 /// assert_eq!("hello\nworld!\n".to_string(), string);
66 /// ```
67 fn writeln(&mut self, str:&str) -> &mut Self;
68}
69
70impl WriteLnStringExt for String{
71 fn writeln(&mut self, str: &str) ->&mut String {
72 writeln!(self, "{}", str).expect("failed to push to string!");
73 self
74 }
75}
76
77
78///This an extension trait specifically for appending items with the Debug trait to strings conveniently.
79pub trait WriteDebugStringExt<T>
80 where T: Debug
81{
82 ///Gets the debug formatting for an item and appends it to the string. Can be chained.
83 /// Will panic in the circumstance circumstance that write! fails.
84 /// Example:
85 /// ```
86 /// use lngh_strings::WriteDebugStringExt;
87 ///
88 /// let mut string = String::new();
89 /// let item = (1,'a');
90 /// string.write_debug(item)
91 /// .write_debug(item);
92 /// assert_eq!("(1, 'a')(1, 'a')".to_string(), string);
93 ///
94 /// ```
95 fn write_debug(&mut self, t: T) -> &mut String;
96}
97
98impl<T> WriteDebugStringExt<T> for String
99 where T: Debug
100{
101 fn write_debug(&mut self, t: T) -> &mut String {
102 write!(self, "{:?}",t).expect("failed to push to string!");
103 self
104 }
105}
106
107
108///This an extension trait specifically for appending items with the Debug trait and a newline to strings conveniently.
109pub trait WriteLnDebugStringExt<T> where T: Debug{
110 ///Gets the debug formatting for an item and appends it to the string and then a new line. Can be chained.
111 /// Will panic in the circumstance circumstance that write! fails.
112 /// Example:
113 /// ```
114 /// use lngh_strings::WriteLnDebugStringExt;
115 ///
116 /// let mut string = String::new();
117 /// let item = (1,'a');
118 /// string.writeln_debug(item)
119 /// .writeln_debug(item);
120 /// assert_eq!("(1, 'a')\n(1, 'a')\n".to_string(), string);
121 ///
122 /// ```
123 fn writeln_debug(&mut self, t: T) -> &mut String;
124}
125
126impl<T> WriteLnDebugStringExt<T> for String
127 where T: Debug
128{
129 fn writeln_debug(&mut self, t: T) -> &mut String {
130 writeln!(self, "{:?}",t).expect("failed to push to string!");
131 self
132 }
133}
134
135
136
137//TODO: hide this away in a better place!
138#[cfg(test)]
139mod tests{
140 use crate::WriteStringExt;
141 use crate::WriteLnStringExt;
142 use crate::WriteDebugStringExt;
143 use crate::WriteLnDebugStringExt;
144
145 #[test]
146 pub fn write_test(){
147 let mut string = String::new();
148 string.write("hello")
149 .write(" world!");
150 assert_eq!("hello world!".to_string(), string);
151
152 }
153
154
155 #[test]
156 pub fn writeln_test(){
157 let mut string = String::new();
158 string.writeln("hello")
159 .writeln("world!");
160 assert_eq!("hello\nworld!\n".to_string(), string);
161 }
162
163 #[test]
164 pub fn write_debug(){
165 let mut string = String::new();
166 let item = (1,'a');
167 string.write_debug(item)
168 .write_debug(item);
169
170 assert_eq!("(1, 'a')(1, 'a')".to_string(), string);
171 }
172
173 #[test]
174 pub fn writeln_debug(){
175
176 let mut string = String::new();
177 let item = (1,'a');
178 string.writeln_debug(item)
179 .writeln_debug(item);
180 assert_eq!("(1, 'a')\n(1, 'a')\n".to_string(), string);
181 }
182
183 #[test]
184 pub fn multiple(){
185 let pink = (255, 27, 141);
186 let yellow = (255, 218, 0);
187 let blue = (27, 179, 255);
188
189 let mut string = String::new();
190 string.writeln("hello world!")
191 .writeln("The pan flag's colours are (in rgb): ")
192 .write("pink: ").writeln_debug(pink)
193 .write("yellow: ").writeln_debug(yellow)
194 .write("blue: ").write_debug(blue);
195 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);
196 }
197}