Skip to main content

gengo/
builder.rs

1use super::Gengo;
2use super::binary::Binary;
3use super::documentation::Documentation;
4use super::generated::Generated;
5use super::vendored::Vendored;
6
7use crate::file_source::FileSource;
8use std::error::Error as ErrorTrait;
9
10/// Builds a new `Gengo` instance.
11pub struct Builder<FS: for<'fs> FileSource<'fs>> {
12    file_source: FS,
13    read_limit: Option<usize>,
14}
15
16impl<FS: for<'fs> FileSource<'fs>> Builder<FS> {
17    pub const DEFAULT_READ_LIMIT: usize = 1 << 20;
18
19    pub fn new(file_source: FS) -> Self {
20        Self {
21            file_source,
22            read_limit: None,
23        }
24    }
25
26    /// Sets the limit for how many bytes should be read from each file for
27    /// heuristic analysis. If this is not set, `DEFAULT_READ_LIMIT` will be
28    /// used.
29    pub fn read_limit(mut self, read_limit: usize) -> Self {
30        self.read_limit = Some(read_limit);
31        self
32    }
33
34    pub fn build(self) -> Result<Gengo<FS>, Box<dyn ErrorTrait>> {
35        let file_source = self.file_source;
36        let read_limit = self.read_limit.unwrap_or(Self::DEFAULT_READ_LIMIT);
37        let binary = Binary::new(read_limit);
38        let documentation = Documentation::new();
39        let generated = Generated::new();
40        let vendored = Vendored::new();
41        Ok(Gengo {
42            file_source,
43            read_limit,
44            binary,
45            documentation,
46            generated,
47            vendored,
48        })
49    }
50}