forge_fmt/
chunk.rs

1use crate::comments::CommentWithMetadata;
2
3/// Holds information about a non-whitespace-splittable string, and the surrounding comments
4#[derive(Clone, Debug, Default)]
5pub struct Chunk {
6    pub postfixes_before: Vec<CommentWithMetadata>,
7    pub prefixes: Vec<CommentWithMetadata>,
8    pub content: String,
9    pub postfixes: Vec<CommentWithMetadata>,
10    pub needs_space: Option<bool>,
11}
12
13impl From<String> for Chunk {
14    fn from(string: String) -> Self {
15        Self { content: string, ..Default::default() }
16    }
17}
18
19impl From<&str> for Chunk {
20    fn from(string: &str) -> Self {
21        Self { content: string.to_owned(), ..Default::default() }
22    }
23}
24
25// The struct with information about chunks used in the [Formatter::surrounded] method
26#[derive(Debug)]
27pub struct SurroundingChunk {
28    pub before: Option<usize>,
29    pub next: Option<usize>,
30    pub spaced: Option<bool>,
31    pub content: String,
32}
33
34impl SurroundingChunk {
35    pub fn new(
36        content: impl std::fmt::Display,
37        before: Option<usize>,
38        next: Option<usize>,
39    ) -> Self {
40        Self { before, next, content: format!("{content}"), spaced: None }
41    }
42
43    pub fn spaced(mut self) -> Self {
44        self.spaced = Some(true);
45        self
46    }
47
48    pub fn non_spaced(mut self) -> Self {
49        self.spaced = Some(false);
50        self
51    }
52
53    pub fn loc_before(&self) -> usize {
54        self.before.unwrap_or_default()
55    }
56
57    pub fn loc_next(&self) -> Option<usize> {
58        self.next
59    }
60}