1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! The [`Document`] type for storing document content and metadata.
use crate::parser::Grammar;
/// Represents a document to be processed.
///
/// This struct holds the content of a file and its optional filename, which
/// is not available if the data to process was passed through the shell pipe (stdin).
#[derive(Debug, Clone)]
pub struct Document {
/// The content of the document as a string.
pub content: String,
/// The grammar (programming language) of the document.
pub grammar: Grammar,
/// The name of the file from which the content was read, if available.
/// This will be None if the content was read from stdin.
pub filename: Option<String>,
}
impl Document {
/// Creates a new document with the given content and filename.
///
/// # Arguments
///
/// * `content` - The content of the document.
/// * `grammar` - The grammar (programming language) of the document.
/// * `filename` - The optional name of the file from which the content was read.
/// None if the content was read from stdin.
pub fn new<S1: Into<String>, S2: Into<String>>(
content: S1,
grammar: Grammar,
filename: Option<S2>,
) -> Self {
Self {
content: content.into(),
grammar,
filename: filename.map(Into::into),
}
}
}