roan_ast/
source.rs

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::{
    fs::File,
    io::{self, BufReader, Read},
    path::PathBuf,
    str::Chars,
};
use tracing::debug;

/// A source of Roan code.
#[derive(Clone, Debug)]
pub struct Source {
    content: String,
    path: Option<PathBuf>,
}

impl Source {
    /// Creates a new `Source` from a `String`.
    pub fn from_string(string: String) -> Self {
        debug!("Creating source from string");
        Self {
            content: string,
            path: None,
        }
    }

    /// Creates a new `Source` from a byte slice.
    pub fn from_bytes<T: AsRef<[u8]> + ?Sized>(source: &T) -> Self {
        debug!("Creating source from bytes");
        Self {
            content: source.as_ref().iter().map(|&b| b as char).collect(),
            path: None,
        }
    }

    /// Creates a new `Source` from a file path.
    pub fn from_path(path: PathBuf) -> io::Result<Self> {
        debug!("Creating source from path: {:?}", path);
        let file = File::open(&path)?;
        let reader = BufReader::new(file);
        Ok(Self {
            content: reader
                .bytes()
                .filter_map(|b| b.ok().map(|b| b as char))
                .collect(),
            path: Some(path),
        })
    }

    /// Sets or updates the path of this `Source`.
    pub fn with_path(self, new_path: PathBuf) -> Self {
        Self {
            content: self.content,
            path: Some(new_path),
        }
    }

    /// Returns the content of this `Source`.
    pub fn content(&self) -> String {
        self.content.clone()
    }

    /// Returns the path associated with this `Source`, if any.
    pub fn path(&self) -> Option<PathBuf> {
        self.path.clone()
    }

    /// Returns the content of this `Source`.
    pub fn len(&self) -> usize {
        self.content.len()
    }

    /// Returns the content of this `Source` as a char iterator.
    pub fn chars(&self) -> Chars {
        self.content.chars()
    }

    /// Returns the content of this `Source` between the specified indices.
    pub fn get_between(&self, start: usize, end: usize) -> String {
        self.content[start..end].to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_source_from_string() {
        let source = Source::from_string("fn main() {}".to_string());

        assert_eq!(source.content(), "fn main() {}");
        assert_eq!(source.path(), None);
    }

    #[test]
    fn test_source_from_bytes() {
        let source = Source::from_bytes(b"fn main() {}");

        assert_eq!(source.content(), "fn main() {}");
        assert_eq!(source.path(), None);
    }

    #[test]
    fn test_source_with_path() {
        let source = Source::from_string("fn main() {}".to_string())
            .with_path(PathBuf::from("tests/test.roan"));

        assert_eq!(source.content(), "fn main() {}");
        assert_eq!(source.path(), Some(PathBuf::from("tests/test.roan")));
    }

    #[test]
    fn test_source_len() {
        let source = Source::from_string("fn main() {}".to_string());

        assert_eq!(source.len(), 12);
    }

    #[test]
    fn test_source_chars() {
        let source = Source::from_string("fn main() {}".to_string());

        assert_eq!(source.chars().collect::<String>(), "fn main() {}");
    }

    #[test]
    fn test_source_get_between() {
        let source = Source::from_string("fn main() {}".to_string());

        assert_eq!(source.get_between(3, 7), "main");
    }
}