>>103570377
>>103570387
Okay, I asked ChatGPT to translate your code into Rust. It just so happens to be 100x more readable and understandable than your line noise garbage.
// GetCommand sends a command to the specified address and returns the JSON response
func GetCommand(tp string, cmd string, parameter *string) (json.RawMessage, error) {
// Connect to the TCP server
conn, err := net.Dial("tcp", fmt.Sprintf("%s:4028", tp))
if err != nil {
return nil, fmt.Errorf("failed to connect: %w", err)
}
defer conn.Close()
// Create the command request
request := CommandRequest{
Command: cmd,
}
if parameter != nil {
request.Parameter = *parameter
}
// Marshal the request to JSON
data, err := json.Marshal(request)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
// Send the request
_, err = conn.Write(data)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
// Read the response
buffer := make([]byte, 4096)
n, err := conn.Read(buffer)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
}
// Trim null terminators and any whitespace
response := strings.TrimRight(string(buffer[:n]), "\x00\r\n\t ")
// Validate the response is valid JSON
var jsonResponse json.RawMessage
if err := json.Unmarshal([]byte(response), &jsonResponse); err != nil {
return nil, fmt.Errorf("invalid JSON response: %w", err)
}
return jsonResponse, nil
}