imgcli\io.mbt

50% · 6 / 12 points · 66 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// 文件 IO 的平台分叉:`imgtool` 是 native 工具,但库包本身要保持
  3. 3/// 四目标可编译(`moon check --target all --deny-warn` 是仓库门禁),
  4. 4/// 因此 wasm/js 下给出同签名的拒绝实现而不是编译失败。
  5. 5
  6. 6///|
  7. 7#cfg(target="native")
  8. 8pub fn read_bytes(path : String) -> Bytes raise @types.ImageError {
  9. 9● @fs.read_file(path) catch {
  10. 10 _ =>
  11. 11● raise @types.ImageError::Decode(
  12. 12 @types.ErrorCode::InvalidArgument,
  13. 13 "读取文件失败: \{path}",
  14. 14 )
  15. 15 }
  16. 16}
  17. 17
  18. 18///|
  19. 19#cfg(target="native")
  20. 20pub fn write_bytes(path : String, data : Bytes) -> Unit raise @types.ImageError {
  21. 21● @fs.write_file(path, data) catch {
  22. 22 _ =>
  23. 23○ raise @types.ImageError::Process(
  24. 24 @types.ErrorCode::InvalidArgument,
  25. 25 "写入文件失败: \{path}",
  26. 26 )
  27. 27 }
  28. 28}
  29. 29
  30. 30///|
  31. 31#cfg(not(target="native"))
  32. 32pub fn read_bytes(path : String) -> Bytes raise @types.ImageError {
  33. 33○ ignore(path)
  34. 34 raise @types.ImageError::Decode(
  35. 35 @types.ErrorCode::UnsupportedFormat,
  36. 36 "imgtool 的读写依赖 native 文件系统,请以 native 目标构建",
  37. 37 )
  38. 38}
  39. 39
  40. 40///|
  41. 41#cfg(not(target="native"))
  42. 42pub fn write_bytes(path : String, data : Bytes) -> Unit raise @types.ImageError {
  43. 43○ ignore(path)
  44. 44○ ignore(data)
  45. 45 raise @types.ImageError::Process(
  46. 46 @types.ErrorCode::UnsupportedFormat,
  47. 47 "imgtool 的读写依赖 native 文件系统,请以 native 目标构建",
  48. 48 )
  49. 49}
  50. 50
  51. 51///|
  52. 52#cfg(target="native")
  53. 53pub fn ensure_dir(path : String) -> Unit {
  54. 54● if path == "" || path == "." {
  55. 55○ return
  56. 56 }
  57. 57● @fs.create_dir(path) catch {
  58. 58● _ => ()
  59. 59 }
  60. 60}
  61. 61
  62. 62///|
  63. 63#cfg(not(target="native"))
  64. 64pub fn ensure_dir(path : String) -> Unit {
  65. 65○ ignore(path)
  66. 66}