Skip to content

Commit a613514

Browse files
committed
Remove use of LineColumn.
1 parent 42f34d4 commit a613514

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

macros/src/embed_python.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use proc_macro::Span;
2-
use proc_macro2::{Delimiter, Ident, Spacing, TokenStream, TokenTree, LineColumn};
2+
use proc_macro2::{Delimiter, Ident, Spacing, TokenStream, TokenTree};
33
use quote::quote_spanned;
44
use std::collections::BTreeMap;
55
use std::fmt::Write;
@@ -8,7 +8,8 @@ pub struct EmbedPython {
88
pub python: String,
99
pub variables: BTreeMap<String, Ident>,
1010
pub first_indent: Option<usize>,
11-
pub loc: LineColumn,
11+
pub line: usize,
12+
pub column: usize,
1213
pub compile_time: bool,
1314
}
1415

@@ -17,30 +18,31 @@ impl EmbedPython {
1718
Self {
1819
python: String::new(),
1920
variables: BTreeMap::new(),
20-
loc: LineColumn { line: 1, column: 0 },
21+
line: 1,
22+
column: 0,
2123
first_indent: None,
2224
compile_time: false,
2325
}
2426
}
2527

26-
fn add_whitespace(&mut self, span: Span, loc: LineColumn) -> Result<(), TokenStream> {
28+
fn add_whitespace(&mut self, span: Span, line: usize, column: usize) -> Result<(), TokenStream> {
2729
#[allow(clippy::comparison_chain)]
28-
if loc.line > self.loc.line {
29-
while loc.line > self.loc.line {
30+
if line > self.line {
31+
while line > self.line {
3032
self.python.push('\n');
31-
self.loc.line += 1;
33+
self.line += 1;
3234
}
33-
let first_indent = *self.first_indent.get_or_insert(loc.column);
34-
let indent = loc.column.checked_sub(first_indent);
35+
let first_indent = *self.first_indent.get_or_insert(column);
36+
let indent = column.checked_sub(first_indent);
3537
let indent = indent.ok_or_else(|| quote_spanned!(span.into() => compile_error!{"Invalid indentation"}))?;
3638
for _ in 0..indent {
3739
self.python.push(' ');
3840
}
39-
self.loc.column = loc.column;
40-
} else if loc.line == self.loc.line {
41-
while loc.column > self.loc.column {
41+
self.column = column;
42+
} else if line == self.line {
43+
while column > self.column {
4244
self.python.push(' ');
43-
self.loc.column += 1;
45+
self.column += 1;
4446
}
4547
}
4648

@@ -52,9 +54,7 @@ impl EmbedPython {
5254

5355
while let Some(token) = tokens.next() {
5456
let span = token.span().unwrap();
55-
let start_span = span.start();
56-
let lc = LineColumn { line: start_span.line(), column: start_span.column() };
57-
self.add_whitespace(span, lc)?;
57+
self.add_whitespace(span, span.line(), span.column())?;
5858

5959
match &token {
6060
TokenTree::Group(x) => {
@@ -65,14 +65,12 @@ impl EmbedPython {
6565
Delimiter::None => ("", ""),
6666
};
6767
self.python.push_str(start);
68-
self.loc.column += start.len();
68+
self.column += start.len();
6969
self.add(x.stream())?;
70-
let end_loc = token.span().unwrap().end();
71-
let end_col = end_loc.column().saturating_sub(end.len());
72-
let elc = LineColumn { line: end_loc.line(), column: end_col };
73-
self.add_whitespace(span, elc)?;
70+
let end_span = token.span().unwrap().end();
71+
self.add_whitespace(span, end_span.line(), end_span.column().saturating_sub(end.len()))?;
7472
self.python.push_str(end);
75-
self.loc.column += end.len();
73+
self.column += end.len();
7674
}
7775
TokenTree::Punct(x) => {
7876
if !self.compile_time && x.as_char() == '\'' && x.spacing() == Spacing::Joint {
@@ -83,34 +81,35 @@ impl EmbedPython {
8381
};
8482
let name_str = format!("_RUST_{}", name);
8583
self.python.push_str(&name_str);
86-
self.loc.column += name_str.chars().count() - 6 + 1;
84+
self.column += name_str.chars().count() - 6 + 1;
8785
self.variables.entry(name_str).or_insert(name);
8886
} else if x.as_char() == '#' && x.spacing() == Spacing::Joint {
8987
// Convert '##' to '//', because otherwise it's
9088
// impossible to use the Python operators '//' and '//='.
9189
match tokens.next() {
9290
Some(TokenTree::Punct(ref p)) if p.as_char() == '#' => {
9391
self.python.push_str("//");
94-
self.loc.column += 2;
92+
self.column += 2;
9593
}
9694
Some(TokenTree::Punct(p)) => {
9795
self.python.push(x.as_char());
9896
self.python.push(p.as_char());
99-
self.loc.column += 2;
97+
self.column += 2;
10098
}
10199
_ => {
102100
unreachable!();
103101
}
104102
}
105103
} else {
106104
self.python.push(x.as_char());
107-
self.loc.column += 1;
105+
self.column += 1;
108106
}
109107
}
110108
TokenTree::Ident(x) => {
111109
write!(&mut self.python, "{}", x).unwrap();
112110
let end_span = token.span().unwrap().end();
113-
self.loc = LineColumn { line: end_span.line(), column: end_span.column() };
111+
self.line = end_span.line();
112+
self.column = end_span.column();
114113
}
115114
TokenTree::Literal(x) => {
116115
let s = x.to_string();
@@ -124,7 +123,8 @@ impl EmbedPython {
124123
}
125124
self.python += &s;
126125
let end_span = token.span().unwrap().end();
127-
self.loc = LineColumn { line: end_span.line(), column: end_span.column() };
126+
self.line = end_span.line();
127+
self.column = end_span.column();
128128
}
129129
}
130130
}

0 commit comments

Comments
 (0)