| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#[cfg(target_arch = "wasm32")] |
| 10 |
extern crate wee_alloc; |
| 11 |
#[cfg(target_arch = "wasm32")] |
| 12 |
#[global_allocator] |
| 13 |
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; |
| 14 |
|
| 15 |
extern crate pest; |
| 16 |
#[macro_use] |
| 17 |
extern crate pest_derive; |
| 18 |
|
| 19 |
use pest::error::Error; |
| 20 |
use pest::Parser; |
| 21 |
use serde::{Deserialize, Serialize}; |
| 22 |
use std::{env, fmt, str}; |
| 23 |
use wasm_bindgen::prelude::*; |
| 24 |
|
| 25 |
#[derive(Parser)] |
| 26 |
#[grammar = "ini.pest"] |
| 27 |
struct INIParser; |
| 28 |
|
| 29 |
#[wasm_bindgen] |
| 30 |
pub fn parse_to_json(contents: &str) -> JsValue { |
| 31 |
let ast = parse(&contents).unwrap(); |
| 32 |
return serde_wasm_bindgen::to_value(&ast).unwrap(); |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
#[wasm_bindgen] |
| 37 |
pub fn version() -> String { |
| 38 |
String::from(env!("CARGO_PKG_VERSION")) |
| 39 |
} |
| 40 |
|
| 41 |
#[wasm_bindgen] |
| 42 |
#[derive(Copy, Clone)] |
| 43 |
#[repr(u32)] |
| 44 |
pub enum TokenTypes { |
| 45 |
Key, |
| 46 |
Value, |
| 47 |
Section, |
| 48 |
CommentIndicator, |
| 49 |
CommentValue, |
| 50 |
} |
| 51 |
|
| 52 |
#[wasm_bindgen] |
| 53 |
pub fn parse_to_uint32array(contents: &[u8]) -> Result<Vec<u32>, JsError> { |
| 54 |
let input = str::from_utf8(contents)?; |
| 55 |
match parse_to_vec(input) { |
| 56 |
Ok(res) => Ok(res), |
| 57 |
Err(er) => Err(JsError::from(er)), |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
pub fn parse_to_vec(contents: &str) -> Result<Vec<u32>, Error<Rule>> { |
| 76 |
let mut parsed = INIParser::parse(Rule::ini, contents)?; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
let mut results = Vec::with_capacity(300); |
| 81 |
|
| 82 |
fill_vec(parsed.next().unwrap(), &mut results); |
| 83 |
Ok(results) |
| 84 |
} |
| 85 |
|
| 86 |
fn push_token(typ: TokenTypes, pair: pest::iterators::Pair<'_, Rule>, results: &mut Vec<u32>) { |
| 87 |
let span = pair.as_span(); |
| 88 |
results.push(typ as u32); |
| 89 |
results.push(span.start() as u32); |
| 90 |
results.push(span.end() as u32); |
| 91 |
} |
| 92 |
|
| 93 |
fn fill_vec(pair: pest::iterators::Pair<'_, Rule>, results: &mut Vec<u32>) { |
| 94 |
let children = pair.into_inner().filter(|p| match p.as_rule() { |
| 95 |
Rule::EOI => false, |
| 96 |
_ => true, |
| 97 |
}); |
| 98 |
for child in children { |
| 99 |
match child.as_rule() { |
| 100 |
Rule::section => { |
| 101 |
let mut inner_rules = child.into_inner(); |
| 102 |
let mut header = inner_rules.next().unwrap().into_inner(); |
| 103 |
push_token(TokenTypes::Section, header.next().unwrap(), results); |
| 104 |
|
| 105 |
fill_vec(inner_rules.next().unwrap(), results); |
| 106 |
} |
| 107 |
Rule::pair => { |
| 108 |
let mut inner_rules = child.into_inner(); |
| 109 |
push_token(TokenTypes::Key, inner_rules.next().unwrap(), results); |
| 110 |
push_token(TokenTypes::Value, inner_rules.next().unwrap(), results); |
| 111 |
} |
| 112 |
Rule::comment => { |
| 113 |
let mut inner_rules = child.into_inner(); |
| 114 |
push_token( |
| 115 |
TokenTypes::CommentIndicator, |
| 116 |
inner_rules.next().unwrap(), |
| 117 |
results, |
| 118 |
); |
| 119 |
push_token( |
| 120 |
TokenTypes::CommentValue, |
| 121 |
inner_rules.next().unwrap(), |
| 122 |
results, |
| 123 |
); |
| 124 |
} |
| 125 |
_ => unreachable!(), |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
pub fn parse(contents: &str) -> Result<OneINIAST, Error<Rule>> { |
| 142 |
return match INIParser::parse(Rule::ini, contents) { |
| 143 |
Ok(mut pairs) => Ok(OneINIAST::new(create_body(pairs.next().unwrap()))), |
| 144 |
Err(e) => Err(e), |
| 145 |
}; |
| 146 |
} |
| 147 |
|
| 148 |
fn create_body(pair: pest::iterators::Pair<'_, Rule>) -> Vec<Item> { |
| 149 |
return pair |
| 150 |
.into_inner() |
| 151 |
.filter(|p| match p.as_rule() { |
| 152 |
Rule::EOI => false, |
| 153 |
_ => true, |
| 154 |
}) |
| 155 |
.map(|p| match p.as_rule() { |
| 156 |
Rule::section => { |
| 157 |
let mut inner_rules = p.into_inner(); |
| 158 |
let header = inner_rules.next().unwrap().into_inner().next().unwrap(); |
| 159 |
return Item::Section(Section { |
| 160 |
name: String::from(header.as_str()), |
| 161 |
body: match inner_rules.next() { |
| 162 |
Some(pair) => create_body(pair), |
| 163 |
_ => vec![], |
| 164 |
}, |
| 165 |
}); |
| 166 |
} |
| 167 |
Rule::pair => { |
| 168 |
let mut inner_rules = p.into_inner(); |
| 169 |
return Item::Pair(Pair { |
| 170 |
key: String::from(inner_rules.next().unwrap().as_str()), |
| 171 |
value: String::from(inner_rules.next().unwrap().as_str()), |
| 172 |
}); |
| 173 |
} |
| 174 |
Rule::comment => { |
| 175 |
let mut inner_rules = p.into_inner(); |
| 176 |
return Item::Comment(Comment { |
| 177 |
indicator: inner_rules.next().unwrap().as_str().chars().nth(0).unwrap(), |
| 178 |
value: String::from(inner_rules.next().unwrap().as_str()), |
| 179 |
}); |
| 180 |
} |
| 181 |
_ => unreachable!(), |
| 182 |
}) |
| 183 |
.collect(); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
#[derive(Serialize, Deserialize, Debug)] |
| 231 |
pub struct OneINIAST { |
| 232 |
|
| 233 |
pub version: String, |
| 234 |
|
| 235 |
#[serde(skip_serializing_if = "Vec::is_empty")] |
| 236 |
pub body: Vec<Item>, |
| 237 |
} |
| 238 |
|
| 239 |
impl OneINIAST { |
| 240 |
pub fn new<B: Into<Vec<Item>>>(body: B) -> Self { |
| 241 |
OneINIAST { |
| 242 |
version: String::from(env!("CARGO_PKG_VERSION")), |
| 243 |
body: body.into(), |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
impl fmt::Display for OneINIAST { |
| 249 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 250 |
let mut wrote = false; |
| 251 |
for item in &self.body { |
| 252 |
match item { |
| 253 |
Item::Section(_section) => { |
| 254 |
if wrote { |
| 255 |
writeln!(formatter)?; |
| 256 |
} |
| 257 |
} |
| 258 |
_ => (), |
| 259 |
} |
| 260 |
item.fmt(formatter)?; |
| 261 |
wrote = true; |
| 262 |
} |
| 263 |
Ok(()) |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
#[derive(Serialize, Deserialize, Debug)] |
| 290 |
#[serde(tag = "type")] |
| 291 |
pub enum Item { |
| 292 |
Comment(Comment), |
| 293 |
Pair(Pair), |
| 294 |
Section(Section), |
| 295 |
} |
| 296 |
|
| 297 |
impl fmt::Display for Item { |
| 298 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 299 |
match self { |
| 300 |
Item::Comment(comment) => comment.fmt(formatter), |
| 301 |
Item::Pair(pair) => pair.fmt(formatter), |
| 302 |
Item::Section(section) => section.fmt(formatter), |
| 303 |
}?; |
| 304 |
Ok(()) |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
#[derive(Serialize, Deserialize, Debug)] |
| 349 |
pub struct Comment { |
| 350 |
|
| 351 |
|
| 352 |
pub indicator: char, |
| 353 |
|
| 354 |
pub value: String, |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
impl fmt::Display for Comment { |
| 390 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 391 |
writeln!(formatter, "{}{}", self.indicator, self.value)?; |
| 392 |
Ok(()) |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
#[derive(Serialize, Deserialize, Debug)] |
| 409 |
pub struct Pair { |
| 410 |
|
| 411 |
pub key: String, |
| 412 |
|
| 413 |
pub value: String, |
| 414 |
} |
| 415 |
|
| 416 |
impl fmt::Display for Pair { |
| 417 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 418 |
writeln!(formatter, "{}={}", self.key, self.value)?; |
| 419 |
Ok(()) |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
#[derive(Serialize, Deserialize, Debug)] |
| 451 |
pub struct Section { |
| 452 |
|
| 453 |
pub name: String, |
| 454 |
|
| 455 |
|
| 456 |
#[serde(skip_serializing_if = "Vec::is_empty")] |
| 457 |
pub body: Vec<Item>, |
| 458 |
} |
| 459 |
|
| 460 |
impl fmt::Display for Section { |
| 461 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 462 |
writeln!(formatter, "[{}]", self.name)?; |
| 463 |
for item in &self.body { |
| 464 |
item.fmt(formatter)?; |
| 465 |
} |
| 466 |
Ok(()) |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
#[cfg(test)] |
| 471 |
mod tests { |
| 472 |
use super::*; |
| 473 |
use std::fs; |
| 474 |
|
| 475 |
#[test] |
| 476 |
fn it_works() { |
| 477 |
let contents = fs::read_to_string("tests/fixtures/config.ini").unwrap(); |
| 478 |
let ast = parse(&contents).unwrap(); |
| 479 |
assert_eq!(ast.to_string(), contents); |
| 480 |
} |
| 481 |
} |