-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.rs
44 lines (41 loc) · 1.28 KB
/
common.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
use std::{fs::File, io::Read, path::Path};
/// return `CompilerError` whose kind is `Unimplemented`
/// - 1st arg: `debug_info: DebugInfo`
/// - 2rd arg: `msg: T` `where T: Display` // error message
///
/// Also no args provided is allowed.
#[macro_export]
macro_rules! unimplemented_err {
($debug_info: expr, $msg: expr) => {
CompileError::new($crate::error::CompileErrorKind::Unimplemented(
Some($debug_info),
format!("{}{}", $crate::meta!(), $msg),
))
};
($msg: expr) => {
CompileError::new($crate::error::CompileErrorKind::Unimplemented(
None,
format!("{} {}", $crate::meta!(), $msg),
))
};
() => {
CompileError::new($crate::error::CompileErrorKind::Unimplemented(
None,
format!("{} Not yet implemented.", $crate::meta!()),
))
};
}
#[macro_export]
macro_rules! meta {
() => {
concat!(file!(), ":", line!(), ":", column!(), ": ")
};
}
/// read file of given path as text and return it as `String`
pub fn read_file(path: &Path) -> Result<String, std::io::Error> {
let mut file = File::open(path).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
contents.push('\n');
Ok(contents)
}