drop extra vector, replace GString with String, use const for common values

This commit is contained in:
yggverse 2024-12-02 20:04:27 +02:00
parent 9b531bfd82
commit 5002fc392b

View file

@ -1,11 +1,12 @@
pub mod error; pub mod error;
pub use error::Error; pub use error::Error;
use glib::GString; pub const NEW_LINE: char = '\n';
pub const TAG: &str = "```";
pub struct Multiline { pub struct Multiline {
pub alt: Option<GString>, pub alt: Option<String>,
pub buffer: Vec<GString>, pub value: String,
pub completed: bool, pub completed: bool,
} }
@ -13,15 +14,15 @@ impl Multiline {
/// Search in line for tag open, /// Search in line for tag open,
/// return Self constructed on success or None /// return Self constructed on success or None
pub fn begin_from(line: &str) -> Option<Self> { pub fn begin_from(line: &str) -> Option<Self> {
if line.starts_with("```") { if line.starts_with(TAG) {
let alt = line.trim_start_matches("```").trim(); let alt = line.trim_start_matches(TAG).trim();
return Some(Self { return Some(Self {
alt: match alt.is_empty() { alt: match alt.is_empty() {
true => None, true => None,
false => Some(GString::from(alt)), false => Some(String::from(alt)),
}, },
buffer: Vec::new(), value: String::new(),
completed: false, completed: false,
}); });
} }
@ -37,14 +38,15 @@ impl Multiline {
return Err(Error::Completed); return Err(Error::Completed);
} }
// Line contain close tag // Append to value, trim close tag on exists
if line.ends_with("```") { self.value.push_str(line.trim_end_matches(TAG));
self.completed = true;
}
// Append data to the buffer, trim close tag on exists // Line contain close tag
self.buffer if line.ends_with(TAG) {
.push(GString::from(line.trim_end_matches("```"))); self.completed = true;
} else {
self.value.push(NEW_LINE);
}
Ok(()) Ok(())
} }