(module main)

(import std.fs (read_text read_text_result read_text_option write_text_status write_text_result read_text_or write_text_ok read_i32_result read_i32_option read_i32_or_zero read_i32_or read_u32_result read_u32_option read_u32_or_zero read_u32_or read_i64_result read_i64_option read_i64_or_zero read_i64_or read_u64_result read_u64_option read_u64_or_zero read_u64_or read_f64_result read_f64_option read_f64_or_zero read_f64_or read_bool_result read_bool_option read_bool_or_false read_bool_or))

(fn fixture_path () -> string
  "glagol-std-import-fs-alpha.txt")

(fn fixture_text () -> string
  "std fs source search alpha")

(fn missing_fixture_path () -> string
  "glagol-std-import-fs-alpha-missing.txt")

(fn fallback_text () -> string
  "std fs source fallback alpha")

(fn i32_fixture_path () -> string
  "glagol-std-import-fs-alpha-i32.txt")

(fn i64_fixture_path () -> string
  "glagol-std-import-fs-alpha-i64.txt")

(fn u32_fixture_path () -> string
  "glagol-std-import-fs-alpha-u32.txt")

(fn u64_fixture_path () -> string
  "glagol-std-import-fs-alpha-u64.txt")

(fn f64_fixture_path () -> string
  "glagol-std-import-fs-alpha-f64.txt")

(fn bool_fixture_path () -> string
  "glagol-std-import-fs-alpha-bool.txt")

(fn invalid_fixture_path () -> string
  "glagol-std-import-fs-alpha-invalid.txt")

(fn imported_write_text_status () -> i32
  (write_text_status (fixture_path) (fixture_text)))

(fn imported_read_text () -> string
  (write_text_status (fixture_path) (fixture_text))
  (read_text (fixture_path)))

(fn imported_write_text_result () -> (result i32 i32)
  (write_text_result (fixture_path) (fixture_text)))

(fn imported_read_text_result () -> (result string i32)
  (write_text_status (fixture_path) (fixture_text))
  (read_text_result (fixture_path)))

(fn imported_read_text_option_missing_none () -> bool
  (match (read_text_option (missing_fixture_path))
    ((some text)
      false)
    ((none)
      true)))

(fn imported_read_text_option_present_some () -> bool
  (write_text_status (fixture_path) (fixture_text))
  (match (read_text_option (fixture_path))
    ((some text)
      (= text (fixture_text)))
    ((none)
      false)))

(fn imported_read_text_or_missing_fallback () -> bool
  (= (read_text_or (missing_fixture_path) (fallback_text)) (fallback_text)))

(fn imported_read_text_or_present_value () -> bool
  (write_text_status (fixture_path) (fixture_text))
  (= (read_text_or (fixture_path) (fallback_text)) (fixture_text)))

(fn imported_write_text_ok () -> bool
  (write_text_ok (fixture_path) (fixture_text)))

(fn imported_read_i32_result_present () -> bool
  (write_text_status (i32_fixture_path) "42")
  (match (read_i32_result (i32_fixture_path))
    ((ok value)
      (= value 42))
    ((err code)
      false)))

(fn imported_read_i32_or_zero_invalid () -> bool
  (write_text_status (invalid_fixture_path) "bad")
  (= (read_i32_or_zero (invalid_fixture_path)) 0))

(fn imported_read_u32_result_present () -> bool
  (write_text_status (u32_fixture_path) "42")
  (match (read_u32_result (u32_fixture_path))
    ((ok value)
      (= value 42u32))
    ((err code)
      false)))

(fn imported_read_u32_or_zero_invalid () -> bool
  (write_text_status (invalid_fixture_path) "bad")
  (= (read_u32_or_zero (invalid_fixture_path)) 0u32))

(fn imported_read_i64_result_present () -> bool
  (write_text_status (i64_fixture_path) "42000000000")
  (match (read_i64_result (i64_fixture_path))
    ((ok value)
      (= value 42000000000i64))
    ((err code)
      false)))

(fn imported_read_i64_or_zero_missing () -> bool
  (= (read_i64_or_zero (missing_fixture_path)) 0i64))

(fn imported_read_u64_result_present () -> bool
  (write_text_status (u64_fixture_path) "4294967296")
  (match (read_u64_result (u64_fixture_path))
    ((ok value)
      (= value 4294967296u64))
    ((err code)
      false)))

(fn imported_read_u64_or_zero_missing () -> bool
  (= (read_u64_or_zero (missing_fixture_path)) 0u64))

(fn imported_read_f64_result_present () -> bool
  (write_text_status (f64_fixture_path) "42.5")
  (match (read_f64_result (f64_fixture_path))
    ((ok value)
      (= value 42.5))
    ((err code)
      false)))

(fn imported_read_f64_or_zero_invalid () -> bool
  (write_text_status (invalid_fixture_path) "bad")
  (= (read_f64_or_zero (invalid_fixture_path)) 0.0))

(fn imported_read_bool_result_present () -> bool
  (write_text_status (bool_fixture_path) "true")
  (match (read_bool_result (bool_fixture_path))
    ((ok value)
      value)
    ((err code)
      false)))

(fn imported_read_bool_or_false_invalid () -> bool
  (write_text_status (invalid_fixture_path) "bad")
  (if (read_bool_or_false (invalid_fixture_path))
    false
    true))

(fn imported_typed_options_ok () -> bool
  (write_text_status (i32_fixture_path) "42")
  (write_text_status (u32_fixture_path) "42")
  (write_text_status (i64_fixture_path) "42000000000")
  (write_text_status (u64_fixture_path) "4294967296")
  (write_text_status (f64_fixture_path) "42.5")
  (write_text_status (bool_fixture_path) "true")
  (write_text_status (invalid_fixture_path) "bad")
  (if (match (read_i32_option (i32_fixture_path))
    ((some value)
      (= value 42))
    ((none)
      false))
    (if (match (read_i32_option (invalid_fixture_path))
      ((some value)
        false)
      ((none)
        true))
      (if (match (read_u32_option (u32_fixture_path))
        ((some value)
          (= value 42u32))
        ((none)
          false))
        (if (match (read_u32_option (invalid_fixture_path))
          ((some value)
            false)
          ((none)
            true))
          (if (match (read_i64_option (i64_fixture_path))
            ((some value)
              (= value 42000000000i64))
            ((none)
              false))
            (if (match (read_i64_option (missing_fixture_path))
              ((some value)
                false)
              ((none)
                true))
              (if (match (read_u64_option (u64_fixture_path))
                ((some value)
                  (= value 4294967296u64))
                ((none)
                  false))
                (if (match (read_u64_option (missing_fixture_path))
                  ((some value)
                    false)
                  ((none)
                    true))
                  (if (match (read_f64_option (f64_fixture_path))
                    ((some value)
                      (= value 42.5))
                    ((none)
                      false))
                    (if (match (read_f64_option (invalid_fixture_path))
                      ((some value)
                        false)
                      ((none)
                        true))
                      (if (match (read_bool_option (bool_fixture_path))
                        ((some value)
                          value)
                        ((none)
                          false))
                        (match (read_bool_option (invalid_fixture_path))
                          ((some value)
                            false)
                          ((none)
                            true))
                        false)
                      false)
                    false)
                  false)
                false)
              false)
            false)
          false)
        false)
      false)
    false))

(fn imported_typed_custom_fallbacks_ok () -> bool
  (write_text_status (i32_fixture_path) "42")
  (write_text_status (u32_fixture_path) "42")
  (write_text_status (i64_fixture_path) "42000000000")
  (write_text_status (u64_fixture_path) "4294967296")
  (write_text_status (f64_fixture_path) "42.5")
  (write_text_status (bool_fixture_path) "true")
  (write_text_status (invalid_fixture_path) "bad")
  (if (= (read_i32_or (i32_fixture_path) 7) 42)
    (if (= (read_i32_or (invalid_fixture_path) 7) 7)
      (if (= (read_u32_or (u32_fixture_path) 7u32) 42u32)
        (if (= (read_u32_or (invalid_fixture_path) 7u32) 7u32)
          (if (= (read_i64_or (i64_fixture_path) 9i64) 42000000000i64)
            (if (= (read_i64_or (invalid_fixture_path) 9i64) 9i64)
              (if (= (read_u64_or (u64_fixture_path) 9u64) 4294967296u64)
                (if (= (read_u64_or (invalid_fixture_path) 9u64) 9u64)
                  (if (= (read_f64_or (f64_fixture_path) 1.5) 42.5)
                    (if (= (read_f64_or (invalid_fixture_path) 1.5) 1.5)
                      (if (read_bool_or (bool_fixture_path) false)
                        (read_bool_or (invalid_fixture_path) true)
                        false)
                      false)
                    false)
                  false)
                false)
              false)
            false)
          false)
        false)
      false)
    false))

(fn imported_status_roundtrip_ok () -> bool
  (write_text_status (fixture_path) (fixture_text))
  (= (read_text (fixture_path)) (fixture_text)))

(fn imported_result_roundtrip_ok () -> bool
  (write_text_result (fixture_path) (fixture_text))
  (= (unwrap_ok (read_text_result (fixture_path))) (fixture_text)))

(fn imported_fs_facade_ok () -> bool
  (if (imported_status_roundtrip_ok)
    (if (imported_result_roundtrip_ok)
      (if (imported_read_text_option_missing_none)
        (if (imported_read_text_option_present_some)
          (if (imported_read_text_or_missing_fallback)
            (if (imported_read_text_or_present_value)
              (if (imported_write_text_ok)
                (if (imported_read_i32_result_present)
                  (if (imported_read_i32_or_zero_invalid)
                    (if (imported_read_u32_result_present)
                      (if (imported_read_u32_or_zero_invalid)
                        (if (imported_read_i64_result_present)
                          (if (imported_read_i64_or_zero_missing)
                            (if (imported_read_u64_result_present)
                              (if (imported_read_u64_or_zero_missing)
                                (if (imported_read_f64_result_present)
                                  (if (imported_read_f64_or_zero_invalid)
                                    (if (imported_read_bool_result_present)
                                      (if (imported_read_bool_or_false_invalid)
                                        (if (imported_typed_options_ok)
                                          (imported_typed_custom_fallbacks_ok)
                                          false)
                                        false)
                                      false)
                                    false)
                                  false)
                                false)
                              false)
                            false)
                          false)
                        false)
                      false)
                    false)
                  false)
                false)
              false)
            false)
          false)
        false)
      false)
    false))

(fn main () -> i32
  (if (imported_fs_facade_ok)
    42
    1))

(test "explicit std fs write text status facade"
  (= (imported_write_text_status) 0))

(test "explicit std fs read text facade"
  (= (imported_read_text) (fixture_text)))

(test "explicit std fs write text result facade"
  (= (unwrap_ok (imported_write_text_result)) 0))

(test "explicit std fs read text result facade"
  (= (unwrap_ok (imported_read_text_result)) (fixture_text)))

(test "explicit std fs read text option missing facade"
  (imported_read_text_option_missing_none))

(test "explicit std fs read text option present facade"
  (imported_read_text_option_present_some))

(test "explicit std fs read text or missing facade"
  (imported_read_text_or_missing_fallback))

(test "explicit std fs read text or present facade"
  (imported_read_text_or_present_value))

(test "explicit std fs write text ok facade"
  (imported_write_text_ok))

(test "explicit std fs read i32 result present facade"
  (imported_read_i32_result_present))

(test "explicit std fs read i32 or zero invalid facade"
  (imported_read_i32_or_zero_invalid))

(test "explicit std fs read u32 result present facade"
  (imported_read_u32_result_present))

(test "explicit std fs read u32 or zero invalid facade"
  (imported_read_u32_or_zero_invalid))

(test "explicit std fs read i64 result present facade"
  (imported_read_i64_result_present))

(test "explicit std fs read i64 or zero missing facade"
  (imported_read_i64_or_zero_missing))

(test "explicit std fs read u64 result present facade"
  (imported_read_u64_result_present))

(test "explicit std fs read u64 or zero missing facade"
  (imported_read_u64_or_zero_missing))

(test "explicit std fs read f64 result present facade"
  (imported_read_f64_result_present))

(test "explicit std fs read f64 or zero invalid facade"
  (imported_read_f64_or_zero_invalid))

(test "explicit std fs read bool result present facade"
  (imported_read_bool_result_present))

(test "explicit std fs read bool or false invalid facade"
  (imported_read_bool_or_false_invalid))

(test "explicit std fs typed option facade"
  (imported_typed_options_ok))

(test "explicit std fs typed custom fallback facade"
  (imported_typed_custom_fallbacks_ok))

(test "explicit std fs facade all"
  (= (main) 42))
