lychee_lib/types/input/
content.rs

1//! Input content representation and construction.
2//!
3//! The `InputContent` type represents the actual content extracted from various
4//! input sources, along with metadata about the source and file type.
5
6use super::source::ResolvedInputSource;
7use crate::types::FileType;
8use std::borrow::Cow;
9
10/// Encapsulates the content for a given input
11#[derive(Debug)]
12pub struct InputContent {
13    /// Input source
14    pub source: ResolvedInputSource,
15    /// File type of given input
16    pub file_type: FileType,
17    /// Raw UTF-8 string content
18    pub content: String,
19}
20
21impl InputContent {
22    #[must_use]
23    /// Create an instance of `InputContent` from an input string
24    pub fn from_string(s: &str, file_type: FileType) -> Self {
25        Self {
26            source: ResolvedInputSource::String(Cow::Owned(s.to_owned())),
27            file_type,
28            content: s.to_owned(),
29        }
30    }
31
32    /// Create an instance of `InputContent` from an input string
33    #[must_use]
34    pub fn from_str<S: Into<Cow<'static, str>>>(s: S, file_type: FileType) -> Self {
35        let cow = s.into();
36        Self {
37            source: ResolvedInputSource::String(cow.clone()),
38            file_type,
39            content: cow.into_owned(),
40        }
41    }
42}