28 lines
1 KiB
GDScript
28 lines
1 KiB
GDScript
class_name Convert
|
|
|
|
class FromString:
|
|
static func _invalid_type(value: String, type: String) -> Error:
|
|
return Error.new('"%s" is not a valid %s')
|
|
|
|
static func to_int(value: String) -> Result:
|
|
if value.is_valid_int():
|
|
return Result.ok(value.to_int())
|
|
else: return Result.err(_invalid_type(value, 'int'))
|
|
|
|
static func to_float(value: String) -> Result:
|
|
if value.is_valid_float():
|
|
return Result.ok(value.to_float())
|
|
else: return Result.err(_invalid_type(value, 'float'))
|
|
|
|
static func to_bool(value: String) -> Result:
|
|
if value == 'true': return Result.ok(true)
|
|
elif value == 'false': return Result.ok(false)
|
|
else: return Result.err(_invalid_type(value, 'bool'))
|
|
|
|
static func binary_to_int(value: String) -> int:
|
|
return value.bin_to_int()
|
|
|
|
static func hex_to_int(value: String) -> Result:
|
|
if value.is_valid_hex_number():
|
|
return Result.ok(value.hex_to_int())
|
|
else: return Result.err(_invalid_type(value, 'hex string'))
|