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.
Rustのイケてる構文に難儀している人のために、メニューを指差してファーストフードを食べるぐらい簡単な操作のTOMLパーサーを提供することを目標とします。
Unstable version. It's a tryal and error process. Specifications will change.
不安定版。 試行錯誤中です。 仕様はコロコロ変わるでしょう。
Tomboy is a pun.
トムボーイ(おてんば娘)は語呂合わせです。
References:
Take a look at the repository.
リポジトリを見てください。
shell
cargo run --example comment
cargo run --example example
cargo run --example example-tail-comment
cargo run --example inline_table
cargo run --example main
cargo run --example mix_array
cargo run --example spot
cargo run --example table
cargo run --example toml-io-en-a-quick-tour-of-toml-v1-0-0rc3
cargo run --example toml-io-en-v1-0-0rc3-full-speck
The specifications will gradually solidify.
仕様は少しずつ固めていきます。
You can think that you can't do anything that isn't written here.
ここに書かれていないことは何もできないと思ってもらって構いません。
``rust
//! An exemplary program.
//! 模範的なプログラム。
//!
//!
cargo run --example example`
extern crate tomboytomldom;
use chrono::{ prelude::{DateTime, Utc}, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime, }; use tomboytomldom::Toml;
fn main() { // Read a toml. // Toml読取。 let doc = Toml::from_file("./resource/example.toml");
// Read a number.
// 数値読取。
assert!(doc.contains_key("age"));
assert_eq!(doc.get_i128_by_key("age"), Some(40));
assert_eq!(doc.get_isize_by_key("age"), Some(40));
assert_eq!(doc.get_u128_by_key("age"), Some(40));
assert_eq!(doc.get_usize_by_key("age"), Some(40));
assert_eq!(doc.get_f64_by_key("weight"), Some(93.5));
assert_eq!(doc.get_i128_by_key("i32_max"), Some(2147483647));
assert_eq!(doc.get_i128_by_key("i32_min"), Some(-2147483648));
assert_eq!(
doc.get_i128_by_key("i128_max"),
Some(170_141_183_460_469_231_731_687_303_715_884_105_727)
);
assert_eq!(
doc.get_i128_by_key("i128_min"),
Some(-170_141_183_460_469_231_731_687_303_715_884_105_728)
);
assert_eq!(
doc.get_u128_by_key("u128_max"),
Some(340_282_366_920_938_463_463_374_607_431_768_211_455)
);
assert_eq!(doc.get_i128_by_key("hex1"), Some(0xDEADBEEF));
assert_eq!(doc.get_i128_by_key("hex2"), Some(0xdeadbeef));
assert_eq!(doc.get_i128_by_key("hex3"), Some(0xdead_beef));
assert_eq!(doc.get_i128_by_key("oct1"), Some(0o01234567));
assert_eq!(doc.get_i128_by_key("oct2"), Some(0o755));
assert_eq!(doc.get_i128_by_key("bin1"), Some(0b11010110));
assert_eq!(doc.get_f64_by_key("float1"), Some(1.0));
assert_eq!(doc.get_f64_by_key("float2"), Some(3.1415));
assert_eq!(doc.get_f64_by_key("float3"), Some(-0.01));
assert_eq!(doc.get_f64_by_key("float4"), Some(5e+22));
assert_eq!(doc.get_f64_by_key("float5"), Some(1e06));
assert_eq!(doc.get_f64_by_key("float6"), Some(-2E-2));
assert_eq!(doc.get_f64_by_key("float7"), Some(6.626e-34));
assert_eq!(doc.get_f64_by_key("float8"), Some(224_617.445_991_228));
assert_eq!(doc.get_f64_by_key("infinite1"), Some(f64::INFINITY));
assert_eq!(doc.get_f64_by_key("infinite2"), Some(f64::INFINITY));
assert_eq!(doc.get_f64_by_key("infinite3"), Some(-f64::INFINITY));
assert!(if let Some(n) = doc.get_f64_by_key("not1") {
n.is_nan() && n.is_sign_positive()
} else {
false
});
assert!(if let Some(n) = doc.get_f64_by_key("not2") {
n.is_nan() && n.is_sign_positive()
} else {
false
});
assert!(if let Some(n) = doc.get_f64_by_key("not3") {
n.is_nan() && n.is_sign_negative()
} else {
false
});
// WIP. Read a string.
// 作業中。 文字列読取。
assert_eq!(doc.get_string_by_key("apple"), Some("pie".to_string()));
assert_eq!(
doc.get_string_by_key("basic_string_letter"),
Some("Hello, world!!".to_string())
);
assert_eq!(
doc.get_string_by_key("basic_string_empty"),
Some("".to_string())
);
assert_eq!(
doc.get_string_by_key("basic_string_escape_backslash"),
Some("\\".to_string())
);
assert_eq!(
doc.get_string_by_key("basic_string_escape_double_quotation"),
Some("\"".to_string())
);
assert_eq!(
doc.get_string_by_key("basic_string_punctuation"),
Some("., ={}[]'\"\\!?".to_string())
);
assert_eq!(
doc.get_string_by_key("basic_string_tab"),
Some("a\tb".to_string())
);
assert_eq!(
doc.get_string_by_key("multiline_basic_string_letter"),
Some(
"Hello,
world!!" .to_string() ) );
assert_eq!(
doc.get_string_by_key("multiline_basic_string_punctuation"),
Some(
"., ={}[]\"'\"\"\\
!?" .tostring() ) ); asserteq!( doc.getstringbykey("multilinebasicstringtrimstart"), Some("The quick brown fox jumps over the lazy dog.".tostring()) ); asserteq!( doc.getstringbykey("multilinebasicstringescapedoublequotation"), Some( " \ " .tostring() ) ); /* // Fixed. println!( "debug|multilinebasicstringtab|{}", doc.getdebugstringbykey("multilinebasicstringtab") ); */ asserteq!( doc.getstringbykey("multilinebasicstringtab"), Some( " a\tb " .tostring() ) );
assert_eq!(
doc.get_string_by_key("literal_string_empty"),
Some("".to_string())
);
assert_eq!(
doc.get_string_by_key("literal_string_letter"),
Some("Hello, world!!".to_string())
);
assert_eq!(
doc.get_string_by_key("literal_string_punctuation"),
Some("., ={}[]\"\\!?".to_string())
);
assert_eq!(
doc.get_string_by_key("multiline_literal_string_letter"),
Some(
"Hello,
world!!" .tostring() ) ); asserteq!( doc.getstringbykey("multilineliteralstringpunctuation"), Some( "., ={}[]'\"\ !?" .tostring() ) ); asserteq!( doc.getstringbykey("multilineliteralstringfirstnewlineistrimmed"), Some( "The first newline is trimmed in raw strings. All other whitespace is preserved. " .tostring() ) );
// Read a boolean.
// 論理値読取。
assert_eq!(doc.get_bool_by_key("adult"), Some(true));
assert_eq!(doc.get_bool_by_key("student"), Some(false));
// DateTime.
// 日付と時刻。
assert_eq!(
doc.get_datetime_utc_by_key("dob"),
Some(
"1979-05-27T07:32:00-08:00"
.parse::<DateTime<Utc>>()
.unwrap()
)
);
assert_eq!(
doc.get_datetime_utc_by_key("odt1"),
Some("1979-05-27T07:32:00Z".parse::<DateTime<Utc>>().unwrap())
);
assert_eq!(
doc.get_datetime_fixed_offset_by_key("odt2"),
Some(
"1979-05-27T00:32:00-07:00"
.parse::<DateTime<FixedOffset>>()
.unwrap()
)
);
assert_eq!(
doc.get_datetime_fixed_offset_by_key("odt3"),
Some(
"1979-05-27T00:32:00.999999-07:00"
.parse::<DateTime<FixedOffset>>()
.unwrap()
)
);
// TODO Local datetime
assert_eq!(
// "1979-05-27T07:32:00". Toml の独自書式か。該当するフォーマット定義見つからず。
doc.get_naive_datetime_by_key("ldt1"),
Some(
match NaiveDateTime::parse_from_str("1979-05-27T07:32:00", "%Y-%m-%dT%H:%M:%S") {
Ok(n) => n,
Err(why) => panic!("{}", why),
}
)
);
assert_eq!(
// "1979-05-27T00:32:00.999999".
doc.get_naive_datetime_by_key("ldt2"),
Some(
NaiveDateTime::parse_from_str("1979-05-27T00:32:00.999999", "%Y-%m-%dT%H:%M:%S%.6f")
.unwrap()
)
);
assert_eq!(
// "1979-05-27".
doc.get_naive_date_by_key("ld1"),
Some(match NaiveDate::parse_from_str("1979-05-27", "%Y-%m-%d") {
Ok(n) => n,
Err(why) => panic!("{}", why),
})
);
assert_eq!(
doc.get_naive_time_by_key("lt1"),
Some(NaiveTime::parse_from_str("07:32:00", "%H:%M:%S").unwrap())
);
assert_eq!(
doc.get_naive_time_by_key("lt2"),
Some(NaiveTime::parse_from_str("00:32:00.999999", "%H:%M:%S%.6f").unwrap())
);
} ```
0b
- binary.0o
- octal.0x
- hexadecimal._
- separators..
- point. Example: 3.14
._
- separators.inf
- Positive infinity.+inf
- Positive infinity.-inf
- Negative infinity.nan
- Not a number. Positive.+nan
- Not a number. Positive.-nan
- Not a number. Negative."abc"
- Basic string."""abc"""
- Multi-line basic string.'abc'
- Literal string.'''abc'''
- multi-line literal string.\r
- caridge return.\n
- line feed.\t
- tab.\\
- backslash.\"
- double quotation.\u0000
- Unicode.\U00000000
- Unicode.1979-05-27
- Local date. (Naive date)1979-05-27T07:32:00
- Local datetime. (Naive datetime)1979-05-27T07:32:00Z
- UTC datetime. (Datetime Utc)1979-05-27T00:32:00.999999
- Local datetime. (Naive datetime)1979-05-27T00:32:00-07:00
- UTC datetime. (Datetime fixed offset)1979-05-27T00:32:00.999999-07:00
- UTC datetime. (Datetime fixed offset)07:32:00
- Local time. (Naive time)00:32:00.999999
- Local time. (Naive time)