Skip to main content

tomboy_toml_dom/
lib.rs

1//! For those who are struggling with Rust's cool syntax, our goal is to provide a TOML parser that's as easy as pointing to a menu and eating fast food.  
2//! Rustのイケてる構文に難儀している人のために、メニューを指差してファーストフードを食べるぐらい簡単な操作のTOMLパーサーを提供することを目標とします。  
3
4// Publish:
5//
6// (1) `cargo test`
7// (2a) `cargo run --example example`
8// (2b) `cargo run --example example-tail-comment`
9// (2c) `cargo run --example toml-io-en-a-quick-tour-of-toml-v1-0-0rc3`
10// (2d) `cargo run --example toml-io-en-v1-0-0rc3-full-speck`
11// (3) Open auto-generated log file. I check it.
12// (4) Remove the log file.
13// (5) Update `README.md`.
14// (6) Version up on Cargo.toml.
15// (7) `cargo doc --open`
16// (8) Comit to Git-hub.
17// (9) `cargo publish --dry-run`
18// (10) `cargo publish`
19
20// #[macro_use]
21// extern crate lazy_static;
22extern crate chrono;
23extern crate num_traits;
24extern crate rand;
25
26pub mod model;
27mod parser;
28mod util;
29
30use crate::model::layer310::TomlDocument;
31use crate::parser::{
32    phase100::lexical_parser::LexicalParser,
33    phase200::{layer210::PResult, layer310::DocumentP},
34};
35use casual_logger::{ArrayOfTable, Log, Table};
36use std::convert::TryInto;
37use std::fs::File;
38use std::io::{BufRead, BufReader};
39
40/// TOML.  
41/// トムル。  
42pub struct Toml {}
43impl Toml {
44    /// Line scan.
45    /// 行走査。
46    pub fn from_file(path: &str) -> TomlDocument {
47        let mut error_tables = Vec::<Table>::new();
48        let mut output_document = TomlDocument::default();
49        match File::open(path) {
50            Ok(file) => {
51                let mut document_p = DocumentP::default();
52                for (i, line) in BufReader::new(file).lines().enumerate() {
53                    let row_number = i + 1;
54                    let line = match line {
55                        Ok(line) => line,
56                        Err(why) => panic!(Log::fatal(&format!("{}", why))),
57                    };
58                    // Log::trace(&format!("from_file/line=|{}|", line));
59                    let mut lexical_p = LexicalParser::new(row_number);
60                    lexical_p.parse_line(&line);
61
62                    match document_p.scan_line(&lexical_p.product(), &mut output_document) {
63                        PResult::End => {} // Ignored it.
64                        PResult::Err(table) => {
65                            error_tables.push(
66                                Table::default()
67                                    .str("via", "lib.rs.65.")
68                                    .int(
69                                        "row_number",
70                                        if let Ok(n) = row_number.try_into() {
71                                            n
72                                        } else {
73                                            -1
74                                        },
75                                    )
76                                    .str("line", &format!("{}", line))
77                                    .str("token_line", &format!("{}", lexical_p))
78                                    .sub_t("table", &table)
79                                    .sub_t("document_p", &document_p.log())
80                                    .clone(),
81                            );
82                        }
83                        PResult::Ongoing => {} // Ignored it.
84                    }
85                }
86            }
87            Err(why) => panic!("{}", why),
88        }
89
90        if !error_tables.is_empty() {
91            let mut error_aot = ArrayOfTable::default();
92            for err_tbl in error_tables {
93                error_aot.table(&err_tbl);
94            }
95            Log::error_t(
96                "List if error exists.",
97                Table::default().sub_aot("error_aot", &error_aot),
98            );
99        }
100
101        output_document
102    }
103}