diff --git a/.gitignore b/.gitignore index ec52756..edd913a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -target/ +target +_build .mooncakes/ .DS_Store diff --git a/README.mbt.md b/README.mbt.md index d7d07c7..e453cd3 100644 --- a/README.mbt.md +++ b/README.mbt.md @@ -52,38 +52,44 @@ max_connections=100 The simplest way to use `ini` is with the `parse` function: -```mbt test -let config_str = - #|[server] - #|host=localhost - #|port=3000 -let ini = @ini.parse(config_str) -let host = ini.get(section="server", "host").unwrap() -inspect(host, content="localhost") +```mbt check +///| +test { + let config_str = + #|[server] + #|host=localhost + #|port=3000 + let ini = @ini.parse(config_str) + let host = ini.get(section="server", "host").unwrap() + inspect(host, content="localhost") +} ``` ### **⚙️ Configuration Options** ini offers configuration options when parsing: -```mbt test -let content = - #|[server] - #|host=localhost - #|port=3000 - #|[Server] - #|host=remote +```mbt check +///| +test { + let content = + #|[server] + #|host=localhost + #|port=3000 + #|[Server] + #|host=remote -// Case-sensitive parsing + // Case-sensitive parsing -let ini = @ini.parse(content, is_case_sensitive=true) -inspect(ini.get(section="server", "host").unwrap(), content="localhost") -inspect(ini.get(section="Server", "host").unwrap(), content="remote") + let ini = @ini.parse(content, is_case_sensitive=true) + inspect(ini.get(section="server", "host").unwrap(), content="localhost") + inspect(ini.get(section="Server", "host").unwrap(), content="remote") -// Create an empty INI file object + // Create an empty INI file object -let ini = @ini.IniFile::new(is_case_sensitive=true) -ignore(ini) + let ini = @ini.IniFile::new(is_case_sensitive=true) + ignore(ini) +} ``` --- @@ -92,23 +98,26 @@ ignore(ini) After parsing, you can access values using various methods: -```mbt test -let content = - #|[server] - #|host=localhost - #|port=3000 - #|[feature] - #|foo=true -let ini = @ini.parse(content) -let host = ini.get(section="server", "host") -inspect( - host, - content=( - #|Some("localhost") - ), -) -let foo_enabled = ini.get_bool(section="feature", "foo") -inspect(foo_enabled, content="Some(true)") +```mbt check +///| +test { + let content = + #|[server] + #|host=localhost + #|port=3000 + #|[feature] + #|foo=true + let ini = @ini.parse(content) + let host = ini.get(section="server", "host") + inspect( + host, + content=( + #|Some("localhost") + ), + ) + let foo_enabled = ini.get_bool(section="feature", "foo") + inspect(foo_enabled, content="Some(true)") +} ``` --- @@ -116,24 +125,34 @@ inspect(foo_enabled, content="Some(true)") ### **🛠️ Full Example** -```mbt test -let content = - #|[server] - #|host=localhost - #|port=3000 - #|enabled=true - #| - #|[database] - #|url=mysql://localhost/db - -// Parse INI content -let ini = @ini.parse(content) - -// Access various values -let host = ini.get(section="server", "host").unwrap() -let port = ini.get(section="server", "port").unwrap_or("8080") -let enabled = ini.get_bool(section="server", "enabled").unwrap() -inspect(if enabled { "\{host}:\{port}" } else { "" }, content="localhost:3000") +```mbt check +///| +test { + let content = + #|[server] + #|host=localhost + #|port=3000 + #|enabled=true + #| + #|[database] + #|url=mysql://localhost/db + + // Parse INI content + let ini = @ini.parse(content) + + // Access various values + let host = ini.get(section="server", "host").unwrap() + let port = ini.get(section="server", "port").unwrap_or("8080") + let enabled = ini.get_bool(section="server", "enabled").unwrap() + inspect( + if enabled { + "\{host}:\{port}" + } else { + "" + }, + content="localhost:3000", + ) +} ``` ## 📜 License diff --git a/README_zh_CN.mbt.md b/README_zh_CN.mbt.md index 8cfee03..fe836a5 100644 --- a/README_zh_CN.mbt.md +++ b/README_zh_CN.mbt.md @@ -52,36 +52,42 @@ max_connections=100 使用 `ini` 最简单的方法是使用 `parse` 函数: -```mbt test -let config_str = - #|[server] - #|host=localhost - #|port=3000 -let ini = @ini.parse(config_str) -let host = ini.get(section="server", "host").unwrap() -inspect(host, content="localhost") +```mbt check +///| +test { + let config_str = + #|[server] + #|host=localhost + #|port=3000 + let ini = @ini.parse(config_str) + let host = ini.get(section="server", "host").unwrap() + inspect(host, content="localhost") +} ``` ### **⚙️ 配置选项** ini 在解析时提供配置选项: -```mbt test -let content = - #|[server] - #|host=localhost - #|port=3000 - #|[Server] - #|host=remote - -// 大小写敏感解析 -let ini = @ini.parse(content, is_case_sensitive=true) -inspect(ini.get(section="server", "host").unwrap(), content="localhost") -inspect(ini.get(section="Server", "host").unwrap(), content="remote") - -// 创建空的 INI 文件对象 -let ini = @ini.IniFile::new(is_case_sensitive=true) -ignore(ini) +```mbt check +///| +test { + let content = + #|[server] + #|host=localhost + #|port=3000 + #|[Server] + #|host=remote + + // 大小写敏感解析 + let ini = @ini.parse(content, is_case_sensitive=true) + inspect(ini.get(section="server", "host").unwrap(), content="localhost") + inspect(ini.get(section="Server", "host").unwrap(), content="remote") + + // 创建空的 INI 文件对象 + let ini = @ini.IniFile::new(is_case_sensitive=true) + ignore(ini) +} ``` --- @@ -90,47 +96,60 @@ ignore(ini) 解析后,您可以使用各种方法访问值: -```mbt test -let content = - #|[server] - #|host=localhost - #|port=3000 - #|[feature] - #|foo=true -let ini = @ini.parse(content) -let host = ini.get(section="server", "host") -inspect( - host, - content=( - #|Some("localhost") - ), -) -let foo_enabled = ini.get_bool(section="feature", "foo") -inspect(foo_enabled, content="Some(true)") +```mbt check +///| +test { + let content = + #|[server] + #|host=localhost + #|port=3000 + #|[feature] + #|foo=true + let ini = @ini.parse(content) + let host = ini.get(section="server", "host") + inspect( + host, + content=( + #|Some("localhost") + ), + ) + let foo_enabled = ini.get_bool(section="feature", "foo") + inspect(foo_enabled, content="Some(true)") +} ``` --- ### **🛠️ 完整示例** -```mbt test -let content = - #|[server] - #|host=localhost - #|port=3000 - #|enabled=true - #| - #|[database] - #|url=mysql://localhost/db - -// 解析 INI 内容 -let ini = @ini.parse(content) - -// 访问各种值 -let host = ini.get(section="server", "host").unwrap() -let port = ini.get(section="server", "port").unwrap_or("8080") -let enabled = ini.get_bool(section="server", "enabled").unwrap() -inspect(if enabled { "\{host}:\{port}" } else { "" }, content="localhost:3000") +```mbt check +///| +test { + let content = + #|[server] + #|host=localhost + #|port=3000 + #|enabled=true + #| + #|[database] + #|url=mysql://localhost/db + + // 解析 INI 内容 + let ini = @ini.parse(content) + + // 访问各种值 + let host = ini.get(section="server", "host").unwrap() + let port = ini.get(section="server", "port").unwrap_or("8080") + let enabled = ini.get_bool(section="server", "enabled").unwrap() + inspect( + if enabled { + "\{host}:\{port}" + } else { + "" + }, + content="localhost:3000", + ) +} ``` ## 📜 许可证 diff --git a/parser.mbt b/parser.mbt index a3a587c..c8cf36b 100644 --- a/parser.mbt +++ b/parser.mbt @@ -181,7 +181,7 @@ fn IniParseState::handle_section_char( if self.ctx.buffer.is_empty() { raise IniParseError::EmptySection(line=self.ctx.line, col=self.ctx.col) } - let section_name = self.ctx.buffer.to_string().trim_space() + let section_name = self.ctx.buffer.to_string().trim() let section_name = self.lower_if_needed(section_name.to_string()) self.ctx.current_section = Section::NamedSection(section_name) if not(self.ctx.sections.contains(self.ctx.current_section)) { @@ -212,7 +212,7 @@ fn IniParseState::handle_key_char( col=self.ctx.col, ) } - let key = self.ctx.buffer.to_string().trim_space() + let key = self.ctx.buffer.to_string().trim() let key = self.lower_if_needed(key.to_string()) self.ctx.buffer.reset() self.phase = ReadingValue(ReadingValueState::None) @@ -220,7 +220,7 @@ fn IniParseState::handle_key_char( self } '\n' | ';' | '#' => { - let key = self.ctx.buffer.to_string().trim_space() + let key = self.ctx.buffer.to_string().trim() if not(key.is_empty()) { let key = self.lower_if_needed(key.to_string()) self.ctx.key = key