use toml_edit::Decor;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Marker {
pub(crate) prose: String,
pub(crate) sample: String,
}
pub const DEFAULT_PROSE_MARKER: &str = "##:";
pub const DEFAULT_SAMPLE_MARKER: &str = "#:";
impl Marker {
pub fn new(prose: impl Into<String>, sample: impl Into<String>) -> Self {
Self {
prose: prose.into(),
sample: sample.into(),
}
}
pub fn prose(&self) -> &str {
&self.prose
}
pub fn sample(&self) -> &str {
&self.sample
}
pub fn classify(&self, line: &str) -> PrefixLine {
let text = line.to_owned();
if line.trim().is_empty() {
PrefixLine::Blank { text }
} else if carries(&self.prose, line) {
PrefixLine::Prose { text }
} else if carries(&self.sample, line) {
PrefixLine::Sample { text }
} else {
PrefixLine::User { text }
}
}
pub fn owns(&self, line: &str) -> bool {
matches!(
self.classify(line),
PrefixLine::Prose { .. } | PrefixLine::Sample { .. }
)
}
pub fn undress(&self, line: &str) -> String {
let text = line.trim_start();
let body = text
.strip_prefix(&self.sample)
.or_else(|| text.strip_prefix(&self.prose))
.unwrap_or(text);
body.strip_prefix(' ').unwrap_or(body).to_owned()
}
fn echo_line(&self, echo: &DefaultEcho) -> String {
format!("{} {} = {}", self.sample, echo.key, echo.value)
}
}
impl Default for Marker {
fn default() -> Self {
Self::new(DEFAULT_PROSE_MARKER, DEFAULT_SAMPLE_MARKER)
}
}
fn carries(marker: &str, line: &str) -> bool {
let text = line.trim_start();
text.starts_with(marker) && hashes(text) == hashes(marker)
}
fn hashes(text: &str) -> usize {
text.chars().take_while(|c| *c == '#').count()
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Prefix {
pub(crate) raw: String,
}
impl Prefix {
pub fn of(decor: &Decor) -> Self {
let raw = decor
.prefix()
.and_then(|raw| raw.as_str())
.unwrap_or_default()
.to_owned();
Self { raw }
}
pub fn from_text(text: &str) -> Self {
Self {
raw: text.to_owned(),
}
}
pub fn lines(&self, marker: &Marker) -> Vec<PrefixLine> {
let mut parts: Vec<&str> = self.raw.split('\n').collect();
parts.pop();
parts
.into_iter()
.map(|line| marker.classify(line))
.collect()
}
pub fn split(&self, marker: &Marker) -> (Vec<PrefixLine>, Vec<PrefixLine>) {
let lines = self.lines(marker);
let touching = lines
.iter()
.rposition(|line| matches!(line, PrefixLine::Blank { .. }))
.map_or(0, |at| at + 1);
(lines[..touching].to_vec(), lines[touching..].to_vec())
}
pub fn indent(&self) -> &str {
match self.raw.rfind('\n') {
Some(at) => &self.raw[at + 1..],
None => &self.raw,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PrefixLine {
Prose {
text: String,
},
Sample {
text: String,
},
User {
text: String,
},
Blank {
text: String,
},
}
impl PrefixLine {
pub fn text(&self) -> &str {
match self {
Self::Prose { text }
| Self::Sample { text }
| Self::User { text }
| Self::Blank { text } => text,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum Sample {
#[default]
None,
Echo(DefaultEcho),
Lines(Vec<String>),
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct DocBlock {
pub(crate) floating: Vec<String>,
pub(crate) leading_blanks: Vec<String>,
pub(crate) prose: Vec<String>,
pub(crate) sample: Sample,
pub(crate) user_lines: Vec<String>,
pub(crate) indent: String,
}
impl DocBlock {
pub(crate) fn keep_user_text(&mut self, prefix: &Prefix, marker: &Marker) {
let lines = prefix.lines(marker);
let mut seen_content = false;
let mut blanks = Vec::new();
for line in lines {
match line {
PrefixLine::Blank { text } if !seen_content => self.leading_blanks.push(text),
PrefixLine::Blank { text } => blanks.push(text),
PrefixLine::Prose { .. } | PrefixLine::Sample { .. } => {
seen_content = true;
blanks.clear();
}
PrefixLine::User { text } => {
seen_content = true;
if !self.user_lines.is_empty() {
self.user_lines.append(&mut blanks);
} else {
blanks.clear();
}
self.user_lines.push(text);
}
}
}
if !self.user_lines.is_empty() {
self.user_lines.append(&mut blanks);
}
self.indent = prefix.indent().to_owned();
}
pub(crate) fn take_docs(&mut self, touching: &[PrefixLine]) {
for line in touching {
match line {
PrefixLine::Prose { text } => self.prose.push(text.clone()),
PrefixLine::Sample { text } => match &mut self.sample {
Sample::Lines(lines) => lines.push(text.clone()),
_ => self.sample = Sample::Lines(vec![text.clone()]),
},
_ => {}
}
}
}
pub(crate) fn render(&self, marker: &Marker) -> String {
let mut out = String::new();
for line in &self.floating {
out.push_str(line);
out.push('\n');
}
if self.floating.is_empty() {
for line in &self.leading_blanks {
out.push_str(line);
out.push('\n');
}
}
for line in &self.prose {
out.push_str(line);
out.push('\n');
}
match &self.sample {
Sample::None => {}
Sample::Echo(echo) => {
out.push_str(&marker.echo_line(echo));
out.push('\n');
}
Sample::Lines(lines) => {
for line in lines {
out.push_str(line);
out.push('\n');
}
}
}
for line in &self.user_lines {
out.push_str(line);
out.push('\n');
}
out.push_str(&self.indent);
out
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DefaultEcho {
pub(crate) key: String,
pub(crate) value: String,
}