Skip to content

Commit aef294f

Browse files
fix: string indexing warnings and type conversion issues (#3)
* Fix all deprecation warnings and syntax lint issues - Replace deprecated @hashmap.T with @hashmap.HashMap (8 warnings) - Replace deprecated is_empty() with is None pattern (6 warnings) - Replace deprecated substring with str[:] syntax (1 warning) - Fix duplicate test name (1 warning) - Fix syntax lint warning by rewriting match (try? expr) to try-catch pattern (1 warning) All changes are minimal and focused on fixing deprecation warnings. moon check now passes with 0 warnings, and all 70 tests pass. Co-authored-by: openhands <openhands@all-hands.dev> * AI work on: Try to eliminate all the warnings with minimal changes possible. The bottom line is that moon check works. It would be better if moon test works. The package may not need fix. In this case, do nothing. One major change is that the string[i] used to return Int, but now it needs to return UInt16. Try to fix it by using the correct type conversion. * fix: make the code more idomatic --------- Co-authored-by: openhands <openhands@all-hands.dev>
1 parent b111869 commit aef294f

4 files changed

Lines changed: 69 additions & 32 deletions

File tree

src/ini_file.mbt

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ priv struct Value(String) derive(Show)
1212

1313
///|
1414
struct IniFile {
15-
sections : @hashmap.T[Section, @hashmap.T[Key, Value]]
15+
sections : @hashmap.HashMap[Section, @hashmap.HashMap[Key, Value]]
1616
// default not case sensitive
1717
is_case_sensitive : Bool
1818
}
1919

2020
///|
21-
pub fn IniFile::new(is_case_sensitive~ : Bool = false) -> IniFile {
21+
pub fn IniFile::new(is_case_sensitive? : Bool = false) -> IniFile {
2222
IniFile::{ sections: @hashmap.new(), is_case_sensitive }
2323
}
2424

2525
///|
2626
fn IniFile::new_with_sections(
27-
sections : @hashmap.T[Section, @hashmap.T[Key, Value]],
28-
is_case_sensitive~ : Bool = false,
27+
sections : @hashmap.HashMap[Section, @hashmap.HashMap[Key, Value]],
28+
is_case_sensitive? : Bool = false,
2929
) -> IniFile {
3030
IniFile::{ sections, is_case_sensitive }
3131
}
@@ -39,10 +39,11 @@ fn IniFile::lower_if_needed(self : IniFile, s : String) -> String {
3939
}
4040
}
4141

42-
///| get is a alias for get_string
42+
///|
43+
/// get is a alias for get_string
4344
pub fn IniFile::get(
4445
self : IniFile,
45-
section~ : String = "",
46+
section? : String = "",
4647
key : String,
4748
) -> String? {
4849
self.get_string(section~, key)
@@ -51,7 +52,7 @@ pub fn IniFile::get(
5152
///|
5253
pub fn IniFile::get_string(
5354
self : IniFile,
54-
section~ : String = "",
55+
section? : String = "",
5556
key : String,
5657
) -> String? {
5758
let section_map = self.get_section(
@@ -61,10 +62,10 @@ pub fn IniFile::get_string(
6162
},
6263
create_if_not_exists=false,
6364
)
64-
guard not(section_map.is_empty()) else { None }
65+
guard section_map is Some(_) else { None }
6566
let section_map = section_map.unwrap()
6667
let value = section_map.get(self.lower_if_needed(key))
67-
guard not(value.is_empty()) else { None }
68+
guard value is Some(_) else { None }
6869
let value = value.unwrap()
6970
let Value(v) = value
7071
Some(v)
@@ -73,11 +74,11 @@ pub fn IniFile::get_string(
7374
///|
7475
pub fn IniFile::get_bool(
7576
self : IniFile,
76-
section~ : String = "",
77+
section? : String = "",
7778
key : String,
7879
) -> Bool? {
7980
let value = self.get_string(section~, key)
80-
guard not(value.is_empty()) else { None }
81+
guard value is Some(_) else { None }
8182
let value = value.unwrap()
8283
match value.to_lower() {
8384
"true" | "yes" | "on" => Some(true)
@@ -92,8 +93,8 @@ pub fn IniFile::get_bool(
9293
fn IniFile::get_section(
9394
self : IniFile,
9495
section : Section,
95-
create_if_not_exists~ : Bool = false,
96-
) -> @hashmap.T[Key, Value]? {
96+
create_if_not_exists? : Bool = false,
97+
) -> @hashmap.HashMap[Key, Value]? {
9798
let section_map = self.sections.get(section)
9899
match section_map {
99100
None =>
@@ -112,15 +113,15 @@ fn IniFile::get_section(
112113
fn IniFile::get_section_or_create(
113114
self : IniFile,
114115
section : Section,
115-
) -> @hashmap.T[Key, Value] {
116+
) -> @hashmap.HashMap[Key, Value] {
116117
let section_map = self.get_section(section, create_if_not_exists=true)
117118
section_map.unwrap()
118119
}
119120

120121
///|
121122
pub fn IniFile::set(
122123
self : IniFile,
123-
section~ : String = "",
124+
section? : String = "",
124125
key : String,
125126
value : String,
126127
) -> Unit {
@@ -133,7 +134,8 @@ pub fn IniFile::set(
133134
section_map.set(self.lower_if_needed(key), Value(value))
134135
}
135136

136-
///| Converts the IniFile to a standard INI format string
137+
///|
138+
/// Converts the IniFile to a standard INI format string
137139
pub fn IniFile::to_string(self : IniFile) -> String {
138140
fn escape_char(c : Char) -> String {
139141
match c {
@@ -176,7 +178,7 @@ pub fn IniFile::to_string(self : IniFile) -> String {
176178
let buf = StringBuilder::new()
177179
// Global section first
178180
let global_section = self.sections.get(Section::Global)
179-
if not(global_section.is_empty()) {
181+
if global_section is Some(_) {
180182
global_section
181183
.unwrap()
182184
.iter()

src/parser.mbt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub suberror IniParseError {
1111
///|
1212
priv struct ParseContext {
1313
mut current_section : Section
14-
sections : @hashmap.T[Section, @hashmap.T[Key, Value]]
14+
sections : @hashmap.HashMap[Section, @hashmap.HashMap[Key, Value]]
1515
mut line : Int
1616
mut col : Int
1717
mut key : String
@@ -66,7 +66,7 @@ fn IniParseState::lower_if_needed(self : IniParseState, s : String) -> String {
6666
}
6767

6868
///|
69-
pub fn IniParseState::new(is_case_sensitive~ : Bool = false) -> IniParseState {
69+
pub fn IniParseState::new(is_case_sensitive? : Bool = false) -> IniParseState {
7070
IniParseState::{
7171
ctx: ParseContext::{
7272
current_section: Section::Global,
@@ -86,7 +86,7 @@ pub fn IniParseState::new(is_case_sensitive~ : Bool = false) -> IniParseState {
8686
///|
8787
pub fn parse(
8888
str : String,
89-
is_case_sensitive~ : Bool = false,
89+
is_case_sensitive? : Bool = false,
9090
) -> IniFile raise IniParseError {
9191
IniParseState::new(is_case_sensitive~).parse(str).finish()
9292
}
@@ -405,11 +405,11 @@ fn IniParseState::commit_value(
405405
self : IniParseState,
406406
// If true, drop the last space in the value
407407
// this is used to handle the case when a comment is at the end of the line
408-
drop_last_one_space~ : Bool = false,
408+
drop_last_one_space? : Bool = false,
409409
) -> IniParseState raise IniParseError {
410410
let mut value = self.ctx.buffer.to_string()
411-
if drop_last_one_space && value.rev_iter().nth(0).unwrap().is_whitespace() {
412-
value = value.substring(end=value.length() - 1)
411+
if drop_last_one_space && value is [.. rest, ch] && ch.is_whitespace() {
412+
value = rest.to_string()
413413
}
414414
let section = self.ctx.sections.get(self.ctx.current_section).unwrap()
415415
let key = self.ctx.key
@@ -500,7 +500,7 @@ test "IniParseState::process_char/global_section_edge_cases" {
500500
}
501501

502502
///|
503-
test "panic IniParseState::process_char/unclosed_section" {
503+
test "panic IniParseState::process_char/unclosed_section_newline" {
504504
let state = IniParseState::new()
505505
ignore(state.process_char('['))
506506
ignore(state.process_char('\n'))
@@ -649,7 +649,7 @@ test "IniParseState::handle_key_char/valueless_key" {
649649
inspect(state.ctx.key, content="key")
650650
inspect(state.phase, content="Start")
651651
let section = state.ctx.sections.get(state.ctx.current_section)
652-
inspect(section.is_empty(), content="false")
652+
inspect(section is Some(_), content="true")
653653
let value = section.unwrap().get("key")
654654
inspect(value, content="Some(Value(\"\"))")
655655
}
@@ -732,10 +732,10 @@ test "IniParseState::commit_value/basic" {
732732
inspect(state.ctx.buffer.is_empty(), content="true")
733733
// Verify the section exists
734734
let section = state.ctx.sections.get(Section::NamedSection("section"))
735-
inspect(section.is_empty(), content="false")
735+
inspect(section is Some(_), content="true")
736736
// Verify the key exists in the section
737737
let value = section.unwrap().get("key")
738-
inspect(value.is_empty(), content="false")
738+
inspect(value is Some(_), content="true")
739739
}
740740

741741
///|
@@ -752,8 +752,8 @@ test "IniParseState::commit_value/empty_value" {
752752
let state = state.commit_value()
753753
// Verify the section exists
754754
let section = state.ctx.sections.get(Section::NamedSection("section"))
755-
inspect(section.is_empty(), content="false")
755+
inspect(section is Some(_), content="true")
756756
// Verify the key exists in the section with empty value
757757
let value = section.unwrap().get("key")
758-
inspect(value.is_empty(), content="false")
758+
inspect(value is Some(_), content="true")
759759
}

src/parser_test.mbt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ test "parse/error_value_without_section_or_key" {
128128
// This case is tricky because the parser might interpret 'value' as a key
129129
// Let's test assignment without a preceding key
130130
let input = "[section]\n=value"
131-
match (try? parse(input)) {
132-
Ok(_) => assert_true(false)
133-
Err(_) => assert_true(true)
131+
try parse(input) catch {
132+
_ => assert_true(true)
133+
} noraise {
134+
_ => assert_true(false)
134135
}
135136
// The ValueWithoutSection error is harder to trigger directly with current logic,
136137
// as '=' is required to start reading a value, which implies a key was being read.

src/pkg.generated.mbti

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Generated using `moon info`, DON'T EDIT IT
2+
package "ShellWen/sw_ini"
3+
4+
// Values
5+
pub fn parse(String, is_case_sensitive? : Bool) -> IniFile raise IniParseError
6+
7+
// Errors
8+
pub suberror IniParseError {
9+
UnexpectedEqualSign(line~ : Int, col~ : Int)
10+
EmptySection(line~ : Int, col~ : Int)
11+
UnclosedSection(line~ : Int, col~ : Int)
12+
ValueWithoutSection(line~ : Int, col~ : Int)
13+
QuoteMismatch(line~ : Int, col~ : Int)
14+
QuoteNotClosed(line~ : Int, col~ : Int)
15+
}
16+
17+
// Types and methods
18+
type IniFile
19+
pub fn IniFile::get(Self, section? : String, String) -> String?
20+
pub fn IniFile::get_bool(Self, section? : String, String) -> Bool?
21+
pub fn IniFile::get_string(Self, section? : String, String) -> String?
22+
pub fn IniFile::new(is_case_sensitive? : Bool) -> Self
23+
pub fn IniFile::set(Self, section? : String, String, String) -> Unit
24+
pub fn IniFile::to_string(Self) -> String
25+
26+
type IniParseState
27+
pub fn IniParseState::finish(Self) -> IniFile raise IniParseError
28+
pub fn IniParseState::new(is_case_sensitive? : Bool) -> Self
29+
pub fn IniParseState::parse(Self, String) -> Self raise IniParseError
30+
31+
// Type aliases
32+
33+
// Traits
34+

0 commit comments

Comments
 (0)