use crate::element::Element;
#[derive(Debug)]
pub struct MultiLineComment {
start : String,
line : String,
end : String,
text : String
}
impl MultiLineComment {
pub fn new( start : & str, line : & str, end : & str, text : & str ) -> MultiLineComment {
MultiLineComment {
start : String::from( start ),
line : String::from( line ),
end : String::from( end ),
text : String::from( text )
}
}
pub fn add( & mut self, text : String ) {
self.text.push_str( & text );
}
}
impl Element for MultiLineComment {
fn to_source_code( & self, indent : usize ) -> String {
let mut source = String::new();
if self.text.is_empty() {
return source;
}
let indent_str = crate::util::indent( indent );
let split = self.text.split( '\n' );
let mut start = & self.start;
for i in split {
if i.len() == 0 {
source.push_str( & format!( "{}{}\n", & indent_str, & start ) );
}
else {
source.push_str( & format!( "{}{} {}\n", & indent_str, & start, & i ) );
}
start = & self.line;
}
source.push_str( & format!( "{}{}\n", & indent_str, & self.end ) );
return source;
}
}
impl std::fmt::Display for MultiLineComment {
fn fmt( & self, f : & mut std::fmt::Formatter<'_> ) -> std::fmt::Result {
write!( f, "{}", self.to_source_code( 0 ) )
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn multi_line_single() {
let c1 = MultiLineComment::new(
"/**",
" *",
"*/",
"Single line of text"
);
assert_eq!(
c1.to_source_code( 1 ),
String::from( " /** Single line of text\n */\n" )
);
}
#[test]
fn multi_line() {
let c1 = MultiLineComment::new(
"/**",
" *",
" */",
"First line of text\nsecond line\n\nfourth line"
);
assert_eq!(
c1.to_source_code( 2 ),
String::from(
concat!(
" /** First line of text\n",
" * second line\n",
" *\n",
" * fourth line\n",
" */\n"
)
)
);
}
}