// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// TODO:
//
// - Format functions.
// - Support hexadecimal floating point number.
// - Implements Eisel-Lemire algorithm to speed up floating point parsing.

///|
/// Trait for parsing values from textual input.
pub(open) trait FromStr {
  #as_free_fn
  fn from_str(StringView) -> Self raise
}

///|
pub impl FromStr for Bool with fn from_str(str) {
  parse_bool(str)
}

///|
pub impl FromStr for Int with fn from_str(str) {
  parse_int(str)
}

///|
pub impl FromStr for Int64 with fn from_str(str) {
  parse_int64(str)
}

///|
pub impl FromStr for UInt with fn from_str(str) {
  parse_uint(str)
}

///|
pub impl FromStr for UInt64 with fn from_str(str) {
  parse_uint64(str)
}

///|
pub impl FromStr for Double with fn from_str(str) {
  parse_double(str)
}

///|
pub impl FromStr for @bigint.BigInt with fn from_str(str) {
  parse_bigint(str)
}

///|
test "parse" {
  let b : Bool = from_str("true")
  inspect(b, content="true")
  let i : Int = from_str("12345")
  inspect(i, content="12345")
  let i64 : Int64 = from_str("9223372036854775807")
  @test.assert_eq(i64, 9223372036854775807L)
  let ui : UInt = from_str("4294967295")
  inspect(ui, content="4294967295")
  let ui64 : UInt64 = from_str("18446744073709551615")
  @test.assert_eq(ui64, 18446744073709551615UL)
  let d : Double = from_str("1234.56789")
  @test.assert_eq(d, 1234.56789)
  let bigint : @bigint.BigInt = from_str("123456789012345678901234567890")
  inspect(bigint, content="123456789012345678901234567890")
}