Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
target
_build
.mooncakes/
.DS_Store
133 changes: 76 additions & 57 deletions README.mbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
```

---
Expand All @@ -92,48 +98,61 @@ 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)")
}
```

---


### **🛠️ 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
Expand Down
137 changes: 78 additions & 59 deletions README_zh_CN.mbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
```

---
Expand All @@ -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",
)
}
```

## 📜 许可证
Expand Down
6 changes: 3 additions & 3 deletions parser.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -212,15 +212,15 @@ 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)
self.ctx.key = key
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
Expand Down