-
Notifications
You must be signed in to change notification settings - Fork 8
/
lib.rs
102 lines (88 loc) · 2.76 KB
/
lib.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
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
//!
//! nufmt is a library for formatting nu.
//!
//! It does not do anything more than that, which makes it so fast.
use config::Config;
use formatting::format_inner;
use log::{debug, trace};
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
pub mod config;
pub mod formatting;
/// Reads the file and format it. After that, writes the file inplace
pub fn format_single_file(file: &PathBuf, config: &Config) {
// read the contents of the file
let contents = std::fs::read(file)
.unwrap_or_else(|_| panic!("something went wrong reading the file {}", file.display()));
// obtain the formatted file
let formatted_bytes = format_inner(&contents, config);
// compare the contents
if formatted_bytes == contents {
debug!("File is formatted correctly.")
}
// write down the file to path
let mut writer = File::create(file).unwrap();
let file_bites = formatted_bytes.as_slice();
trace!("writing {:?}", formatted_bytes);
writer
.write_all(file_bites)
.expect("something went wrong writing");
trace!("written")
}
pub fn format_string(input_string: &String, config: &Config) -> String {
let contents = input_string.as_bytes();
let formatted_bytes = format_inner(contents, config);
String::from_utf8(formatted_bytes).unwrap()
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn array_of_object() {
let nu = String::from(
"[
{
\"a\": 0
},
{},
{
\"a\": null
}
]",
);
let expected = String::from("[{\"a\":0},{},{\"a\":null}]\n");
assert_eq!(expected, format_string(&nu, &Config::default()));
}
#[test]
fn echoes_primitive() {
let nu = String::from("1.35");
let expected = String::from("1.35\n");
assert_eq!(expected, format_string(&nu, &Config::default()));
}
#[test]
fn handle_escaped_strings() {
let nu = String::from(" \"hallo\\\"\"");
let expected = String::from("\"hallo\\\"\"\n");
assert_eq!(expected, format_string(&nu, &Config::default()));
}
#[test]
#[ignore = "comments aren't a part of Spans,"]
fn ignore_comments() {
let nu = String::from("# this is a comment");
let expected = String::from("# this is a comment");
assert_eq!(expected, format_string(&nu, &Config::default()));
}
#[test]
fn ignore_whitespace_in_string() {
let nu = String::from("\" hallo \"");
let expected = String::from("\" hallo \"\n");
assert_eq!(expected, format_string(&nu, &Config::default()));
}
#[test]
fn remove_leading_whitespace() {
let nu = String::from(" 0");
let expected = String::from("0\n");
assert_eq!(expected, format_string(&nu, &Config::default()));
}
}