Skip to content

Commit 04753b2

Browse files
author
Paolo Tranquilli
committed
Rust: tweak trap_key
1 parent 590a146 commit 04753b2

2 files changed

Lines changed: 53 additions & 44 deletions

File tree

rust/src/translate.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22
use std::fs;
33
use std::path::{PathBuf};
4-
use crate::trap::{KeyPart, TrapFile, TrapId, TrapLabel};
4+
use crate::trap::{TrapFile, TrapId, TrapLabel};
55
use crate::{generated, trap_key};
66
use ra_ap_hir::{Crate, Module, ModuleDef};
77
use anyhow;
@@ -74,7 +74,7 @@ impl CrateTranslator<'_> {
7474
let start = data.line_index.line_col(range.start());
7575
let end = data.line_index.line_col(range.end());
7676
return Ok(Some(self.trap.emit(generated::DbLocation {
77-
id: trap_key![data.label, format!(":{}:{}:{}:{}", start.line, start.col, end.line, end.col)],
77+
id: trap_key![data.label, ":", start.line, ":", start.col, ":", end.line, ":", end.col],
7878
file: data.label,
7979
start_line: start.line,
8080
start_column: start.col,
@@ -129,20 +129,20 @@ impl CrateTranslator<'_> {
129129
self.emit_file(self.krate.root_file(self.db))?;
130130
let mut map = HashMap::<Module, TrapLabel>::new();
131131
for module in self.krate.modules(self.db) {
132-
let mut key = Vec::<KeyPart>::new();
132+
let mut key = String::new();
133133
if let Some(parent) = module.parent(self.db) {
134134
// assumption: parent was already listed
135135
let parent_label = *map.get(&parent).unwrap();
136-
key.push(parent_label.into());
136+
key.push_str(&parent_label.as_key_part());
137137
}
138138
let def = module.definition_source(self.db);
139139
if let Some(file) = def.file_id.file_id() {
140140
if let Some(data) = self.emit_file(file.file_id())? {
141-
key.push(data.label.into());
141+
key.push_str(&data.label.as_key_part());
142142
}
143143
}
144144
if let Some(name) = module.name(self.db) {
145-
key.push(name.as_str().into());
145+
key.push_str(name.as_str());
146146
}
147147
let label = self.trap.label(TrapId::Key(key))?;
148148
map.insert(module, label);

rust/src/trap.rs

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ use std::path::{Path, PathBuf};
55
use log::{debug, trace};
66
use crate::{config, path};
77

8-
#[derive(Debug, Clone, Copy)]
8+
#[derive(Clone, Copy)]
99
pub struct TrapLabel(u64);
1010

11+
impl Debug for TrapLabel {
12+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
13+
write!(f, "TrapLabel({:x})", self.0)
14+
}
15+
}
16+
1117
impl Display for TrapLabel {
1218
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1319
write!(f, "#{:x}", self.0)
@@ -16,64 +22,59 @@ impl Display for TrapLabel {
1622

1723
//TODO: typed labels
1824

25+
impl TrapLabel {
26+
pub fn as_key_part(&self) -> String {
27+
format!("{{{}}}", self)
28+
}
29+
}
30+
1931
#[derive(Debug, Clone)]
20-
pub enum KeyPart {
32+
pub enum TrapId {
33+
Star,
34+
Key(String),
2135
Label(TrapLabel),
22-
Text(String),
2336
}
2437

25-
impl From<String> for KeyPart {
38+
impl From<String> for TrapId {
2639
fn from(value: String) -> Self {
27-
KeyPart::Text(value)
40+
TrapId::Key(value)
2841
}
2942
}
3043

31-
impl From<&str> for KeyPart {
44+
impl From<&str> for TrapId {
3245
fn from(value: &str) -> Self {
33-
KeyPart::Text(value.into())
46+
TrapId::Key(value.into())
3447
}
3548
}
3649

37-
impl From<TrapLabel> for KeyPart {
50+
impl From<TrapLabel> for TrapId {
3851
fn from(value: TrapLabel) -> Self {
39-
KeyPart::Label(value)
40-
}
41-
}
42-
43-
#[derive(Debug, Clone)]
44-
pub enum TrapId {
45-
Star,
46-
Key(Vec<KeyPart>),
47-
Label(TrapLabel),
48-
}
49-
50-
impl<T: Into<KeyPart>> From<T> for TrapId {
51-
fn from(value: T) -> Self {
52-
TrapId::Key(vec![value.into()])
52+
TrapId::Label(value)
5353
}
5454
}
5555

5656
#[macro_export]
5757
macro_rules! trap_key {
58-
($($x:expr),+ $(,)?) => (
59-
$crate::trap::TrapId::Key(vec![$($x.into()),+])
60-
);
58+
($($x:expr),+ $(,)?) => {{
59+
trait BlanketKeyPart: std::fmt::Display {
60+
fn as_key_part(&self) -> String {
61+
format!("{}", self)
62+
}
63+
}
64+
impl<T: std::fmt::Display> BlanketKeyPart for T {}
65+
let mut key = String::new();
66+
$(
67+
key.push_str(&$x.as_key_part());
68+
)*
69+
$crate::TrapId::Key(key)
70+
}};
6171
}
6272

6373
impl Display for TrapId {
6474
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
6575
match self {
6676
TrapId::Star => write!(f, "*"),
67-
TrapId::Key(k) => {
68-
f.write_str("@\"")?;
69-
for p in k {
70-
match p {
71-
KeyPart::Label(l) => write!(f, "{{{l}}}")?,
72-
KeyPart::Text(s) => f.write_str(&escaped(s))?,
73-
}
74-
}
75-
f.write_str("\"")
76-
}
77+
TrapId::Key(k) => write!(f, "@{}", quoted(k)),
7778
TrapId::Label(l) => Display::fmt(&l, f)
7879
}
7980
}
@@ -109,9 +110,17 @@ impl TrapFile {
109110
Ok(())
110111
}
111112

112-
pub fn label(&mut self, id: TrapId) -> std::io::Result<TrapLabel> {
113-
if let TrapId::Label(l) = id {
114-
return Ok(l);
113+
pub fn label(&mut self, mut id: TrapId) -> std::io::Result<TrapLabel> {
114+
match id {
115+
TrapId::Star => {}
116+
TrapId::Key(ref s) => {
117+
if s.is_empty() {
118+
id = TrapId::Star;
119+
}
120+
}
121+
TrapId::Label(l) => {
122+
return Ok(l);
123+
}
115124
}
116125
let ret = self.create_label();
117126
trace!("emit -> {}: {ret:?} = {id:?}", self.trap_name);

0 commit comments

Comments
 (0)