-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathlib.rs
More file actions
116 lines (98 loc) · 2.89 KB
/
lib.rs
File metadata and controls
116 lines (98 loc) · 2.89 KB
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
#![deny(warnings, missing_copy_implementations)]
#![allow(clippy::upper_case_acronyms, clippy::enum_variant_names)]
use std::io::{Cursor, Write};
use std::str;
#[cfg(all(feature = "use_jemalloc", not(target_env = "msvc")))]
#[global_allocator]
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
pub type RawStatus = i64;
mod comment_block;
mod delimiters;
mod file_comments;
mod format_prism;
mod heredoc_string;
mod intermediary;
mod line_metadata;
mod line_tokens;
mod parser_state;
mod render_queue_writer;
mod render_targets;
mod string_escape;
mod types;
mod util;
use file_comments::FileComments;
use parser_state::ParserState;
#[cfg(debug_assertions)]
use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TermLogger, TerminalMode};
#[derive(Debug)]
pub enum RichFormatError {
SyntaxError,
IOError(std::io::Error),
}
impl RichFormatError {
pub fn as_exit_code(&self) -> i32 {
self.as_format_error() as i32
}
fn as_format_error(&self) -> FormatError {
match self {
RichFormatError::SyntaxError => FormatError::SyntaxError,
RichFormatError::IOError(_) => FormatError::IOError,
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum FormatError {
OK = 0,
SyntaxError = 1,
RipperParseFailure = 2,
IOError = 3,
OtherRubyError = 4,
// Diffs are only necessary in --check mode
DiffDetected = 5,
}
pub fn format_buffer(buf: &str) -> Result<Vec<u8>, RichFormatError> {
let out_data = vec![];
let mut output = Cursor::new(out_data);
let parse_result = ruby_prism::parse(buf.as_bytes());
if parse_result.errors().next().is_some() {
return Err(RichFormatError::SyntaxError);
}
let end_data = parse_result.data_loc();
toplevel_format_program_with_prism(
&mut output,
parse_result.node(),
parse_result.comments(),
buf.as_bytes(),
end_data,
)?;
output.flush().expect("flushing to a vec should never fail");
Ok(output.into_inner())
}
pub fn toplevel_format_program_with_prism<W: Write>(
writer: &mut W,
tree: ruby_prism::Node,
comments: ruby_prism::Comments,
source: &[u8],
data: Option<ruby_prism::Location>,
) -> Result<(), RichFormatError> {
let mut ps = ParserState::new(FileComments::from_prism_comments(comments, source));
ps.flush_start_of_file_comments();
format_prism::format_program(&mut ps, tree.as_program_node().unwrap(), data);
ps.write(writer).map_err(RichFormatError::IOError)?;
writer.flush().map_err(RichFormatError::IOError)?;
Ok(())
}
pub fn init_logger() {
#[cfg(debug_assertions)]
{
TermLogger::init(
LevelFilter::Debug,
ConfigBuilder::new()
.set_time_level(LevelFilter::Off)
.build(),
TerminalMode::Stderr,
ColorChoice::Auto,
)
.expect("making a term logger");
}
}