Skip to content

Commit 9b05090

Browse files
committed
wip
1 parent e070fd7 commit 9b05090

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

sol-anchor/util.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,54 @@ func PrintMapPrimitiveToString(primitiveType string, fieldName string, variableN
216216
return fmt.Sprintf("%s: map_primitive_to_string(%s),", fieldName, variableName)
217217
}
218218

219-
func ToRustPascalCase(t string) string {
220-
return t
219+
func ToRustPascalCase(input string) string {
220+
var words []string
221+
var current []rune
222+
223+
// Helper: flush current word
224+
flush := func() {
225+
if len(current) > 0 {
226+
words = append(words, string(current))
227+
current = []rune{}
228+
}
229+
}
230+
231+
runes := []rune(input)
232+
for i := 0; i < len(runes); i++ {
233+
r := runes[i]
234+
235+
if i > 0 && unicode.IsUpper(r) && (i+1 < len(runes) && !unicode.IsUpper(runes[i+1])) {
236+
flush()
237+
}
238+
239+
current = append(current, r)
240+
}
241+
flush()
242+
243+
// Convert words with logic
244+
for i := 0; i < len(words); i++ {
245+
word := words[i]
246+
if isAllUpper(word) {
247+
if i == len(words)-1 {
248+
// Acronym at the end: keep as-is
249+
continue
250+
}
251+
// Acronym in the middle: capitalize only first letter
252+
words[i] = strings.ToUpper(string(word[0])) + strings.ToLower(word[1:])
253+
}
254+
// otherwise, leave it as-is (already PascalCase)
255+
}
256+
257+
return strings.Join(words, "")
258+
}
259+
260+
func isAllUpper(s string) bool {
261+
for _, r := range s {
262+
if !unicode.IsUpper(r) {
263+
return false
264+
}
265+
}
266+
return true
221267
}
222268

223269
func toLowerCaseCapitalized(input string) string {

sol-anchor/util_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@ package solanchor
22

33
import "testing"
44

5-
func TestToRustFriendlyPascalCase(t *testing.T) {
5+
func TestProtobufToRustName(t *testing.T) {
66
tests := []struct {
77
input string
88
expected string
99
}{
10+
{"SwapRaydiumCPVariant", "SwapRaydiumCpVariant"},
11+
{"RaydiumCP", "RaydiumCP"},
1012
{"WrappedI80F48", "WrappedI80f48"},
11-
{"MyXMLParser", "Myxmlparser"},
12-
{"ETHPriceFeed", "EthPriceFeed"},
13-
{"I80F48Wrapper", "I80f48Wrapper"},
14-
{"SimpleName", "SimpleName"},
15-
{"bigUIDHandler", "BigUidHandler"},
16-
{"multiPARTnameTEST", "Multipartnametest"},
17-
{"", ""},
18-
{"some_number_123", "SomeNumber123"},
13+
{"MyHTTPResponse", "MyHttpResponse"},
14+
{"URLParser", "UrlParser"},
15+
{"ABCTestXYZ", "AbcTestXYZ"},
16+
{"CPUUsageData", "CpuUsageData"},
17+
{"IOConfig", "IoConfig"},
18+
{"HTTP2Connection", "Http2Connection"},
19+
{"DNSInfoPacket", "DnsInfoPacket"},
1920
}
2021

2122
for _, tt := range tests {
22-
result := ToRustFriendlyPascalCase(tt.input)
23+
result := ToRustPascalCase(tt.input)
2324
if result != tt.expected {
24-
t.Errorf("ToRustFriendlyPascalCase(%q) = %q, want %q", tt.input, result, tt.expected)
25+
t.Errorf("ProtobufToRustName(%q) = %q, want %q", tt.input, result, tt.expected)
2526
}
2627
}
2728
}

0 commit comments

Comments
 (0)