Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Canop committed Feb 5, 2023
1 parent e4b6914 commit 695286e
Show file tree
Hide file tree
Showing 19 changed files with 294 additions and 104 deletions.
3 changes: 3 additions & 0 deletions fmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rustup default nightly
cargo fmt
rustup default stable
4 changes: 4 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
edition = "2021"
version = "Two"
imports_layout = "Vertical"
fn_args_layout = "Vertical"
1 change: 0 additions & 1 deletion src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,3 @@ pub fn lines(src: &str) -> Vec<&str> {
}
result_lines
}

1 change: 0 additions & 1 deletion src/markdown/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ impl Alignment {
}
}
}

17 changes: 13 additions & 4 deletions src/markdown/composite.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// a composite is a group of compounds. It can be a whole line,
/// or a table cell

use crate::*;

/// The global style of a composite
Expand Down Expand Up @@ -126,7 +125,10 @@ impl<'a> Composite<'a> {
self.compounds.len() == 0
}
/// remove characters, and whole compounds if necessary
pub fn remove_chars_left(&mut self, mut to_remove: usize) {
pub fn remove_chars_left(
&mut self,
mut to_remove: usize,
) {
while to_remove > 0 {
if self.compounds.is_empty() {
return;
Expand All @@ -142,7 +144,10 @@ impl<'a> Composite<'a> {
}
}
/// remove characters, and whole compounds if necessary
pub fn remove_chars_right(&mut self, mut to_remove: usize) {
pub fn remove_chars_right(
&mut self,
mut to_remove: usize,
) {
while to_remove > 0 {
if self.compounds.is_empty() {
return;
Expand All @@ -163,7 +168,11 @@ impl<'a> Composite<'a> {
///
/// align is the alignment of the composite. If the composite is left
/// aligned, we remove chars at the right.
pub fn remove_chars(&mut self, to_remove: usize, align: Alignment) {
pub fn remove_chars(
&mut self,
to_remove: usize,
align: Alignment,
) {
match align {
Alignment::Left => {
self.remove_chars_right(to_remove);
Expand Down
79 changes: 63 additions & 16 deletions src/markdown/compound.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::fmt::{self, Write};

use std::fmt::{
self,
Write,
};

/// a Compound is a part of a line with a consistent styling.
/// It can be part of word, several words, some inline code, or even the whole line.
Expand All @@ -26,12 +28,18 @@ impl<'s> Compound<'s> {
}
}
/// change the content but keeps the style arguments
pub fn set_str(&mut self, src: &'s str) {
pub fn set_str(
&mut self,
src: &'s str,
) {
self.src = src;
}
/// change the attributes by taking the values from the other
/// compound, keeping the str
pub fn set_attributes_from(&mut self, other: &Compound) {
pub fn set_attributes_from(
&mut self,
other: &Compound,
) {
self.bold = other.bold;
self.italic = other.italic;
self.code = other.code;
Expand All @@ -41,7 +49,11 @@ impl<'s> Compound<'s> {
/// r_start is relative, that is 0 is the index of the first
/// byte of this compound.
#[inline(always)]
pub fn sub(&self, r_start: usize, r_end: usize) -> Compound<'s> {
pub fn sub(
&self,
r_start: usize,
r_end: usize,
) -> Compound<'s> {
Compound {
src: &self.src[r_start..r_end],
bold: self.bold,
Expand All @@ -57,7 +69,11 @@ impl<'s> Compound<'s> {
/// The difference with `sub` is that this method is unicode
/// aware and counts the chars instead of asking for the bytes
#[inline(always)]
pub fn sub_chars(&self, r_start: usize, r_end: usize) -> Compound<'s> {
pub fn sub_chars(
&self,
r_start: usize,
r_end: usize,
) -> Compound<'s> {
let mut rb_start = 0;
let mut rb_end = 0;
for (char_idx, (byte_idx, _)) in self.as_str().char_indices().enumerate() {
Expand All @@ -78,7 +94,10 @@ impl<'s> Compound<'s> {
/// r_start is relative, that is if you give 0 you get a clone of
/// this compound
#[inline(always)]
pub fn tail(&self, r_start: usize) -> Compound<'s> {
pub fn tail(
&self,
r_start: usize,
) -> Compound<'s> {
Compound {
src: &self.src[r_start..],
bold: self.bold,
Expand All @@ -94,7 +113,10 @@ impl<'s> Compound<'s> {
/// The difference with `tail` is that this method is unicode
/// aware and counts the chars instead of asking for the bytes
#[inline(always)]
pub fn tail_chars(&self, r_start: usize) -> Compound<'s> {
pub fn tail_chars(
&self,
r_start: usize,
) -> Compound<'s> {
let mut rb_start = 0;
for (char_idx, (byte_idx, _)) in self.as_str().char_indices().enumerate() {
rb_start = byte_idx;
Expand All @@ -107,7 +129,10 @@ impl<'s> Compound<'s> {

// shortens this compound by `tail_size` bytes and returns the tail
// as another compound
pub fn cut_tail(&mut self, tail_size: usize) -> Compound<'s> {
pub fn cut_tail(
&mut self,
tail_size: usize,
) -> Compound<'s> {
let cut = self.src.len() - tail_size;
let tail = Compound {
src: &self.src[cut..],
Expand All @@ -123,7 +148,11 @@ impl<'s> Compound<'s> {
// make a raw unstyled compound from part of a string
// Involves no parsing
#[inline(always)]
pub fn raw_part(src: &'s str, start: usize, end: usize) -> Compound<'s> {
pub fn raw_part(
src: &'s str,
start: usize,
end: usize,
) -> Compound<'s> {
Compound {
src: &src[start..end],
bold: false,
Expand Down Expand Up @@ -171,19 +200,31 @@ impl<'s> Compound<'s> {
self
}
#[inline(always)]
pub fn set_bold(&mut self, bold: bool) {
pub fn set_bold(
&mut self,
bold: bool,
) {
self.bold = bold;
}
#[inline(always)]
pub fn set_italic(&mut self, italic: bool) {
pub fn set_italic(
&mut self,
italic: bool,
) {
self.italic = italic;
}
#[inline(always)]
pub fn set_code(&mut self, code: bool) {
pub fn set_code(
&mut self,
code: bool,
) {
self.code = code;
}
#[inline(always)]
pub fn set_strikeout(&mut self, strikeout: bool) {
pub fn set_strikeout(
&mut self,
strikeout: bool,
) {
self.strikeout = strikeout;
}
#[inline(always)]
Expand All @@ -201,14 +242,20 @@ impl<'s> Compound<'s> {
}

impl fmt::Display for Compound<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fn fmt(
&self,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
f.write_str(self.as_str())?;
Ok(())
}
}

impl fmt::Debug for Compound<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fn fmt(
&self,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
if self.bold {
f.write_char('B')?;
}
Expand Down
1 change: 0 additions & 1 deletion src/markdown/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,3 @@ fn header_level_count() {
assert_eq!(header_level("######## a b"), 8);
assert_eq!(header_level("######### a b"), 0); // too deep
}

5 changes: 4 additions & 1 deletion src/markdown/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ impl Line<'_> {
compounds,
})
}
pub fn new_header(level: u8, compounds: Vec<Compound<'_>>) -> Line<'_> {
pub fn new_header(
level: u8,
compounds: Vec<Compound<'_>>,
) -> Line<'_> {
Line::Normal(Composite {
style: CompositeStyle::Header(level),
compounds,
Expand Down
15 changes: 12 additions & 3 deletions src/markdown/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ mod text;

pub use {
align::Alignment,
composite::{Composite, CompositeStyle},
composite::{
Composite,
CompositeStyle,
},
compound::Compound,
header::header_level,
line::{Line, MAX_HEADER_DEPTH},
tbl::{TableRow, TableRule},
line::{
Line,
MAX_HEADER_DEPTH,
},
tbl::{
TableRow,
TableRule,
},
text::Text,
};
1 change: 0 additions & 1 deletion src/markdown/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ impl<'s> Text<'s> {
crate::parser::parse_lines(md_lines, Options::default())
}
}

45 changes: 25 additions & 20 deletions src/parser/line_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ impl<'s> LineParser<'s> {
strikeout: false,
}
}
fn close_compound(&mut self, end: usize, tag_length: usize, compounds: &mut Vec<Compound<'s>>) {
fn close_compound(
&mut self,
end: usize,
tag_length: usize,
compounds: &mut Vec<Compound<'s>>,
) {
if end > self.idx {
compounds.push(Compound::new(
self.src,
Expand All @@ -45,10 +50,16 @@ impl<'s> LineParser<'s> {
}
self.idx = end + tag_length;
}
fn code_compound_from_idx(&self, idx: usize) -> Compound<'s> {
fn code_compound_from_idx(
&self,
idx: usize,
) -> Compound<'s> {
Compound::new(self.src, idx, self.src.len(), false, false, true, false)
}
fn parse_compounds(&mut self, stop_on_pipe: bool) -> Vec<Compound<'s>> {
fn parse_compounds(
&mut self,
stop_on_pipe: bool,
) -> Vec<Compound<'s>> {
let mut compounds = Vec::new();
let mut after_first_star = false;
let mut after_first_tilde = false;
Expand All @@ -71,12 +82,12 @@ impl<'s> LineParser<'s> {
continue;
}

#[cfg(feature="escaping")]
#[cfg(feature = "escaping")]
if after_antislash {
after_antislash = false;
match char {
'*' | '~' | '|' | '`' => {
self.close_compound(idx-1, 1, &mut compounds);
self.close_compound(idx - 1, 1, &mut compounds);
continue;
}
'\\' => {
Expand All @@ -85,7 +96,7 @@ impl<'s> LineParser<'s> {
}
_ => {} // we don't escape at all normal chars
}
} else if char=='\\' {
} else if char == '\\' {
after_antislash = true;
continue;
}
Expand Down Expand Up @@ -390,13 +401,11 @@ mod tests {
);
}

#[cfg(feature="escaping")]
#[cfg(feature = "escaping")]
#[test]
fn escapes() {
assert_eq!(
Line::from(
"no \\*italic\\* here"
),
Line::from("no \\*italic\\* here"),
Line::new_paragraph(vec![
Compound::raw_str("no "),
Compound::raw_str("*italic"),
Expand All @@ -407,17 +416,11 @@ mod tests {
// we're not losing the '\' when it's not escaping something
// (only markdown modifiers can be escaped)
assert_eq!(
Line::from(
"a\\bc\\"
),
Line::new_paragraph(vec![
Compound::raw_str("a\\bc\\"),
])
Line::from("a\\bc\\"),
Line::new_paragraph(vec![Compound::raw_str("a\\bc\\"),])
);
assert_eq!(
Line::from(
"*italic\\*and\\*still\\*italic*"
),
Line::from("*italic\\*and\\*still\\*italic*"),
Line::new_paragraph(vec![
Compound::raw_str("italic").italic(),
Compound::raw_str("*and").italic(),
Expand All @@ -426,7 +429,9 @@ mod tests {
])
);
assert_eq!(
Line::from("\\**Italic then **bold\\\\ and \\`italic `and some *code*`** and italic*\\*"),
Line::from(
"\\**Italic then **bold\\\\ and \\`italic `and some *code*`** and italic*\\*"
),
Line::new_paragraph(vec![
Compound::raw_str("*"),
Compound::raw_str("Italic then ").italic(),
Expand Down

0 comments on commit 695286e

Please sign in to comment.