Skip to main content

big_code_analysis/spaces/
source.rs

1//! Inherent impl block for [`super::Source`].
2//!
3//! Split out of `spaces.rs` to keep that module focused on the public
4//! API type definitions. The block is moved verbatim; method
5//! resolution is by type, so `crate::spaces::Source`'s methods resolve
6//! unchanged.
7
8use super::*;
9
10impl<'a> Source<'a> {
11    /// Build a `Source` for `lang` and `code` with no name and no
12    /// preprocessor inputs. Chain `with_*` setters to attach a
13    /// display name or preprocessor results.
14    ///
15    /// `Source` is `#[non_exhaustive]`, so external callers cannot
16    /// use struct-literal syntax — this constructor plus the
17    /// builder setters are the supported construction path.
18    #[inline]
19    #[must_use]
20    pub fn new(lang: LANG, code: &'a [u8]) -> Self {
21        Self {
22            lang,
23            code: Cow::Borrowed(code),
24            name: None,
25            preproc_path: None,
26            preproc: None,
27        }
28    }
29
30    /// Build a `Source` that owns `code`, so [`Ast::parse`] moves the
31    /// buffer into the parser instead of copying it. Prefer this over
32    /// [`Source::new`] when you already hold an owned `Vec<u8>` (e.g. a
33    /// just-read file), which saves one full-buffer copy per parse.
34    #[inline]
35    #[must_use]
36    pub fn from_bytes(lang: LANG, code: Vec<u8>) -> Self {
37        Self {
38            lang,
39            code: Cow::Owned(code),
40            name: None,
41            preproc_path: None,
42            preproc: None,
43        }
44    }
45
46    /// Builder-style setter for `Source::name`.
47    #[inline]
48    #[must_use]
49    pub fn with_name(mut self, name: Option<String>) -> Self {
50        self.name = name;
51        self
52    }
53
54    /// Builder-style setter for `Source::preproc_path`.
55    #[inline]
56    #[must_use]
57    pub fn with_preproc_path(mut self, preproc_path: Option<&'a Path>) -> Self {
58        self.preproc_path = preproc_path;
59        self
60    }
61
62    /// Builder-style setter for `Source::preproc`.
63    #[inline]
64    #[must_use]
65    pub fn with_preproc(mut self, preproc: Option<Arc<PreprocResults>>) -> Self {
66        self.preproc = preproc;
67        self
68    }
69}