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
43
44
45
46
47
use std::fs::File;
use std::io::BufReader;
use std::path::{Path, PathBuf};

use anyhow::Result;

use crate::loader::GramsLoader;
use crate::parser::GramsParser;

pub struct GramsFileLoader {
    filepath: PathBuf,
}

impl GramsFileLoader {
    pub fn new<P>(filepath: P) -> Self
    where
        P: AsRef<Path>,
    {
        Self {
            filepath: PathBuf::from(filepath.as_ref()),
        }
    }
}

impl GramsLoader<File> for GramsFileLoader {
    fn parser(&self) -> Result<GramsParser<File>> {
        let reader = BufReader::new(File::open(&self.filepath)?);
        GramsParser::new(reader)
    }
}

pub struct GramsTextLoader<'a> {
    text: &'a [u8],
}

impl<'a> GramsTextLoader<'a> {
    pub const fn new(text: &'a [u8]) -> Self {
        Self { text }
    }
}

impl<'a> GramsLoader<&'a [u8]> for GramsTextLoader<'a> {
    fn parser(&self) -> Result<GramsParser<&'a [u8]>> {
        let reader = BufReader::new(self.text);
        GramsParser::new(reader)
    }
}