Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a2b79e

Browse files
author
Paolo Tranquilli
committed
Rust: skeleton trap file emission code
1 parent 9277100 commit 2a2b79e

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

rust/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use ra_ap_load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice
88

99

1010
mod config;
11-
11+
mod trap;
1212

1313
fn main() -> anyhow::Result<()> {
1414
let cfg = config::Config::extract().context("failed to load configuration")?;

rust/src/trap.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use std::fmt::Formatter;
2+
use std::fs::File;
3+
use std::io::Write;
4+
use std::path::{PathBuf};
5+
6+
#[derive(Debug)]
7+
struct TrapLabel(u64);
8+
9+
impl std::fmt::Display for TrapLabel {
10+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
11+
write!(f, "#{:x}", self.0)
12+
}
13+
}
14+
15+
trait TrapEntry: std::fmt::Display {}
16+
17+
#[derive(Debug)]
18+
struct TrapFile {
19+
label_index: u64,
20+
file: File,
21+
}
22+
23+
impl TrapFile {
24+
pub fn new(path: &PathBuf) -> std::io::Result<TrapFile> {
25+
let file = File::create(path)?;
26+
Ok(TrapFile {
27+
label_index: 0,
28+
file: file,
29+
})
30+
}
31+
32+
pub fn insert_comment(&mut self, comment: &str) -> std::io::Result<()> {
33+
write!(self.file, "/* {comment} */\n")
34+
}
35+
36+
pub fn allocate_label(&mut self) -> std::io::Result<TrapLabel> {
37+
let ret = TrapLabel(self.label_index);
38+
write!(self.file, "{ret}=*\n")?;
39+
self.label_index += 1;
40+
Ok(ret)
41+
}
42+
43+
pub fn allocate_label_for(&mut self, key: &str) -> std::io::Result<TrapLabel> {
44+
let ret = TrapLabel(self.label_index);
45+
write!(self.file, "{ret}=\"{}\"\n", key.replace("\"", "\"\""))?;
46+
self.label_index += 1;
47+
Ok(ret)
48+
}
49+
50+
pub fn emit<T: TrapEntry>(&mut self, entry: T) -> std::io::Result<()> {
51+
write!(self.file, "{entry}\n")
52+
}
53+
}

0 commit comments

Comments
 (0)