(module main)

(import std.fs (read_text read_text_result read_text_option write_text_status write_text_result exists is_file is_dir remove_file_result create_dir_result remove_file_ok create_dir_ok open_text_read_result read_open_text_result close_result read_text_via_handle_result close_ok 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 dir_fixture_path () -> string
  "glagol-std-import-fs-alpha-dir")

(fn dir_helper_fixture_path () -> string
  "glagol-std-import-fs-alpha-dir-helper")

(fn removable_fixture_path () -> string
  "glagol-std-import-fs-alpha-remove.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_via_handle_result () -> (result string i32)
  (write_text_status (fixture_path) (fixture_text))
  (read_text_via_handle_result (fixture_path)))

(fn imported_handle_open_read_close_ok () -> bool
  (write_text_status (fixture_path) (fixture_text))
  (match (open_text_read_result (fixture_path))
    ((ok handle)
      (let text string (unwrap_ok (read_open_text_result handle)))
      (let close_status i32 (unwrap_ok (close_result handle)))
      (and (= text (fixture_text)) (= close_status 0)))
    ((err code)
      false)))

(fn imported_handle_closed_read_err () -> bool
  (write_text_status (fixture_path) (fixture_text))
  (match (open_text_read_result (fixture_path))
    ((ok handle)
      (close_result handle)
      (= (unwrap_err (read_open_text_result handle)) 1))
    ((err code)
      false)))

(fn imported_handle_invalid_close_err () -> bool
  (= (unwrap_err (close_result -1)) 1))

(fn imported_exists_file_ok () -> bool
  (write_text_status (fixture_path) (fixture_text))
  (if (exists (fixture_path))
    (if (is_file (fixture_path))
      (if (is_dir (fixture_path))
        false
        true)
      false)
    false))

(fn imported_exists_missing_false () -> bool
  (if (exists (missing_fixture_path))
    false
    true))

(fn imported_create_dir_result_ok () -> bool
  (if (is_dir (dir_fixture_path))
    true
    (= (unwrap_ok (create_dir_result (dir_fixture_path))) 0)))

(fn imported_is_dir_ok () -> bool
  (imported_create_dir_result_ok)
  (is_dir (dir_fixture_path)))

(fn imported_create_dir_ok_helper () -> bool
  (if (is_dir (dir_helper_fixture_path))
    true
    (create_dir_ok (dir_helper_fixture_path))))

(fn imported_remove_file_result_ok () -> bool
  (write_text_status (removable_fixture_path) (fixture_text))
  (if (exists (removable_fixture_path))
    (if (= (unwrap_ok (remove_file_result (removable_fixture_path))) 0)
      (if (exists (removable_fixture_path))
        false
        true)
      false)
    false))

(fn imported_remove_file_ok_helper () -> bool
  (write_text_status (removable_fixture_path) (fixture_text))
  (if (remove_file_ok (removable_fixture_path))
    (if (exists (removable_fixture_path))
      false
      true)
    false))

(fn imported_fs_status_mutation_ok () -> bool
  (if (imported_exists_file_ok)
    (if (imported_exists_missing_false)
      (if (imported_create_dir_result_ok)
        (if (imported_is_dir_ok)
          (if (imported_create_dir_ok_helper)
            (if (imported_remove_file_result_ok)
              (imported_remove_file_ok_helper)
              false)
            false)
          false)
        false)
      false)
    false))

(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)
                                          (if (imported_typed_custom_fallbacks_ok)
                                            (if (= (unwrap_ok (imported_read_text_via_handle_result)) (fixture_text))
                                              (if (imported_handle_open_read_close_ok)
                                                (if (imported_handle_closed_read_err)
                                                  (if (imported_handle_invalid_close_err)
                                                    (imported_fs_status_mutation_ok)
                                                    false)
                                                  false)
                                                false)
                                              false)
                                            false)
                                          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 via handle facade"
  (= (unwrap_ok (imported_read_text_via_handle_result)) (fixture_text)))

(test "explicit std fs open read close handle facade"
  (imported_handle_open_read_close_ok))

(test "explicit std fs closed handle read err facade"
  (imported_handle_closed_read_err))

(test "explicit std fs invalid close err facade"
  (imported_handle_invalid_close_err))

(test "explicit std fs exists file facade"
  (imported_exists_file_ok))

(test "explicit std fs exists missing facade"
  (imported_exists_missing_false))

(test "explicit std fs create dir result facade"
  (imported_create_dir_result_ok))

(test "explicit std fs is dir facade"
  (imported_is_dir_ok))

(test "explicit std fs create dir ok facade"
  (imported_create_dir_ok_helper))

(test "explicit std fs remove file result facade"
  (imported_remove_file_result_ok))

(test "explicit std fs remove file ok facade"
  (imported_remove_file_ok_helper))

(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))
