@clayroach/effect 3.19.14-source-capture.8 → 3.19.14-source-trace.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (452) hide show
  1. package/SourceLocation/package.json +6 -0
  2. package/dist/cjs/Effect.js +2 -28
  3. package/dist/cjs/Effect.js.map +1 -1
  4. package/dist/cjs/FiberRef.js +12 -1
  5. package/dist/cjs/FiberRef.js.map +1 -1
  6. package/dist/cjs/Layer.js +2 -24
  7. package/dist/cjs/Layer.js.map +1 -1
  8. package/dist/cjs/RuntimeFlags.js +1 -29
  9. package/dist/cjs/RuntimeFlags.js.map +1 -1
  10. package/dist/cjs/SourceLocation.js +60 -0
  11. package/dist/cjs/SourceLocation.js.map +1 -0
  12. package/dist/cjs/Tracer.js +1 -15
  13. package/dist/cjs/Tracer.js.map +1 -1
  14. package/dist/cjs/Utils.js +1 -1
  15. package/dist/cjs/Utils.js.map +1 -1
  16. package/dist/cjs/index.js +3 -1
  17. package/dist/cjs/index.js.map +1 -1
  18. package/dist/cjs/internal/clock.js +1 -1
  19. package/dist/cjs/internal/clock.js.map +1 -1
  20. package/dist/cjs/internal/core.js +17 -50
  21. package/dist/cjs/internal/core.js.map +1 -1
  22. package/dist/cjs/internal/effect/circular.js +18 -30
  23. package/dist/cjs/internal/effect/circular.js.map +1 -1
  24. package/dist/cjs/internal/fiberRuntime.js +16 -65
  25. package/dist/cjs/internal/fiberRuntime.js.map +1 -1
  26. package/dist/cjs/internal/layer/circular.js +1 -5
  27. package/dist/cjs/internal/layer/circular.js.map +1 -1
  28. package/dist/cjs/internal/layer.js +1 -3
  29. package/dist/cjs/internal/layer.js.map +1 -1
  30. package/dist/cjs/internal/logger.js +25 -2
  31. package/dist/cjs/internal/logger.js.map +1 -1
  32. package/dist/cjs/internal/runtimeFlags.js +2 -11
  33. package/dist/cjs/internal/runtimeFlags.js.map +1 -1
  34. package/dist/cjs/internal/tracer.js +1 -114
  35. package/dist/cjs/internal/tracer.js.map +1 -1
  36. package/dist/dts/Config.d.ts +2 -2
  37. package/dist/dts/Config.d.ts.map +1 -1
  38. package/dist/dts/Effect.d.ts +8 -29
  39. package/dist/dts/Effect.d.ts.map +1 -1
  40. package/dist/dts/FiberRef.d.ts +12 -0
  41. package/dist/dts/FiberRef.d.ts.map +1 -1
  42. package/dist/dts/Layer.d.ts +0 -22
  43. package/dist/dts/Layer.d.ts.map +1 -1
  44. package/dist/dts/RuntimeFlags.d.ts +0 -28
  45. package/dist/dts/RuntimeFlags.d.ts.map +1 -1
  46. package/dist/dts/SourceLocation.d.ts +88 -0
  47. package/dist/dts/SourceLocation.d.ts.map +1 -0
  48. package/dist/dts/Tracer.d.ts +0 -15
  49. package/dist/dts/Tracer.d.ts.map +1 -1
  50. package/dist/dts/index.d.ts +6 -0
  51. package/dist/dts/index.d.ts.map +1 -1
  52. package/dist/dts/internal/core.d.ts.map +1 -1
  53. package/dist/dts/internal/layer.d.ts.map +1 -1
  54. package/dist/dts/internal/runtimeFlags.d.ts.map +1 -1
  55. package/dist/esm/Effect.js +0 -26
  56. package/dist/esm/Effect.js.map +1 -1
  57. package/dist/esm/FiberRef.js +11 -0
  58. package/dist/esm/FiberRef.js.map +1 -1
  59. package/dist/esm/Layer.js +0 -22
  60. package/dist/esm/Layer.js.map +1 -1
  61. package/dist/esm/RuntimeFlags.js +0 -28
  62. package/dist/esm/RuntimeFlags.js.map +1 -1
  63. package/dist/esm/SourceLocation.js +51 -0
  64. package/dist/esm/SourceLocation.js.map +1 -0
  65. package/dist/esm/Tracer.js +0 -14
  66. package/dist/esm/Tracer.js.map +1 -1
  67. package/dist/esm/Utils.js +1 -1
  68. package/dist/esm/Utils.js.map +1 -1
  69. package/dist/esm/index.js +6 -0
  70. package/dist/esm/index.js.map +1 -1
  71. package/dist/esm/internal/clock.js +1 -1
  72. package/dist/esm/internal/clock.js.map +1 -1
  73. package/dist/esm/internal/core.js +12 -45
  74. package/dist/esm/internal/core.js.map +1 -1
  75. package/dist/esm/internal/effect/circular.js +18 -30
  76. package/dist/esm/internal/effect/circular.js.map +1 -1
  77. package/dist/esm/internal/fiberRuntime.js +13 -60
  78. package/dist/esm/internal/fiberRuntime.js.map +1 -1
  79. package/dist/esm/internal/layer/circular.js +0 -4
  80. package/dist/esm/internal/layer/circular.js.map +1 -1
  81. package/dist/esm/internal/layer.js +0 -2
  82. package/dist/esm/internal/layer.js.map +1 -1
  83. package/dist/esm/internal/logger.js +25 -2
  84. package/dist/esm/internal/logger.js.map +1 -1
  85. package/dist/esm/internal/runtimeFlags.js +1 -9
  86. package/dist/esm/internal/runtimeFlags.js.map +1 -1
  87. package/dist/esm/internal/tracer.js +0 -111
  88. package/dist/esm/internal/tracer.js.map +1 -1
  89. package/package.json +12 -1
  90. package/src/Arbitrary.ts +1101 -0
  91. package/src/Array.ts +3589 -0
  92. package/src/BigDecimal.ts +1349 -0
  93. package/src/BigInt.ts +643 -0
  94. package/src/Boolean.ts +287 -0
  95. package/src/Brand.ts +360 -0
  96. package/src/Cache.ts +281 -0
  97. package/src/Cause.ts +1555 -0
  98. package/src/Channel.ts +2355 -0
  99. package/src/ChildExecutorDecision.ts +146 -0
  100. package/src/Chunk.ts +1495 -0
  101. package/src/Clock.ts +111 -0
  102. package/src/Config.ts +542 -0
  103. package/src/ConfigError.ts +270 -0
  104. package/src/ConfigProvider.ts +333 -0
  105. package/src/ConfigProviderPathPatch.ts +100 -0
  106. package/src/Console.ts +226 -0
  107. package/src/Context.ts +585 -0
  108. package/src/Cron.ts +706 -0
  109. package/src/Data.ts +596 -0
  110. package/src/DateTime.ts +1686 -0
  111. package/src/DefaultServices.ts +34 -0
  112. package/src/Deferred.ts +301 -0
  113. package/src/Differ.ts +450 -0
  114. package/src/Duration.ts +1000 -0
  115. package/src/Effect.ts +14817 -0
  116. package/src/Effectable.ts +107 -0
  117. package/src/Either.ts +1040 -0
  118. package/src/Encoding.ts +195 -0
  119. package/src/Equal.ts +98 -0
  120. package/src/Equivalence.ts +235 -0
  121. package/src/ExecutionPlan.ts +308 -0
  122. package/src/ExecutionStrategy.ts +119 -0
  123. package/src/Exit.ts +467 -0
  124. package/src/FastCheck.ts +9 -0
  125. package/src/Fiber.ts +744 -0
  126. package/src/FiberHandle.ts +540 -0
  127. package/src/FiberId.ts +195 -0
  128. package/src/FiberMap.ts +656 -0
  129. package/src/FiberRef.ts +444 -0
  130. package/src/FiberRefs.ts +204 -0
  131. package/src/FiberRefsPatch.ts +105 -0
  132. package/src/FiberSet.ts +491 -0
  133. package/src/FiberStatus.ts +108 -0
  134. package/src/Function.ts +1222 -0
  135. package/src/GlobalValue.ts +53 -0
  136. package/src/Graph.ts +3732 -0
  137. package/src/GroupBy.ts +103 -0
  138. package/src/HKT.ts +45 -0
  139. package/src/Hash.ts +195 -0
  140. package/src/HashMap.ts +519 -0
  141. package/src/HashRing.ts +317 -0
  142. package/src/HashSet.ts +2346 -0
  143. package/src/Inspectable.ts +287 -0
  144. package/src/Iterable.ts +1119 -0
  145. package/src/JSONSchema.ts +1044 -0
  146. package/src/KeyedPool.ts +167 -0
  147. package/src/Layer.ts +1228 -0
  148. package/src/LayerMap.ts +436 -0
  149. package/src/List.ts +977 -0
  150. package/src/LogLevel.ts +285 -0
  151. package/src/LogSpan.ts +25 -0
  152. package/src/Logger.ts +702 -0
  153. package/src/Mailbox.ts +268 -0
  154. package/src/ManagedRuntime.ts +180 -0
  155. package/src/Match.ts +1477 -0
  156. package/src/MergeDecision.ts +95 -0
  157. package/src/MergeState.ts +172 -0
  158. package/src/MergeStrategy.ts +107 -0
  159. package/src/Metric.ts +780 -0
  160. package/src/MetricBoundaries.ts +69 -0
  161. package/src/MetricHook.ts +151 -0
  162. package/src/MetricKey.ts +224 -0
  163. package/src/MetricKeyType.ts +262 -0
  164. package/src/MetricLabel.ts +47 -0
  165. package/src/MetricPair.ts +71 -0
  166. package/src/MetricPolling.ts +148 -0
  167. package/src/MetricRegistry.ts +48 -0
  168. package/src/MetricState.ts +257 -0
  169. package/src/Micro.ts +4405 -0
  170. package/src/ModuleVersion.ts +18 -0
  171. package/src/MutableHashMap.ts +411 -0
  172. package/src/MutableHashSet.ts +706 -0
  173. package/src/MutableList.ts +297 -0
  174. package/src/MutableQueue.ts +227 -0
  175. package/src/MutableRef.ts +202 -0
  176. package/src/NonEmptyIterable.ts +32 -0
  177. package/src/Number.ts +1071 -0
  178. package/src/Option.ts +2170 -0
  179. package/src/Order.ts +373 -0
  180. package/src/Ordering.ts +111 -0
  181. package/src/ParseResult.ts +2031 -0
  182. package/src/PartitionedSemaphore.ts +200 -0
  183. package/src/Pipeable.ts +566 -0
  184. package/src/Pool.ts +204 -0
  185. package/src/Predicate.ts +1405 -0
  186. package/src/Pretty.ts +205 -0
  187. package/src/PrimaryKey.ts +23 -0
  188. package/src/PubSub.ts +182 -0
  189. package/src/Queue.ts +644 -0
  190. package/src/Random.ts +204 -0
  191. package/src/RateLimiter.ts +138 -0
  192. package/src/RcMap.ts +141 -0
  193. package/src/RcRef.ts +122 -0
  194. package/src/Readable.ts +93 -0
  195. package/src/Record.ts +1274 -0
  196. package/src/RedBlackTree.ts +421 -0
  197. package/src/Redacted.ts +144 -0
  198. package/src/Ref.ts +180 -0
  199. package/src/RegExp.ts +38 -0
  200. package/src/Reloadable.ts +127 -0
  201. package/src/Request.ts +347 -0
  202. package/src/RequestBlock.ts +118 -0
  203. package/src/RequestResolver.ts +366 -0
  204. package/src/Resource.ts +119 -0
  205. package/src/Runtime.ts +383 -0
  206. package/src/RuntimeFlags.ts +336 -0
  207. package/src/RuntimeFlagsPatch.ts +183 -0
  208. package/src/STM.ts +2045 -0
  209. package/src/Schedule.ts +2219 -0
  210. package/src/ScheduleDecision.ts +62 -0
  211. package/src/ScheduleInterval.ts +151 -0
  212. package/src/ScheduleIntervals.ts +122 -0
  213. package/src/Scheduler.ts +353 -0
  214. package/src/Schema.ts +10914 -0
  215. package/src/SchemaAST.ts +3043 -0
  216. package/src/Scope.ts +204 -0
  217. package/src/ScopedCache.ts +151 -0
  218. package/src/ScopedRef.ts +117 -0
  219. package/src/Secret.ts +88 -0
  220. package/src/SingleProducerAsyncInput.ts +67 -0
  221. package/src/Sink.ts +1461 -0
  222. package/src/SortedMap.ts +287 -0
  223. package/src/SortedSet.ts +390 -0
  224. package/src/SourceLocation.ts +108 -0
  225. package/src/Stream.ts +6468 -0
  226. package/src/StreamEmit.ts +136 -0
  227. package/src/StreamHaltStrategy.ts +123 -0
  228. package/src/Streamable.ts +45 -0
  229. package/src/String.ts +778 -0
  230. package/src/Struct.ts +243 -0
  231. package/src/Subscribable.ts +100 -0
  232. package/src/SubscriptionRef.ts +298 -0
  233. package/src/Supervisor.ts +240 -0
  234. package/src/Symbol.ts +29 -0
  235. package/src/SynchronizedRef.ts +270 -0
  236. package/src/TArray.ts +495 -0
  237. package/src/TDeferred.ts +100 -0
  238. package/src/TMap.ts +515 -0
  239. package/src/TPriorityQueue.ts +223 -0
  240. package/src/TPubSub.ts +200 -0
  241. package/src/TQueue.ts +432 -0
  242. package/src/TRandom.ts +129 -0
  243. package/src/TReentrantLock.ts +224 -0
  244. package/src/TRef.ts +178 -0
  245. package/src/TSemaphore.ts +129 -0
  246. package/src/TSet.ts +365 -0
  247. package/src/TSubscriptionRef.ts +192 -0
  248. package/src/Take.ts +258 -0
  249. package/src/TestAnnotation.ts +158 -0
  250. package/src/TestAnnotationMap.ts +119 -0
  251. package/src/TestAnnotations.ts +117 -0
  252. package/src/TestClock.ts +556 -0
  253. package/src/TestConfig.ts +47 -0
  254. package/src/TestContext.ts +36 -0
  255. package/src/TestLive.ts +53 -0
  256. package/src/TestServices.ts +390 -0
  257. package/src/TestSized.ts +55 -0
  258. package/src/Tracer.ts +182 -0
  259. package/src/Trie.ts +840 -0
  260. package/src/Tuple.ts +305 -0
  261. package/src/Types.ts +353 -0
  262. package/src/Unify.ts +113 -0
  263. package/src/UpstreamPullRequest.ts +117 -0
  264. package/src/UpstreamPullStrategy.ts +121 -0
  265. package/src/Utils.ts +809 -0
  266. package/src/index.ts +1568 -0
  267. package/src/internal/array.ts +8 -0
  268. package/src/internal/blockedRequests.ts +520 -0
  269. package/src/internal/cache.ts +733 -0
  270. package/src/internal/cause.ts +1050 -0
  271. package/src/internal/channel/channelExecutor.ts +1200 -0
  272. package/src/internal/channel/channelState.ts +134 -0
  273. package/src/internal/channel/childExecutorDecision.ts +96 -0
  274. package/src/internal/channel/continuation.ts +200 -0
  275. package/src/internal/channel/mergeDecision.ts +113 -0
  276. package/src/internal/channel/mergeState.ts +120 -0
  277. package/src/internal/channel/mergeStrategy.ts +72 -0
  278. package/src/internal/channel/singleProducerAsyncInput.ts +259 -0
  279. package/src/internal/channel/subexecutor.ts +229 -0
  280. package/src/internal/channel/upstreamPullRequest.ts +84 -0
  281. package/src/internal/channel/upstreamPullStrategy.ts +87 -0
  282. package/src/internal/channel.ts +2603 -0
  283. package/src/internal/clock.ts +95 -0
  284. package/src/internal/completedRequestMap.ts +9 -0
  285. package/src/internal/concurrency.ts +54 -0
  286. package/src/internal/config.ts +716 -0
  287. package/src/internal/configError.ts +304 -0
  288. package/src/internal/configProvider/pathPatch.ts +97 -0
  289. package/src/internal/configProvider.ts +799 -0
  290. package/src/internal/console.ts +153 -0
  291. package/src/internal/context.ts +337 -0
  292. package/src/internal/core-effect.ts +2293 -0
  293. package/src/internal/core-stream.ts +998 -0
  294. package/src/internal/core.ts +3189 -0
  295. package/src/internal/data.ts +36 -0
  296. package/src/internal/dataSource.ts +327 -0
  297. package/src/internal/dateTime.ts +1277 -0
  298. package/src/internal/defaultServices/console.ts +100 -0
  299. package/src/internal/defaultServices.ts +163 -0
  300. package/src/internal/deferred.ts +46 -0
  301. package/src/internal/differ/chunkPatch.ts +211 -0
  302. package/src/internal/differ/contextPatch.ts +232 -0
  303. package/src/internal/differ/hashMapPatch.ts +220 -0
  304. package/src/internal/differ/hashSetPatch.ts +176 -0
  305. package/src/internal/differ/orPatch.ts +311 -0
  306. package/src/internal/differ/readonlyArrayPatch.ts +210 -0
  307. package/src/internal/differ.ts +200 -0
  308. package/src/internal/doNotation.ts +80 -0
  309. package/src/internal/effect/circular.ts +895 -0
  310. package/src/internal/effectable.ts +131 -0
  311. package/src/internal/either.ts +110 -0
  312. package/src/internal/encoding/base64.ts +286 -0
  313. package/src/internal/encoding/base64Url.ts +29 -0
  314. package/src/internal/encoding/common.ts +51 -0
  315. package/src/internal/encoding/hex.ts +315 -0
  316. package/src/internal/errors.ts +7 -0
  317. package/src/internal/executionPlan.ts +114 -0
  318. package/src/internal/executionStrategy.ts +74 -0
  319. package/src/internal/fiber.ts +388 -0
  320. package/src/internal/fiberId.ts +267 -0
  321. package/src/internal/fiberMessage.ts +82 -0
  322. package/src/internal/fiberRefs/patch.ts +144 -0
  323. package/src/internal/fiberRefs.ts +297 -0
  324. package/src/internal/fiberRuntime.ts +3842 -0
  325. package/src/internal/fiberScope.ts +71 -0
  326. package/src/internal/fiberStatus.ts +119 -0
  327. package/src/internal/groupBy.ts +530 -0
  328. package/src/internal/hashMap/array.ts +49 -0
  329. package/src/internal/hashMap/bitwise.ts +32 -0
  330. package/src/internal/hashMap/config.ts +14 -0
  331. package/src/internal/hashMap/keySet.ts +8 -0
  332. package/src/internal/hashMap/node.ts +391 -0
  333. package/src/internal/hashMap.ts +586 -0
  334. package/src/internal/hashSet.ts +323 -0
  335. package/src/internal/keyedPool.ts +244 -0
  336. package/src/internal/layer/circular.ts +214 -0
  337. package/src/internal/layer.ts +1483 -0
  338. package/src/internal/logSpan.ts +20 -0
  339. package/src/internal/logger-circular.ts +24 -0
  340. package/src/internal/logger.ts +522 -0
  341. package/src/internal/mailbox.ts +561 -0
  342. package/src/internal/managedRuntime/circular.ts +6 -0
  343. package/src/internal/managedRuntime.ts +134 -0
  344. package/src/internal/matcher.ts +652 -0
  345. package/src/internal/metric/boundaries.ts +75 -0
  346. package/src/internal/metric/hook.ts +483 -0
  347. package/src/internal/metric/key.ts +167 -0
  348. package/src/internal/metric/keyType.ts +238 -0
  349. package/src/internal/metric/label.ts +41 -0
  350. package/src/internal/metric/pair.ts +48 -0
  351. package/src/internal/metric/polling.ts +149 -0
  352. package/src/internal/metric/registry.ts +187 -0
  353. package/src/internal/metric/state.ts +290 -0
  354. package/src/internal/metric.ts +577 -0
  355. package/src/internal/opCodes/cause.ts +35 -0
  356. package/src/internal/opCodes/channel.ts +83 -0
  357. package/src/internal/opCodes/channelChildExecutorDecision.ts +17 -0
  358. package/src/internal/opCodes/channelMergeDecision.ts +11 -0
  359. package/src/internal/opCodes/channelMergeState.ts +17 -0
  360. package/src/internal/opCodes/channelMergeStrategy.ts +11 -0
  361. package/src/internal/opCodes/channelState.ts +23 -0
  362. package/src/internal/opCodes/channelUpstreamPullRequest.ts +11 -0
  363. package/src/internal/opCodes/channelUpstreamPullStrategy.ts +11 -0
  364. package/src/internal/opCodes/config.ts +65 -0
  365. package/src/internal/opCodes/configError.ts +35 -0
  366. package/src/internal/opCodes/continuation.ts +11 -0
  367. package/src/internal/opCodes/deferred.ts +11 -0
  368. package/src/internal/opCodes/effect.ts +89 -0
  369. package/src/internal/opCodes/layer.ts +59 -0
  370. package/src/internal/opCodes/streamHaltStrategy.ts +23 -0
  371. package/src/internal/option.ts +80 -0
  372. package/src/internal/pool.ts +432 -0
  373. package/src/internal/pubsub.ts +1762 -0
  374. package/src/internal/query.ts +204 -0
  375. package/src/internal/queue.ts +766 -0
  376. package/src/internal/random.ts +161 -0
  377. package/src/internal/rateLimiter.ts +93 -0
  378. package/src/internal/rcMap.ts +285 -0
  379. package/src/internal/rcRef.ts +192 -0
  380. package/src/internal/redBlackTree/iterator.ts +200 -0
  381. package/src/internal/redBlackTree/node.ts +68 -0
  382. package/src/internal/redBlackTree.ts +1245 -0
  383. package/src/internal/redacted.ts +73 -0
  384. package/src/internal/ref.ts +171 -0
  385. package/src/internal/reloadable.ts +140 -0
  386. package/src/internal/request.ts +177 -0
  387. package/src/internal/resource.ts +76 -0
  388. package/src/internal/ringBuffer.ts +68 -0
  389. package/src/internal/runtime.ts +558 -0
  390. package/src/internal/runtimeFlags.ts +178 -0
  391. package/src/internal/runtimeFlagsPatch.ts +103 -0
  392. package/src/internal/schedule/decision.ts +47 -0
  393. package/src/internal/schedule/interval.ts +101 -0
  394. package/src/internal/schedule/intervals.ts +180 -0
  395. package/src/internal/schedule.ts +2199 -0
  396. package/src/internal/schema/errors.ts +191 -0
  397. package/src/internal/schema/schemaId.ts +106 -0
  398. package/src/internal/schema/util.ts +50 -0
  399. package/src/internal/scopedCache.ts +644 -0
  400. package/src/internal/scopedRef.ts +118 -0
  401. package/src/internal/secret.ts +89 -0
  402. package/src/internal/singleShotGen.ts +35 -0
  403. package/src/internal/sink.ts +2120 -0
  404. package/src/internal/stack.ts +10 -0
  405. package/src/internal/stm/core.ts +817 -0
  406. package/src/internal/stm/entry.ts +59 -0
  407. package/src/internal/stm/journal.ts +123 -0
  408. package/src/internal/stm/opCodes/stm.ts +71 -0
  409. package/src/internal/stm/opCodes/stmState.ts +17 -0
  410. package/src/internal/stm/opCodes/strategy.ts +17 -0
  411. package/src/internal/stm/opCodes/tExit.ts +29 -0
  412. package/src/internal/stm/opCodes/tryCommit.ts +11 -0
  413. package/src/internal/stm/stm.ts +1453 -0
  414. package/src/internal/stm/stmState.ts +136 -0
  415. package/src/internal/stm/tArray.ts +550 -0
  416. package/src/internal/stm/tDeferred.ts +81 -0
  417. package/src/internal/stm/tExit.ts +190 -0
  418. package/src/internal/stm/tMap.ts +824 -0
  419. package/src/internal/stm/tPriorityQueue.ts +267 -0
  420. package/src/internal/stm/tPubSub.ts +551 -0
  421. package/src/internal/stm/tQueue.ts +393 -0
  422. package/src/internal/stm/tRandom.ts +140 -0
  423. package/src/internal/stm/tReentrantLock.ts +352 -0
  424. package/src/internal/stm/tRef.ts +195 -0
  425. package/src/internal/stm/tSemaphore.ts +113 -0
  426. package/src/internal/stm/tSet.ts +259 -0
  427. package/src/internal/stm/tSubscriptionRef.ts +286 -0
  428. package/src/internal/stm/tryCommit.ts +34 -0
  429. package/src/internal/stm/txnId.ts +14 -0
  430. package/src/internal/stm/versioned.ts +4 -0
  431. package/src/internal/stream/debounceState.ts +57 -0
  432. package/src/internal/stream/emit.ts +123 -0
  433. package/src/internal/stream/haltStrategy.ts +94 -0
  434. package/src/internal/stream/handoff.ts +187 -0
  435. package/src/internal/stream/handoffSignal.ts +59 -0
  436. package/src/internal/stream/pull.ts +34 -0
  437. package/src/internal/stream/sinkEndReason.ts +30 -0
  438. package/src/internal/stream/zipAllState.ts +88 -0
  439. package/src/internal/stream/zipChunksState.ts +56 -0
  440. package/src/internal/stream.ts +8801 -0
  441. package/src/internal/string-utils.ts +107 -0
  442. package/src/internal/subscriptionRef.ts +138 -0
  443. package/src/internal/supervisor/patch.ts +190 -0
  444. package/src/internal/supervisor.ts +303 -0
  445. package/src/internal/synchronizedRef.ts +114 -0
  446. package/src/internal/take.ts +199 -0
  447. package/src/internal/testing/sleep.ts +27 -0
  448. package/src/internal/testing/suspendedWarningData.ts +85 -0
  449. package/src/internal/testing/warningData.ts +94 -0
  450. package/src/internal/tracer.ts +150 -0
  451. package/src/internal/trie.ts +722 -0
  452. package/src/internal/version.ts +7 -0
@@ -0,0 +1,2031 @@
1
+ /**
2
+ * @since 3.10.0
3
+ */
4
+
5
+ import * as Arr from "./Array.js"
6
+ import * as Cause from "./Cause.js"
7
+ import { TaggedError } from "./Data.js"
8
+ import * as Effect from "./Effect.js"
9
+ import * as Either from "./Either.js"
10
+ import * as Exit from "./Exit.js"
11
+ import type { LazyArg } from "./Function.js"
12
+ import { dual } from "./Function.js"
13
+ import { globalValue } from "./GlobalValue.js"
14
+ import * as Inspectable from "./Inspectable.js"
15
+ import * as util_ from "./internal/schema/util.js"
16
+ import * as Option from "./Option.js"
17
+ import * as Predicate from "./Predicate.js"
18
+ import * as Scheduler from "./Scheduler.js"
19
+ import type * as Schema from "./Schema.js"
20
+ import * as AST from "./SchemaAST.js"
21
+ import type { Concurrency } from "./Types.js"
22
+
23
+ /**
24
+ * `ParseIssue` is a type that represents the different types of errors that can occur when decoding/encoding a value.
25
+ *
26
+ * @category model
27
+ * @since 3.10.0
28
+ */
29
+ export type ParseIssue =
30
+ // leaf
31
+ | Type
32
+ | Missing
33
+ | Unexpected
34
+ | Forbidden
35
+ // composite
36
+ | Pointer
37
+ | Refinement
38
+ | Transformation
39
+ | Composite
40
+
41
+ /**
42
+ * @category model
43
+ * @since 3.10.0
44
+ */
45
+ export type SingleOrNonEmpty<A> = A | Arr.NonEmptyReadonlyArray<A>
46
+
47
+ /**
48
+ * @category model
49
+ * @since 3.10.0
50
+ */
51
+ export type Path = SingleOrNonEmpty<PropertyKey>
52
+
53
+ /**
54
+ * @category model
55
+ * @since 3.10.0
56
+ */
57
+ export class Pointer {
58
+ /**
59
+ * @since 3.10.0
60
+ */
61
+ readonly _tag = "Pointer"
62
+ constructor(
63
+ readonly path: Path,
64
+ readonly actual: unknown,
65
+ readonly issue: ParseIssue
66
+ ) {}
67
+ }
68
+
69
+ /**
70
+ * Error that occurs when an unexpected key or index is present.
71
+ *
72
+ * @category model
73
+ * @since 3.10.0
74
+ */
75
+ export class Unexpected {
76
+ /**
77
+ * @since 3.10.0
78
+ */
79
+ readonly _tag = "Unexpected"
80
+ constructor(
81
+ readonly actual: unknown,
82
+ /**
83
+ * @since 3.10.0
84
+ */
85
+ readonly message?: string
86
+ ) {}
87
+ }
88
+
89
+ /**
90
+ * Error that occurs when a required key or index is missing.
91
+ *
92
+ * @category model
93
+ * @since 3.10.0
94
+ */
95
+ export class Missing {
96
+ /**
97
+ * @since 3.10.0
98
+ */
99
+ readonly _tag = "Missing"
100
+ /**
101
+ * @since 3.10.0
102
+ */
103
+ readonly actual = undefined
104
+ constructor(
105
+ /**
106
+ * @since 3.10.0
107
+ */
108
+ readonly ast: AST.Type,
109
+ /**
110
+ * @since 3.10.0
111
+ */
112
+ readonly message?: string
113
+ ) {}
114
+ }
115
+
116
+ /**
117
+ * Error that contains multiple issues.
118
+ *
119
+ * @category model
120
+ * @since 3.10.0
121
+ */
122
+ export class Composite {
123
+ /**
124
+ * @since 3.10.0
125
+ */
126
+ readonly _tag = "Composite"
127
+ constructor(
128
+ readonly ast: AST.AST,
129
+ readonly actual: unknown,
130
+ readonly issues: SingleOrNonEmpty<ParseIssue>,
131
+ readonly output?: unknown
132
+ ) {}
133
+ }
134
+
135
+ /**
136
+ * Error that occurs when a refinement has an error.
137
+ *
138
+ * @category model
139
+ * @since 3.10.0
140
+ */
141
+ export class Refinement {
142
+ /**
143
+ * @since 3.10.0
144
+ */
145
+ readonly _tag = "Refinement"
146
+ constructor(
147
+ readonly ast: AST.Refinement,
148
+ readonly actual: unknown,
149
+ readonly kind: "From" | "Predicate",
150
+ readonly issue: ParseIssue
151
+ ) {}
152
+ }
153
+
154
+ /**
155
+ * Error that occurs when a transformation has an error.
156
+ *
157
+ * @category model
158
+ * @since 3.10.0
159
+ */
160
+ export class Transformation {
161
+ /**
162
+ * @since 3.10.0
163
+ */
164
+ readonly _tag = "Transformation"
165
+ constructor(
166
+ readonly ast: AST.Transformation,
167
+ readonly actual: unknown,
168
+ readonly kind: "Encoded" | "Transformation" | "Type",
169
+ readonly issue: ParseIssue
170
+ ) {}
171
+ }
172
+
173
+ /**
174
+ * The `Type` variant of the `ParseIssue` type represents an error that occurs when the `actual` value is not of the expected type.
175
+ * The `ast` field specifies the expected type, and the `actual` field contains the value that caused the error.
176
+ *
177
+ * @category model
178
+ * @since 3.10.0
179
+ */
180
+ export class Type {
181
+ /**
182
+ * @since 3.10.0
183
+ */
184
+ readonly _tag = "Type"
185
+ constructor(
186
+ readonly ast: AST.AST,
187
+ readonly actual: unknown,
188
+ readonly message?: string
189
+ ) {}
190
+ }
191
+
192
+ /**
193
+ * The `Forbidden` variant of the `ParseIssue` type represents a forbidden operation, such as when encountering an Effect that is not allowed to execute (e.g., using `runSync`).
194
+ *
195
+ * @category model
196
+ * @since 3.10.0
197
+ */
198
+ export class Forbidden {
199
+ /**
200
+ * @since 3.10.0
201
+ */
202
+ readonly _tag = "Forbidden"
203
+ constructor(
204
+ readonly ast: AST.AST,
205
+ readonly actual: unknown,
206
+ readonly message?: string
207
+ ) {}
208
+ }
209
+
210
+ /**
211
+ * @category type id
212
+ * @since 3.10.0
213
+ */
214
+ export const ParseErrorTypeId: unique symbol = Symbol.for("effect/Schema/ParseErrorTypeId")
215
+
216
+ /**
217
+ * @category type id
218
+ * @since 3.10.0
219
+ */
220
+ export type ParseErrorTypeId = typeof ParseErrorTypeId
221
+
222
+ /**
223
+ * @since 3.10.0
224
+ */
225
+ export const isParseError = (u: unknown): u is ParseError => Predicate.hasProperty(u, ParseErrorTypeId)
226
+
227
+ /**
228
+ * @since 3.10.0
229
+ */
230
+ export class ParseError extends TaggedError("ParseError")<{ readonly issue: ParseIssue }> {
231
+ /**
232
+ * @since 3.10.0
233
+ */
234
+ readonly [ParseErrorTypeId] = ParseErrorTypeId
235
+
236
+ get message() {
237
+ return this.toString()
238
+ }
239
+ /**
240
+ * @since 3.10.0
241
+ */
242
+ toString() {
243
+ return TreeFormatter.formatIssueSync(this.issue)
244
+ }
245
+ /**
246
+ * @since 3.10.0
247
+ */
248
+ toJSON() {
249
+ return {
250
+ _id: "ParseError",
251
+ message: this.toString()
252
+ }
253
+ }
254
+ /**
255
+ * @since 3.10.0
256
+ */
257
+ [Inspectable.NodeInspectSymbol]() {
258
+ return this.toJSON()
259
+ }
260
+ }
261
+
262
+ /**
263
+ * @category constructors
264
+ * @since 3.10.0
265
+ */
266
+ export const parseError = (issue: ParseIssue): ParseError => new ParseError({ issue })
267
+
268
+ /**
269
+ * @category constructors
270
+ * @since 3.10.0
271
+ */
272
+ export const succeed: <A>(a: A) => Either.Either<A, ParseIssue> = Either.right
273
+
274
+ /**
275
+ * @category constructors
276
+ * @since 3.10.0
277
+ */
278
+ export const fail: (issue: ParseIssue) => Either.Either<never, ParseIssue> = Either.left
279
+
280
+ const _try: <A>(options: {
281
+ try: LazyArg<A>
282
+ catch: (e: unknown) => ParseIssue
283
+ }) => Either.Either<A, ParseIssue> = Either.try
284
+
285
+ export {
286
+ /**
287
+ * @category constructors
288
+ * @since 3.10.0
289
+ */
290
+ _try as try
291
+ }
292
+
293
+ /**
294
+ * @category constructors
295
+ * @since 3.10.0
296
+ */
297
+ export const fromOption: {
298
+ (onNone: () => ParseIssue): <A>(self: Option.Option<A>) => Either.Either<A, ParseIssue>
299
+ <A>(self: Option.Option<A>, onNone: () => ParseIssue): Either.Either<A, ParseIssue>
300
+ } = Either.fromOption
301
+
302
+ const isEither: <A, E, R>(self: Effect.Effect<A, E, R>) => self is Either.Either<A, E> = Either.isEither as any
303
+
304
+ /**
305
+ * @category optimisation
306
+ * @since 3.10.0
307
+ */
308
+ export const flatMap: {
309
+ <A, B, E1, R1>(
310
+ f: (a: A) => Effect.Effect<B, E1, R1>
311
+ ): <E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<B, E1 | E, R1 | R>
312
+ <A, E, R, B, E1, R1>(
313
+ self: Effect.Effect<A, E, R>,
314
+ f: (a: A) => Effect.Effect<B, E1, R1>
315
+ ): Effect.Effect<B, E | E1, R | R1>
316
+ } = dual(2, <A, E, R, B, E1, R1>(
317
+ self: Effect.Effect<A, E, R>,
318
+ f: (a: A) => Effect.Effect<B, E1, R1>
319
+ ): Effect.Effect<B, E | E1, R | R1> => {
320
+ return isEither(self) ?
321
+ Either.match(self, { onLeft: Either.left, onRight: f }) :
322
+ Effect.flatMap(self, f)
323
+ })
324
+
325
+ /**
326
+ * @category optimisation
327
+ * @since 3.10.0
328
+ */
329
+ export const map: {
330
+ <A, B>(f: (a: A) => B): <E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<B, E, R>
331
+ <A, E, R, B>(self: Effect.Effect<A, E, R>, f: (a: A) => B): Effect.Effect<B, E, R>
332
+ } = dual(2, <A, E, R, B>(self: Effect.Effect<A, E, R>, f: (a: A) => B): Effect.Effect<B, E, R> => {
333
+ return isEither(self) ?
334
+ Either.map(self, f) :
335
+ Effect.map(self, f)
336
+ })
337
+
338
+ /**
339
+ * @category optimisation
340
+ * @since 3.10.0
341
+ */
342
+ export const mapError: {
343
+ <E, E2>(f: (e: E) => E2): <A, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E2, R>
344
+ <A, E, R, E2>(self: Effect.Effect<A, E, R>, f: (e: E) => E2): Effect.Effect<A, E2, R>
345
+ } = dual(2, <A, E, R, E2>(self: Effect.Effect<A, E, R>, f: (e: E) => E2): Effect.Effect<A, E2, R> => {
346
+ return isEither(self) ?
347
+ Either.mapLeft(self, f) :
348
+ Effect.mapError(self, f)
349
+ })
350
+
351
+ // TODO(4.0): remove
352
+ /**
353
+ * @category optimisation
354
+ * @since 3.10.0
355
+ */
356
+ export const eitherOrUndefined = <A, E, R>(
357
+ self: Effect.Effect<A, E, R>
358
+ ): Either.Either<A, E> | undefined => {
359
+ if (isEither(self)) {
360
+ return self
361
+ }
362
+ }
363
+
364
+ /**
365
+ * @category optimisation
366
+ * @since 3.10.0
367
+ */
368
+ export const mapBoth: {
369
+ <E, E2, A, A2>(
370
+ options: { readonly onFailure: (e: E) => E2; readonly onSuccess: (a: A) => A2 }
371
+ ): <R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A2, E2, R>
372
+ <A, E, R, E2, A2>(
373
+ self: Effect.Effect<A, E, R>,
374
+ options: { readonly onFailure: (e: E) => E2; readonly onSuccess: (a: A) => A2 }
375
+ ): Effect.Effect<A2, E2, R>
376
+ } = dual(2, <A, E, R, E2, A2>(
377
+ self: Effect.Effect<A, E, R>,
378
+ options: { readonly onFailure: (e: E) => E2; readonly onSuccess: (a: A) => A2 }
379
+ ): Effect.Effect<A2, E2, R> => {
380
+ return isEither(self) ?
381
+ Either.mapBoth(self, { onLeft: options.onFailure, onRight: options.onSuccess }) :
382
+ Effect.mapBoth(self, options)
383
+ })
384
+
385
+ /**
386
+ * @category optimisation
387
+ * @since 3.10.0
388
+ */
389
+ export const orElse: {
390
+ <E, A2, E2, R2>(
391
+ f: (e: E) => Effect.Effect<A2, E2, R2>
392
+ ): <A, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A2 | A, E2, R2 | R>
393
+ <A, E, R, A2, E2, R2>(
394
+ self: Effect.Effect<A, E, R>,
395
+ f: (e: E) => Effect.Effect<A2, E2, R2>
396
+ ): Effect.Effect<A2 | A, E2, R2 | R>
397
+ } = dual(2, <A, E, R, A2, E2, R2>(
398
+ self: Effect.Effect<A, E, R>,
399
+ f: (e: E) => Effect.Effect<A2, E2, R2>
400
+ ): Effect.Effect<A2 | A, E2, R2 | R> => {
401
+ return isEither(self) ?
402
+ Either.match(self, { onLeft: f, onRight: Either.right }) :
403
+ Effect.catchAll(self, f)
404
+ })
405
+
406
+ /**
407
+ * @since 3.10.0
408
+ */
409
+ export type DecodeUnknown<Out, R> = (u: unknown, options?: AST.ParseOptions) => Effect.Effect<Out, ParseIssue, R>
410
+
411
+ /**
412
+ * @since 3.10.0
413
+ */
414
+ export type DeclarationDecodeUnknown<Out, R> = (
415
+ u: unknown,
416
+ options: AST.ParseOptions,
417
+ ast: AST.Declaration
418
+ ) => Effect.Effect<Out, ParseIssue, R>
419
+
420
+ /** @internal */
421
+ export const mergeInternalOptions = (
422
+ options: InternalOptions | undefined,
423
+ overrideOptions: InternalOptions | number | undefined
424
+ ): InternalOptions | undefined => {
425
+ if (overrideOptions === undefined || Predicate.isNumber(overrideOptions)) {
426
+ return options
427
+ }
428
+ if (options === undefined) {
429
+ return overrideOptions
430
+ }
431
+ return { ...options, ...overrideOptions }
432
+ }
433
+
434
+ const getEither = (ast: AST.AST, isDecoding: boolean, options?: AST.ParseOptions) => {
435
+ const parser = goMemo(ast, isDecoding)
436
+ return (u: unknown, overrideOptions?: AST.ParseOptions): Either.Either<any, ParseIssue> =>
437
+ parser(u, mergeInternalOptions(options, overrideOptions)) as any
438
+ }
439
+
440
+ const getSync = (ast: AST.AST, isDecoding: boolean, options?: AST.ParseOptions) => {
441
+ const parser = getEither(ast, isDecoding, options)
442
+ return (input: unknown, overrideOptions?: AST.ParseOptions) =>
443
+ Either.getOrThrowWith(parser(input, overrideOptions), parseError)
444
+ }
445
+
446
+ /** @internal */
447
+ export const getOption = (ast: AST.AST, isDecoding: boolean, options?: AST.ParseOptions) => {
448
+ const parser = getEither(ast, isDecoding, options)
449
+ return (input: unknown, overrideOptions?: AST.ParseOptions): Option.Option<any> =>
450
+ Option.getRight(parser(input, overrideOptions))
451
+ }
452
+
453
+ const getEffect = <R>(ast: AST.AST, isDecoding: boolean, options?: AST.ParseOptions) => {
454
+ const parser = goMemo(ast, isDecoding)
455
+ return (input: unknown, overrideOptions?: AST.ParseOptions): Effect.Effect<any, ParseIssue, R> =>
456
+ parser(input, { ...mergeInternalOptions(options, overrideOptions), isEffectAllowed: true })
457
+ }
458
+
459
+ /**
460
+ * @throws `ParseError`
461
+ * @category decoding
462
+ * @since 3.10.0
463
+ */
464
+ export const decodeUnknownSync = <A, I>(
465
+ schema: Schema.Schema<A, I, never>,
466
+ options?: AST.ParseOptions
467
+ ): (u: unknown, overrideOptions?: AST.ParseOptions) => A => getSync(schema.ast, true, options)
468
+
469
+ /**
470
+ * @category decoding
471
+ * @since 3.10.0
472
+ */
473
+ export const decodeUnknownOption = <A, I>(
474
+ schema: Schema.Schema<A, I, never>,
475
+ options?: AST.ParseOptions
476
+ ): (u: unknown, overrideOptions?: AST.ParseOptions) => Option.Option<A> => getOption(schema.ast, true, options)
477
+
478
+ /**
479
+ * @category decoding
480
+ * @since 3.10.0
481
+ */
482
+ export const decodeUnknownEither = <A, I>(
483
+ schema: Schema.Schema<A, I, never>,
484
+ options?: AST.ParseOptions
485
+ ): (u: unknown, overrideOptions?: AST.ParseOptions) => Either.Either<A, ParseIssue> =>
486
+ getEither(schema.ast, true, options)
487
+
488
+ /**
489
+ * @category decoding
490
+ * @since 3.10.0
491
+ */
492
+ export const decodeUnknownPromise = <A, I>(
493
+ schema: Schema.Schema<A, I, never>,
494
+ options?: AST.ParseOptions
495
+ ) => {
496
+ const parser = decodeUnknown(schema, options)
497
+ return (u: unknown, overrideOptions?: AST.ParseOptions): Promise<A> => Effect.runPromise(parser(u, overrideOptions))
498
+ }
499
+
500
+ /**
501
+ * @category decoding
502
+ * @since 3.10.0
503
+ */
504
+ export const decodeUnknown = <A, I, R>(
505
+ schema: Schema.Schema<A, I, R>,
506
+ options?: AST.ParseOptions
507
+ ): (u: unknown, overrideOptions?: AST.ParseOptions) => Effect.Effect<A, ParseIssue, R> =>
508
+ getEffect(schema.ast, true, options)
509
+
510
+ /**
511
+ * @throws `ParseError`
512
+ * @category encoding
513
+ * @since 3.10.0
514
+ */
515
+ export const encodeUnknownSync = <A, I>(
516
+ schema: Schema.Schema<A, I, never>,
517
+ options?: AST.ParseOptions
518
+ ): (u: unknown, overrideOptions?: AST.ParseOptions) => I => getSync(schema.ast, false, options)
519
+
520
+ /**
521
+ * @category encoding
522
+ * @since 3.10.0
523
+ */
524
+ export const encodeUnknownOption = <A, I>(
525
+ schema: Schema.Schema<A, I, never>,
526
+ options?: AST.ParseOptions
527
+ ): (u: unknown, overrideOptions?: AST.ParseOptions) => Option.Option<I> => getOption(schema.ast, false, options)
528
+
529
+ /**
530
+ * @category encoding
531
+ * @since 3.10.0
532
+ */
533
+ export const encodeUnknownEither = <A, I>(
534
+ schema: Schema.Schema<A, I, never>,
535
+ options?: AST.ParseOptions
536
+ ): (u: unknown, overrideOptions?: AST.ParseOptions) => Either.Either<I, ParseIssue> =>
537
+ getEither(schema.ast, false, options)
538
+
539
+ /**
540
+ * @category encoding
541
+ * @since 3.10.0
542
+ */
543
+ export const encodeUnknownPromise = <A, I>(
544
+ schema: Schema.Schema<A, I, never>,
545
+ options?: AST.ParseOptions
546
+ ) => {
547
+ const parser = encodeUnknown(schema, options)
548
+ return (u: unknown, overrideOptions?: AST.ParseOptions): Promise<I> => Effect.runPromise(parser(u, overrideOptions))
549
+ }
550
+
551
+ /**
552
+ * @category encoding
553
+ * @since 3.10.0
554
+ */
555
+ export const encodeUnknown = <A, I, R>(
556
+ schema: Schema.Schema<A, I, R>,
557
+ options?: AST.ParseOptions
558
+ ): (u: unknown, overrideOptions?: AST.ParseOptions) => Effect.Effect<I, ParseIssue, R> =>
559
+ getEffect(schema.ast, false, options)
560
+
561
+ /**
562
+ * @category decoding
563
+ * @since 3.10.0
564
+ */
565
+ export const decodeSync: <A, I>(
566
+ schema: Schema.Schema<A, I, never>,
567
+ options?: AST.ParseOptions
568
+ ) => (i: I, overrideOptions?: AST.ParseOptions) => A = decodeUnknownSync
569
+
570
+ /**
571
+ * @category decoding
572
+ * @since 3.10.0
573
+ */
574
+ export const decodeOption: <A, I>(
575
+ schema: Schema.Schema<A, I, never>,
576
+ options?: AST.ParseOptions
577
+ ) => (i: I, overrideOptions?: AST.ParseOptions) => Option.Option<A> = decodeUnknownOption
578
+
579
+ /**
580
+ * @category decoding
581
+ * @since 3.10.0
582
+ */
583
+ export const decodeEither: <A, I>(
584
+ schema: Schema.Schema<A, I, never>,
585
+ options?: AST.ParseOptions
586
+ ) => (i: I, overrideOptions?: AST.ParseOptions) => Either.Either<A, ParseIssue> = decodeUnknownEither
587
+
588
+ /**
589
+ * @category decoding
590
+ * @since 3.10.0
591
+ */
592
+ export const decodePromise: <A, I>(
593
+ schema: Schema.Schema<A, I, never>,
594
+ options?: AST.ParseOptions
595
+ ) => (i: I, overrideOptions?: AST.ParseOptions) => Promise<A> = decodeUnknownPromise
596
+
597
+ /**
598
+ * @category decoding
599
+ * @since 3.10.0
600
+ */
601
+ export const decode: <A, I, R>(
602
+ schema: Schema.Schema<A, I, R>,
603
+ options?: AST.ParseOptions
604
+ ) => (i: I, overrideOptions?: AST.ParseOptions) => Effect.Effect<A, ParseIssue, R> = decodeUnknown
605
+
606
+ /**
607
+ * @throws `ParseError`
608
+ * @category validation
609
+ * @since 3.10.0
610
+ */
611
+ export const validateSync = <A, I, R>(
612
+ schema: Schema.Schema<A, I, R>,
613
+ options?: AST.ParseOptions
614
+ ): (u: unknown, overrideOptions?: AST.ParseOptions) => A => getSync(AST.typeAST(schema.ast), true, options)
615
+
616
+ /**
617
+ * @category validation
618
+ * @since 3.10.0
619
+ */
620
+ export const validateOption = <A, I, R>(
621
+ schema: Schema.Schema<A, I, R>,
622
+ options?: AST.ParseOptions
623
+ ): (u: unknown, overrideOptions?: AST.ParseOptions) => Option.Option<A> =>
624
+ getOption(AST.typeAST(schema.ast), true, options)
625
+
626
+ /**
627
+ * @category validation
628
+ * @since 3.10.0
629
+ */
630
+ export const validateEither = <A, I, R>(
631
+ schema: Schema.Schema<A, I, R>,
632
+ options?: AST.ParseOptions
633
+ ): (u: unknown, overrideOptions?: AST.ParseOptions) => Either.Either<A, ParseIssue> =>
634
+ getEither(AST.typeAST(schema.ast), true, options)
635
+
636
+ /**
637
+ * @category validation
638
+ * @since 3.10.0
639
+ */
640
+ export const validatePromise = <A, I>(
641
+ schema: Schema.Schema<A, I, never>,
642
+ options?: AST.ParseOptions
643
+ ) => {
644
+ const parser = validate(schema, options)
645
+ return (u: unknown, overrideOptions?: AST.ParseOptions): Promise<A> => Effect.runPromise(parser(u, overrideOptions))
646
+ }
647
+
648
+ /**
649
+ * @category validation
650
+ * @since 3.10.0
651
+ */
652
+ export const validate = <A, I, R>(
653
+ schema: Schema.Schema<A, I, R>,
654
+ options?: AST.ParseOptions
655
+ ): (a: unknown, overrideOptions?: AST.ParseOptions) => Effect.Effect<A, ParseIssue, R> =>
656
+ getEffect(AST.typeAST(schema.ast), true, options)
657
+
658
+ /**
659
+ * By default the option `exact` is set to `true`.
660
+ *
661
+ * @category validation
662
+ * @since 3.10.0
663
+ */
664
+ export const is = <A, I, R>(schema: Schema.Schema<A, I, R>, options?: AST.ParseOptions) => {
665
+ const parser = goMemo(AST.typeAST(schema.ast), true)
666
+ return (u: unknown, overrideOptions?: AST.ParseOptions | number): u is A =>
667
+ Either.isRight(parser(u, { exact: true, ...mergeInternalOptions(options, overrideOptions) }) as any)
668
+ }
669
+
670
+ /**
671
+ * By default the option `exact` is set to `true`.
672
+ *
673
+ * @throws `ParseError`
674
+ * @category validation
675
+ * @since 3.10.0
676
+ */
677
+ export const asserts = <A, I, R>(schema: Schema.Schema<A, I, R>, options?: AST.ParseOptions) => {
678
+ const parser = goMemo(AST.typeAST(schema.ast), true)
679
+ return (u: unknown, overrideOptions?: AST.ParseOptions): asserts u is A => {
680
+ const result: Either.Either<any, ParseIssue> = parser(u, {
681
+ exact: true,
682
+ ...mergeInternalOptions(options, overrideOptions)
683
+ }) as any
684
+ if (Either.isLeft(result)) {
685
+ throw parseError(result.left)
686
+ }
687
+ }
688
+ }
689
+
690
+ /**
691
+ * @category encoding
692
+ * @since 3.10.0
693
+ */
694
+ export const encodeSync: <A, I>(
695
+ schema: Schema.Schema<A, I, never>,
696
+ options?: AST.ParseOptions
697
+ ) => (a: A, overrideOptions?: AST.ParseOptions) => I = encodeUnknownSync
698
+
699
+ /**
700
+ * @category encoding
701
+ * @since 3.10.0
702
+ */
703
+ export const encodeOption: <A, I>(
704
+ schema: Schema.Schema<A, I, never>,
705
+ options?: AST.ParseOptions
706
+ ) => (input: A, overrideOptions?: AST.ParseOptions) => Option.Option<I> = encodeUnknownOption
707
+
708
+ /**
709
+ * @category encoding
710
+ * @since 3.10.0
711
+ */
712
+ export const encodeEither: <A, I>(
713
+ schema: Schema.Schema<A, I, never>,
714
+ options?: AST.ParseOptions
715
+ ) => (a: A, overrideOptions?: AST.ParseOptions) => Either.Either<I, ParseIssue> = encodeUnknownEither
716
+
717
+ /**
718
+ * @category encoding
719
+ * @since 3.10.0
720
+ */
721
+ export const encodePromise: <A, I>(
722
+ schema: Schema.Schema<A, I, never>,
723
+ options?: AST.ParseOptions
724
+ ) => (a: A, overrideOptions?: AST.ParseOptions) => Promise<I> = encodeUnknownPromise
725
+
726
+ /**
727
+ * @category encoding
728
+ * @since 3.10.0
729
+ */
730
+ export const encode: <A, I, R>(
731
+ schema: Schema.Schema<A, I, R>,
732
+ options?: AST.ParseOptions
733
+ ) => (a: A, overrideOptions?: AST.ParseOptions) => Effect.Effect<I, ParseIssue, R> = encodeUnknown
734
+
735
+ interface InternalOptions extends AST.ParseOptions {
736
+ readonly isEffectAllowed?: boolean
737
+ }
738
+
739
+ interface Parser {
740
+ (i: any, options?: InternalOptions): Effect.Effect<any, ParseIssue, any>
741
+ }
742
+
743
+ const decodeMemoMap = globalValue(
744
+ Symbol.for("effect/ParseResult/decodeMemoMap"),
745
+ () => new WeakMap<AST.AST, Parser>()
746
+ )
747
+ const encodeMemoMap = globalValue(
748
+ Symbol.for("effect/ParseResult/encodeMemoMap"),
749
+ () => new WeakMap<AST.AST, Parser>()
750
+ )
751
+
752
+ const goMemo = (ast: AST.AST, isDecoding: boolean): Parser => {
753
+ const memoMap = isDecoding ? decodeMemoMap : encodeMemoMap
754
+ const memo = memoMap.get(ast)
755
+ if (memo) {
756
+ return memo
757
+ }
758
+ const raw = go(ast, isDecoding)
759
+ const parseOptionsAnnotation = AST.getParseOptionsAnnotation(ast)
760
+ const parserWithOptions: Parser = Option.isSome(parseOptionsAnnotation)
761
+ ? (i, options) => raw(i, mergeInternalOptions(options, parseOptionsAnnotation.value))
762
+ : raw
763
+ const decodingFallbackAnnotation = AST.getDecodingFallbackAnnotation(ast)
764
+ const parser: Parser = isDecoding && Option.isSome(decodingFallbackAnnotation)
765
+ ? (i, options) =>
766
+ handleForbidden(orElse(parserWithOptions(i, options), decodingFallbackAnnotation.value), ast, i, options)
767
+ : parserWithOptions
768
+ memoMap.set(ast, parser)
769
+ return parser
770
+ }
771
+
772
+ const getConcurrency = (ast: AST.AST): Concurrency | undefined =>
773
+ Option.getOrUndefined(AST.getConcurrencyAnnotation(ast))
774
+
775
+ const getBatching = (ast: AST.AST): boolean | "inherit" | undefined =>
776
+ Option.getOrUndefined(AST.getBatchingAnnotation(ast))
777
+
778
+ const go = (ast: AST.AST, isDecoding: boolean): Parser => {
779
+ switch (ast._tag) {
780
+ case "Refinement": {
781
+ if (isDecoding) {
782
+ const from = goMemo(ast.from, true)
783
+ return (i, options) => {
784
+ options = options ?? AST.defaultParseOption
785
+ const allErrors = options?.errors === "all"
786
+ const result = flatMap(
787
+ orElse(from(i, options), (ef) => {
788
+ const issue = new Refinement(ast, i, "From", ef)
789
+ if (allErrors && AST.hasStableFilter(ast) && isComposite(ef)) {
790
+ return Option.match(
791
+ ast.filter(i, options, ast),
792
+ {
793
+ onNone: () => Either.left<ParseIssue>(issue),
794
+ onSome: (ep) => Either.left(new Composite(ast, i, [issue, new Refinement(ast, i, "Predicate", ep)]))
795
+ }
796
+ )
797
+ }
798
+ return Either.left(issue)
799
+ }),
800
+ (a) =>
801
+ Option.match(
802
+ ast.filter(a, options, ast),
803
+ {
804
+ onNone: () => Either.right(a),
805
+ onSome: (ep) => Either.left(new Refinement(ast, i, "Predicate", ep))
806
+ }
807
+ )
808
+ )
809
+ return handleForbidden(result, ast, i, options)
810
+ }
811
+ } else {
812
+ const from = goMemo(AST.typeAST(ast), true)
813
+ const to = goMemo(dropRightRefinement(ast.from), false)
814
+ return (i, options) => handleForbidden(flatMap(from(i, options), (a) => to(a, options)), ast, i, options)
815
+ }
816
+ }
817
+ case "Transformation": {
818
+ const transform = getFinalTransformation(ast.transformation, isDecoding)
819
+ const from = isDecoding ? goMemo(ast.from, true) : goMemo(ast.to, false)
820
+ const to = isDecoding ? goMemo(ast.to, true) : goMemo(ast.from, false)
821
+ return (i, options) =>
822
+ handleForbidden(
823
+ flatMap(
824
+ mapError(
825
+ from(i, options),
826
+ (e) => new Transformation(ast, i, isDecoding ? "Encoded" : "Type", e)
827
+ ),
828
+ (a) =>
829
+ flatMap(
830
+ mapError(
831
+ transform(a, options ?? AST.defaultParseOption, ast, i),
832
+ (e) => new Transformation(ast, i, "Transformation", e)
833
+ ),
834
+ (i2) =>
835
+ mapError(
836
+ to(i2, options),
837
+ (e) => new Transformation(ast, i, isDecoding ? "Type" : "Encoded", e)
838
+ )
839
+ )
840
+ ),
841
+ ast,
842
+ i,
843
+ options
844
+ )
845
+ }
846
+ case "Declaration": {
847
+ const parse = isDecoding
848
+ ? ast.decodeUnknown(...ast.typeParameters)
849
+ : ast.encodeUnknown(...ast.typeParameters)
850
+ return (i, options) => handleForbidden(parse(i, options ?? AST.defaultParseOption, ast), ast, i, options)
851
+ }
852
+ case "Literal":
853
+ return fromRefinement(ast, (u): u is typeof ast.literal => u === ast.literal)
854
+ case "UniqueSymbol":
855
+ return fromRefinement(ast, (u): u is typeof ast.symbol => u === ast.symbol)
856
+ case "UndefinedKeyword":
857
+ return fromRefinement(ast, Predicate.isUndefined)
858
+ case "NeverKeyword":
859
+ return fromRefinement(ast, Predicate.isNever)
860
+ case "UnknownKeyword":
861
+ case "AnyKeyword":
862
+ case "VoidKeyword":
863
+ return Either.right
864
+ case "StringKeyword":
865
+ return fromRefinement(ast, Predicate.isString)
866
+ case "NumberKeyword":
867
+ return fromRefinement(ast, Predicate.isNumber)
868
+ case "BooleanKeyword":
869
+ return fromRefinement(ast, Predicate.isBoolean)
870
+ case "BigIntKeyword":
871
+ return fromRefinement(ast, Predicate.isBigInt)
872
+ case "SymbolKeyword":
873
+ return fromRefinement(ast, Predicate.isSymbol)
874
+ case "ObjectKeyword":
875
+ return fromRefinement(ast, Predicate.isObject)
876
+ case "Enums":
877
+ return fromRefinement(ast, (u): u is any => ast.enums.some(([_, value]) => value === u))
878
+ case "TemplateLiteral": {
879
+ const regex = AST.getTemplateLiteralRegExp(ast)
880
+ return fromRefinement(ast, (u): u is any => Predicate.isString(u) && regex.test(u))
881
+ }
882
+ case "TupleType": {
883
+ const elements = ast.elements.map((e) => goMemo(e.type, isDecoding))
884
+ const rest = ast.rest.map((annotatedAST) => goMemo(annotatedAST.type, isDecoding))
885
+ let requiredTypes: Array<AST.Type> = ast.elements.filter((e) => !e.isOptional)
886
+ if (ast.rest.length > 0) {
887
+ requiredTypes = requiredTypes.concat(ast.rest.slice(1))
888
+ }
889
+ const requiredLen = requiredTypes.length
890
+ const expectedIndexes = ast.elements.length > 0 ? ast.elements.map((_, i) => i).join(" | ") : "never"
891
+ const concurrency = getConcurrency(ast)
892
+ const batching = getBatching(ast)
893
+ return (input: unknown, options) => {
894
+ if (!Arr.isArray(input)) {
895
+ return Either.left(new Type(ast, input))
896
+ }
897
+ const allErrors = options?.errors === "all"
898
+ const es: Array<[number, ParseIssue]> = []
899
+ let stepKey = 0
900
+ const output: Array<[number, any]> = []
901
+ // ---------------------------------------------
902
+ // handle missing indexes
903
+ // ---------------------------------------------
904
+ const len = input.length
905
+ for (let i = len; i <= requiredLen - 1; i++) {
906
+ const e = new Pointer(i, input, new Missing(requiredTypes[i - len]))
907
+ if (allErrors) {
908
+ es.push([stepKey++, e])
909
+ continue
910
+ } else {
911
+ return Either.left(new Composite(ast, input, e, output))
912
+ }
913
+ }
914
+
915
+ // ---------------------------------------------
916
+ // handle excess indexes
917
+ // ---------------------------------------------
918
+ if (ast.rest.length === 0) {
919
+ for (let i = ast.elements.length; i <= len - 1; i++) {
920
+ const e = new Pointer(i, input, new Unexpected(input[i], `is unexpected, expected: ${expectedIndexes}`))
921
+ if (allErrors) {
922
+ es.push([stepKey++, e])
923
+ continue
924
+ } else {
925
+ return Either.left(new Composite(ast, input, e, output))
926
+ }
927
+ }
928
+ }
929
+
930
+ let i = 0
931
+ type State = {
932
+ es: typeof es
933
+ output: typeof output
934
+ }
935
+ let queue:
936
+ | Array<(_: State) => Effect.Effect<void, ParseIssue, any>>
937
+ | undefined = undefined
938
+
939
+ // ---------------------------------------------
940
+ // handle elements
941
+ // ---------------------------------------------
942
+ for (; i < elements.length; i++) {
943
+ if (len < i + 1) {
944
+ if (ast.elements[i].isOptional) {
945
+ // the input element is missing
946
+ continue
947
+ }
948
+ } else {
949
+ const parser = elements[i]
950
+ const te = parser(input[i], options)
951
+ if (isEither(te)) {
952
+ if (Either.isLeft(te)) {
953
+ // the input element is present but is not valid
954
+ const e = new Pointer(i, input, te.left)
955
+ if (allErrors) {
956
+ es.push([stepKey++, e])
957
+ continue
958
+ } else {
959
+ return Either.left(new Composite(ast, input, e, sortByIndex(output)))
960
+ }
961
+ }
962
+ output.push([stepKey++, te.right])
963
+ } else {
964
+ const nk = stepKey++
965
+ const index = i
966
+ if (!queue) {
967
+ queue = []
968
+ }
969
+ queue.push(({ es, output }: State) =>
970
+ Effect.flatMap(Effect.either(te), (t) => {
971
+ if (Either.isLeft(t)) {
972
+ // the input element is present but is not valid
973
+ const e = new Pointer(index, input, t.left)
974
+ if (allErrors) {
975
+ es.push([nk, e])
976
+ return Effect.void
977
+ } else {
978
+ return Either.left(new Composite(ast, input, e, sortByIndex(output)))
979
+ }
980
+ }
981
+ output.push([nk, t.right])
982
+ return Effect.void
983
+ })
984
+ )
985
+ }
986
+ }
987
+ }
988
+ // ---------------------------------------------
989
+ // handle rest element
990
+ // ---------------------------------------------
991
+ if (Arr.isNonEmptyReadonlyArray(rest)) {
992
+ const [head, ...tail] = rest
993
+ for (; i < len - tail.length; i++) {
994
+ const te = head(input[i], options)
995
+ if (isEither(te)) {
996
+ if (Either.isLeft(te)) {
997
+ const e = new Pointer(i, input, te.left)
998
+ if (allErrors) {
999
+ es.push([stepKey++, e])
1000
+ continue
1001
+ } else {
1002
+ return Either.left(new Composite(ast, input, e, sortByIndex(output)))
1003
+ }
1004
+ } else {
1005
+ output.push([stepKey++, te.right])
1006
+ }
1007
+ } else {
1008
+ const nk = stepKey++
1009
+ const index = i
1010
+ if (!queue) {
1011
+ queue = []
1012
+ }
1013
+ queue.push(
1014
+ ({ es, output }: State) =>
1015
+ Effect.flatMap(Effect.either(te), (t) => {
1016
+ if (Either.isLeft(t)) {
1017
+ const e = new Pointer(index, input, t.left)
1018
+ if (allErrors) {
1019
+ es.push([nk, e])
1020
+ return Effect.void
1021
+ } else {
1022
+ return Either.left(new Composite(ast, input, e, sortByIndex(output)))
1023
+ }
1024
+ } else {
1025
+ output.push([nk, t.right])
1026
+ return Effect.void
1027
+ }
1028
+ })
1029
+ )
1030
+ }
1031
+ }
1032
+ // ---------------------------------------------
1033
+ // handle post rest elements
1034
+ // ---------------------------------------------
1035
+ for (let j = 0; j < tail.length; j++) {
1036
+ i += j
1037
+ if (len < i + 1) {
1038
+ continue
1039
+ } else {
1040
+ const te = tail[j](input[i], options)
1041
+ if (isEither(te)) {
1042
+ if (Either.isLeft(te)) {
1043
+ // the input element is present but is not valid
1044
+ const e = new Pointer(i, input, te.left)
1045
+ if (allErrors) {
1046
+ es.push([stepKey++, e])
1047
+ continue
1048
+ } else {
1049
+ return Either.left(new Composite(ast, input, e, sortByIndex(output)))
1050
+ }
1051
+ }
1052
+ output.push([stepKey++, te.right])
1053
+ } else {
1054
+ const nk = stepKey++
1055
+ const index = i
1056
+ if (!queue) {
1057
+ queue = []
1058
+ }
1059
+ queue.push(
1060
+ ({ es, output }: State) =>
1061
+ Effect.flatMap(Effect.either(te), (t) => {
1062
+ if (Either.isLeft(t)) {
1063
+ // the input element is present but is not valid
1064
+ const e = new Pointer(index, input, t.left)
1065
+ if (allErrors) {
1066
+ es.push([nk, e])
1067
+ return Effect.void
1068
+ } else {
1069
+ return Either.left(new Composite(ast, input, e, sortByIndex(output)))
1070
+ }
1071
+ }
1072
+ output.push([nk, t.right])
1073
+ return Effect.void
1074
+ })
1075
+ )
1076
+ }
1077
+ }
1078
+ }
1079
+ }
1080
+
1081
+ // ---------------------------------------------
1082
+ // compute result
1083
+ // ---------------------------------------------
1084
+ const computeResult = ({ es, output }: State) =>
1085
+ Arr.isNonEmptyArray(es) ?
1086
+ Either.left(new Composite(ast, input, sortByIndex(es), sortByIndex(output))) :
1087
+ Either.right(sortByIndex(output))
1088
+ if (queue && queue.length > 0) {
1089
+ const cqueue = queue
1090
+ return Effect.suspend(() => {
1091
+ const state: State = {
1092
+ es: Arr.copy(es),
1093
+ output: Arr.copy(output)
1094
+ }
1095
+ return Effect.flatMap(
1096
+ Effect.forEach(cqueue, (f) => f(state), { concurrency, batching, discard: true }),
1097
+ () => computeResult(state)
1098
+ )
1099
+ })
1100
+ }
1101
+ return computeResult({ output, es })
1102
+ }
1103
+ }
1104
+ case "TypeLiteral": {
1105
+ if (ast.propertySignatures.length === 0 && ast.indexSignatures.length === 0) {
1106
+ return fromRefinement(ast, Predicate.isNotNullable)
1107
+ }
1108
+
1109
+ const propertySignatures: Array<readonly [Parser, AST.PropertySignature]> = []
1110
+ const expectedKeysMap: Record<PropertyKey, null> = {}
1111
+ const expectedKeys: Array<PropertyKey> = []
1112
+ for (const ps of ast.propertySignatures) {
1113
+ propertySignatures.push([goMemo(ps.type, isDecoding), ps])
1114
+ expectedKeysMap[ps.name] = null
1115
+ expectedKeys.push(ps.name)
1116
+ }
1117
+
1118
+ const indexSignatures = ast.indexSignatures.map((is) =>
1119
+ [
1120
+ goMemo(is.parameter, isDecoding),
1121
+ goMemo(is.type, isDecoding),
1122
+ is.parameter
1123
+ ] as const
1124
+ )
1125
+ const expectedAST = AST.Union.make(
1126
+ ast.indexSignatures.map((is): AST.AST => is.parameter).concat(
1127
+ expectedKeys.map((key) => Predicate.isSymbol(key) ? new AST.UniqueSymbol(key) : new AST.Literal(key))
1128
+ )
1129
+ )
1130
+ const expected = goMemo(expectedAST, isDecoding)
1131
+ const concurrency = getConcurrency(ast)
1132
+ const batching = getBatching(ast)
1133
+ return (input: unknown, options) => {
1134
+ if (!Predicate.isRecord(input)) {
1135
+ return Either.left(new Type(ast, input))
1136
+ }
1137
+ const allErrors = options?.errors === "all"
1138
+ const es: Array<[number, ParseIssue]> = []
1139
+ let stepKey = 0
1140
+
1141
+ // ---------------------------------------------
1142
+ // handle excess properties
1143
+ // ---------------------------------------------
1144
+ const onExcessPropertyError = options?.onExcessProperty === "error"
1145
+ const onExcessPropertyPreserve = options?.onExcessProperty === "preserve"
1146
+ const output: Record<PropertyKey, unknown> = {}
1147
+ let inputKeys: Array<PropertyKey> | undefined
1148
+ if (onExcessPropertyError || onExcessPropertyPreserve) {
1149
+ inputKeys = Reflect.ownKeys(input)
1150
+ for (const key of inputKeys) {
1151
+ const te = expected(key, options)
1152
+ if (isEither(te) && Either.isLeft(te)) {
1153
+ // key is unexpected
1154
+ if (onExcessPropertyError) {
1155
+ const e = new Pointer(
1156
+ key,
1157
+ input,
1158
+ new Unexpected(input[key], `is unexpected, expected: ${String(expectedAST)}`)
1159
+ )
1160
+ if (allErrors) {
1161
+ es.push([stepKey++, e])
1162
+ continue
1163
+ } else {
1164
+ return Either.left(new Composite(ast, input, e, output))
1165
+ }
1166
+ } else {
1167
+ // preserve key
1168
+ output[key] = input[key]
1169
+ }
1170
+ }
1171
+ }
1172
+ }
1173
+
1174
+ // ---------------------------------------------
1175
+ // handle property signatures
1176
+ // ---------------------------------------------
1177
+ type State = {
1178
+ es: typeof es
1179
+ output: typeof output
1180
+ }
1181
+ let queue:
1182
+ | Array<(state: State) => Effect.Effect<void, ParseIssue, any>>
1183
+ | undefined = undefined
1184
+
1185
+ const isExact = options?.exact === true
1186
+ for (let i = 0; i < propertySignatures.length; i++) {
1187
+ const ps = propertySignatures[i][1]
1188
+ const name = ps.name
1189
+ const hasKey = Object.prototype.hasOwnProperty.call(input, name)
1190
+ if (!hasKey) {
1191
+ if (ps.isOptional) {
1192
+ continue
1193
+ } else if (isExact) {
1194
+ const e = new Pointer(name, input, new Missing(ps))
1195
+ if (allErrors) {
1196
+ es.push([stepKey++, e])
1197
+ continue
1198
+ } else {
1199
+ return Either.left(new Composite(ast, input, e, output))
1200
+ }
1201
+ }
1202
+ }
1203
+ const parser = propertySignatures[i][0]
1204
+ const te = parser(input[name], options)
1205
+ if (isEither(te)) {
1206
+ if (Either.isLeft(te)) {
1207
+ const e = new Pointer(name, input, hasKey ? te.left : new Missing(ps))
1208
+ if (allErrors) {
1209
+ es.push([stepKey++, e])
1210
+ continue
1211
+ } else {
1212
+ return Either.left(new Composite(ast, input, e, output))
1213
+ }
1214
+ }
1215
+ output[name] = te.right
1216
+ } else {
1217
+ const nk = stepKey++
1218
+ const index = name
1219
+ if (!queue) {
1220
+ queue = []
1221
+ }
1222
+ queue.push(
1223
+ ({ es, output }: State) =>
1224
+ Effect.flatMap(Effect.either(te), (t) => {
1225
+ if (Either.isLeft(t)) {
1226
+ const e = new Pointer(index, input, hasKey ? t.left : new Missing(ps))
1227
+ if (allErrors) {
1228
+ es.push([nk, e])
1229
+ return Effect.void
1230
+ } else {
1231
+ return Either.left(new Composite(ast, input, e, output))
1232
+ }
1233
+ }
1234
+ output[index] = t.right
1235
+ return Effect.void
1236
+ })
1237
+ )
1238
+ }
1239
+ }
1240
+
1241
+ // ---------------------------------------------
1242
+ // handle index signatures
1243
+ // ---------------------------------------------
1244
+ for (let i = 0; i < indexSignatures.length; i++) {
1245
+ const indexSignature = indexSignatures[i]
1246
+ const parameter = indexSignature[0]
1247
+ const type = indexSignature[1]
1248
+ const keys = util_.getKeysForIndexSignature(input, indexSignature[2])
1249
+ for (const key of keys) {
1250
+ // ---------------------------------------------
1251
+ // handle keys
1252
+ // ---------------------------------------------
1253
+ const keu = parameter(key, options)
1254
+ if (isEither(keu) && Either.isRight(keu)) {
1255
+ // ---------------------------------------------
1256
+ // handle values
1257
+ // ---------------------------------------------
1258
+ const vpr = type(input[key], options)
1259
+ if (isEither(vpr)) {
1260
+ if (Either.isLeft(vpr)) {
1261
+ const e = new Pointer(key, input, vpr.left)
1262
+ if (allErrors) {
1263
+ es.push([stepKey++, e])
1264
+ continue
1265
+ } else {
1266
+ return Either.left(new Composite(ast, input, e, output))
1267
+ }
1268
+ } else {
1269
+ if (!Object.prototype.hasOwnProperty.call(expectedKeysMap, key)) {
1270
+ output[key] = vpr.right
1271
+ }
1272
+ }
1273
+ } else {
1274
+ const nk = stepKey++
1275
+ const index = key
1276
+ if (!queue) {
1277
+ queue = []
1278
+ }
1279
+ queue.push(
1280
+ ({ es, output }: State) =>
1281
+ Effect.flatMap(
1282
+ Effect.either(vpr),
1283
+ (tv) => {
1284
+ if (Either.isLeft(tv)) {
1285
+ const e = new Pointer(index, input, tv.left)
1286
+ if (allErrors) {
1287
+ es.push([nk, e])
1288
+ return Effect.void
1289
+ } else {
1290
+ return Either.left(new Composite(ast, input, e, output))
1291
+ }
1292
+ } else {
1293
+ if (!Object.prototype.hasOwnProperty.call(expectedKeysMap, key)) {
1294
+ output[key] = tv.right
1295
+ }
1296
+ return Effect.void
1297
+ }
1298
+ }
1299
+ )
1300
+ )
1301
+ }
1302
+ }
1303
+ }
1304
+ }
1305
+ // ---------------------------------------------
1306
+ // compute result
1307
+ // ---------------------------------------------
1308
+ const computeResult = ({ es, output }: State) => {
1309
+ if (Arr.isNonEmptyArray(es)) {
1310
+ return Either.left(new Composite(ast, input, sortByIndex(es), output))
1311
+ }
1312
+ if (options?.propertyOrder === "original") {
1313
+ // preserve input keys order
1314
+ const keys = inputKeys || Reflect.ownKeys(input)
1315
+ for (const name of expectedKeys) {
1316
+ if (keys.indexOf(name) === -1) {
1317
+ keys.push(name)
1318
+ }
1319
+ }
1320
+ const out: any = {}
1321
+ for (const key of keys) {
1322
+ if (Object.prototype.hasOwnProperty.call(output, key)) {
1323
+ out[key] = output[key]
1324
+ }
1325
+ }
1326
+ return Either.right(out)
1327
+ }
1328
+ return Either.right(output)
1329
+ }
1330
+ if (queue && queue.length > 0) {
1331
+ const cqueue = queue
1332
+ return Effect.suspend(() => {
1333
+ const state: State = {
1334
+ es: Arr.copy(es),
1335
+ output: Object.assign({}, output)
1336
+ }
1337
+ return Effect.flatMap(
1338
+ Effect.forEach(cqueue, (f) => f(state), { concurrency, batching, discard: true }),
1339
+ () => computeResult(state)
1340
+ )
1341
+ })
1342
+ }
1343
+ return computeResult({ es, output })
1344
+ }
1345
+ }
1346
+ case "Union": {
1347
+ const searchTree = getSearchTree(ast.types, isDecoding)
1348
+ const ownKeys = Reflect.ownKeys(searchTree.keys)
1349
+ const ownKeysLen = ownKeys.length
1350
+ const astTypesLen = ast.types.length
1351
+ const map = new Map<any, Parser>()
1352
+ for (let i = 0; i < astTypesLen; i++) {
1353
+ map.set(ast.types[i], goMemo(ast.types[i], isDecoding))
1354
+ }
1355
+ const concurrency = getConcurrency(ast) ?? 1
1356
+ const batching = getBatching(ast)
1357
+ return (input, options) => {
1358
+ const es: Array<[number, ParseIssue]> = []
1359
+ let stepKey = 0
1360
+ let candidates: Array<AST.AST> = []
1361
+ if (ownKeysLen > 0) {
1362
+ if (Predicate.isRecordOrArray(input)) {
1363
+ for (let i = 0; i < ownKeysLen; i++) {
1364
+ const name = ownKeys[i]
1365
+ const buckets = searchTree.keys[name].buckets
1366
+ // for each property that should contain a literal, check if the input contains that property
1367
+ if (Object.prototype.hasOwnProperty.call(input, name)) {
1368
+ const literal = String(input[name])
1369
+ // check that the value obtained from the input for the property corresponds to an existing bucket
1370
+ if (Object.prototype.hasOwnProperty.call(buckets, literal)) {
1371
+ // retrive the minimal set of candidates for decoding
1372
+ candidates = candidates.concat(buckets[literal])
1373
+ } else {
1374
+ const { candidates, literals } = searchTree.keys[name]
1375
+ const literalsUnion = AST.Union.make(literals)
1376
+ const errorAst = candidates.length === astTypesLen
1377
+ ? new AST.TypeLiteral([new AST.PropertySignature(name, literalsUnion, false, true)], [])
1378
+ : AST.Union.make(candidates)
1379
+ es.push([
1380
+ stepKey++,
1381
+ new Composite(errorAst, input, new Pointer(name, input, new Type(literalsUnion, input[name])))
1382
+ ])
1383
+ }
1384
+ } else {
1385
+ const { candidates, literals } = searchTree.keys[name]
1386
+ const fakePropertySignature = new AST.PropertySignature(name, AST.Union.make(literals), false, true)
1387
+ const errorAst = candidates.length === astTypesLen
1388
+ ? new AST.TypeLiteral([fakePropertySignature], [])
1389
+ : AST.Union.make(candidates)
1390
+ es.push([
1391
+ stepKey++,
1392
+ new Composite(errorAst, input, new Pointer(name, input, new Missing(fakePropertySignature)))
1393
+ ])
1394
+ }
1395
+ }
1396
+ } else {
1397
+ const errorAst = searchTree.candidates.length === astTypesLen
1398
+ ? ast
1399
+ : AST.Union.make(searchTree.candidates)
1400
+ es.push([stepKey++, new Type(errorAst, input)])
1401
+ }
1402
+ }
1403
+ if (searchTree.otherwise.length > 0) {
1404
+ candidates = candidates.concat(searchTree.otherwise)
1405
+ }
1406
+
1407
+ let queue:
1408
+ | Array<(state: State) => Effect.Effect<unknown, ParseIssue, any>>
1409
+ | undefined = undefined
1410
+
1411
+ type State = {
1412
+ finalResult?: any
1413
+ es: typeof es
1414
+ }
1415
+
1416
+ for (let i = 0; i < candidates.length; i++) {
1417
+ const candidate = candidates[i]
1418
+ const pr = map.get(candidate)!(input, options)
1419
+ // the members of a union are ordered based on which one should be decoded first,
1420
+ // therefore if one member has added a task, all subsequent members must
1421
+ // also add a task to the queue even if they are synchronous
1422
+ if (isEither(pr) && (!queue || queue.length === 0)) {
1423
+ if (Either.isRight(pr)) {
1424
+ return pr
1425
+ } else {
1426
+ es.push([stepKey++, pr.left])
1427
+ }
1428
+ } else {
1429
+ const nk = stepKey++
1430
+ if (!queue) {
1431
+ queue = []
1432
+ }
1433
+ queue.push(
1434
+ (state) =>
1435
+ Effect.suspend(() => {
1436
+ if ("finalResult" in state) {
1437
+ return Effect.void
1438
+ } else {
1439
+ return Effect.flatMap(Effect.either(pr), (t) => {
1440
+ if (Either.isRight(t)) {
1441
+ state.finalResult = t
1442
+ } else {
1443
+ state.es.push([nk, t.left])
1444
+ }
1445
+ return Effect.void
1446
+ })
1447
+ }
1448
+ })
1449
+ )
1450
+ }
1451
+ }
1452
+
1453
+ // ---------------------------------------------
1454
+ // compute result
1455
+ // ---------------------------------------------
1456
+ const computeResult = (es: State["es"]) =>
1457
+ Arr.isNonEmptyArray(es) ?
1458
+ es.length === 1 && es[0][1]._tag === "Type" ?
1459
+ Either.left(es[0][1]) :
1460
+ Either.left(new Composite(ast, input, sortByIndex(es))) :
1461
+ // this should never happen
1462
+ Either.left(new Type(ast, input))
1463
+
1464
+ if (queue && queue.length > 0) {
1465
+ const cqueue = queue
1466
+ return Effect.suspend(() => {
1467
+ const state: State = { es: Arr.copy(es) }
1468
+ return Effect.flatMap(
1469
+ Effect.forEach(cqueue, (f) => f(state), { concurrency, batching, discard: true }),
1470
+ () => {
1471
+ if ("finalResult" in state) {
1472
+ return state.finalResult
1473
+ }
1474
+ return computeResult(state.es)
1475
+ }
1476
+ )
1477
+ })
1478
+ }
1479
+ return computeResult(es)
1480
+ }
1481
+ }
1482
+ case "Suspend": {
1483
+ const get = util_.memoizeThunk(() => goMemo(ast.f(), isDecoding))
1484
+ return (a, options) => get()(a, options)
1485
+ }
1486
+ }
1487
+ }
1488
+
1489
+ const fromRefinement = <A>(ast: AST.AST, refinement: (u: unknown) => u is A): Parser => (u) =>
1490
+ refinement(u) ? Either.right(u) : Either.left(new Type(ast, u))
1491
+
1492
+ /** @internal */
1493
+ export const getLiterals = (
1494
+ ast: AST.AST,
1495
+ isDecoding: boolean
1496
+ ): ReadonlyArray<[PropertyKey, AST.Literal]> => {
1497
+ switch (ast._tag) {
1498
+ case "Declaration": {
1499
+ const annotation = AST.getSurrogateAnnotation(ast)
1500
+ if (Option.isSome(annotation)) {
1501
+ return getLiterals(annotation.value, isDecoding)
1502
+ }
1503
+ break
1504
+ }
1505
+ case "TypeLiteral": {
1506
+ const out: Array<[PropertyKey, AST.Literal]> = []
1507
+ for (let i = 0; i < ast.propertySignatures.length; i++) {
1508
+ const propertySignature = ast.propertySignatures[i]
1509
+ const type = isDecoding ? AST.encodedAST(propertySignature.type) : AST.typeAST(propertySignature.type)
1510
+ if (AST.isLiteral(type) && !propertySignature.isOptional) {
1511
+ out.push([propertySignature.name, type])
1512
+ }
1513
+ }
1514
+ return out
1515
+ }
1516
+ case "TupleType": {
1517
+ const out: Array<[PropertyKey, AST.Literal]> = []
1518
+ for (let i = 0; i < ast.elements.length; i++) {
1519
+ const element = ast.elements[i]
1520
+ const type = isDecoding ? AST.encodedAST(element.type) : AST.typeAST(element.type)
1521
+ if (AST.isLiteral(type) && !element.isOptional) {
1522
+ out.push([i, type])
1523
+ }
1524
+ }
1525
+ return out
1526
+ }
1527
+ case "Refinement":
1528
+ return getLiterals(ast.from, isDecoding)
1529
+ case "Suspend":
1530
+ return getLiterals(ast.f(), isDecoding)
1531
+ case "Transformation":
1532
+ return getLiterals(isDecoding ? ast.from : ast.to, isDecoding)
1533
+ }
1534
+ return []
1535
+ }
1536
+
1537
+ /**
1538
+ * The purpose of the algorithm is to narrow down the pool of possible
1539
+ * candidates for decoding as much as possible.
1540
+ *
1541
+ * This function separates the schemas into two groups, `keys` and `otherwise`:
1542
+ *
1543
+ * - `keys`: the schema has at least one property with a literal value
1544
+ * - `otherwise`: the schema has no properties with a literal value
1545
+ *
1546
+ * If a schema has at least one property with a literal value, so it ends up in
1547
+ * `keys`, first a namespace is created for the name of the property containing
1548
+ * the literal, and then within this namespace a "bucket" is created for the
1549
+ * literal value in which to store all the schemas that have the same property
1550
+ * and literal value.
1551
+ *
1552
+ * @internal
1553
+ */
1554
+ export const getSearchTree = (
1555
+ members: ReadonlyArray<AST.AST>,
1556
+ isDecoding: boolean
1557
+ ): {
1558
+ keys: {
1559
+ readonly [key: PropertyKey]: {
1560
+ buckets: { [literal: string]: ReadonlyArray<AST.AST> }
1561
+ literals: ReadonlyArray<AST.Literal> // this is for error messages
1562
+ candidates: ReadonlyArray<AST.AST>
1563
+ }
1564
+ }
1565
+ otherwise: ReadonlyArray<AST.AST>
1566
+ candidates: ReadonlyArray<AST.AST>
1567
+ } => {
1568
+ const keys: {
1569
+ [key: PropertyKey]: {
1570
+ buckets: { [literal: string]: Array<AST.AST> }
1571
+ literals: Array<AST.Literal>
1572
+ candidates: Array<AST.AST>
1573
+ }
1574
+ } = {}
1575
+ const otherwise: Array<AST.AST> = []
1576
+ const candidates: Array<AST.AST> = []
1577
+ for (let i = 0; i < members.length; i++) {
1578
+ const member = members[i]
1579
+ const tags = getLiterals(member, isDecoding)
1580
+ if (tags.length > 0) {
1581
+ candidates.push(member)
1582
+ for (let j = 0; j < tags.length; j++) {
1583
+ const [key, literal] = tags[j]
1584
+ const hash = String(literal.literal)
1585
+ keys[key] = keys[key] || { buckets: {}, literals: [], candidates: [] }
1586
+ const buckets = keys[key].buckets
1587
+ if (Object.prototype.hasOwnProperty.call(buckets, hash)) {
1588
+ if (j < tags.length - 1) {
1589
+ continue
1590
+ }
1591
+ buckets[hash].push(member)
1592
+ keys[key].literals.push(literal)
1593
+ keys[key].candidates.push(member)
1594
+ } else {
1595
+ buckets[hash] = [member]
1596
+ keys[key].literals.push(literal)
1597
+ keys[key].candidates.push(member)
1598
+ break
1599
+ }
1600
+ }
1601
+ } else {
1602
+ otherwise.push(member)
1603
+ }
1604
+ }
1605
+ return { keys, otherwise, candidates }
1606
+ }
1607
+
1608
+ const dropRightRefinement = (ast: AST.AST): AST.AST => AST.isRefinement(ast) ? dropRightRefinement(ast.from) : ast
1609
+
1610
+ const handleForbidden = <A, R>(
1611
+ effect: Effect.Effect<A, ParseIssue, R>,
1612
+ ast: AST.AST,
1613
+ actual: unknown,
1614
+ options: InternalOptions | undefined
1615
+ ): Effect.Effect<A, ParseIssue, R> => {
1616
+ // If effects are allowed, return the original effect
1617
+ if (options?.isEffectAllowed === true) {
1618
+ return effect
1619
+ }
1620
+
1621
+ // If the effect is already an Either, return it directly
1622
+ if (isEither(effect)) {
1623
+ return effect
1624
+ }
1625
+
1626
+ // Otherwise, attempt to execute the effect synchronously
1627
+ const scheduler = new Scheduler.SyncScheduler()
1628
+ const fiber = Effect.runFork(effect as Effect.Effect<A, ParseIssue>, { scheduler })
1629
+ scheduler.flush()
1630
+ const exit = fiber.unsafePoll()
1631
+
1632
+ if (exit) {
1633
+ if (Exit.isSuccess(exit)) {
1634
+ // If the effect successfully resolves, wrap the value in a Right
1635
+ return Either.right(exit.value)
1636
+ }
1637
+ const cause = exit.cause
1638
+ if (Cause.isFailType(cause)) {
1639
+ // The effect executed synchronously but failed due to a ParseIssue
1640
+ return Either.left(cause.error)
1641
+ }
1642
+ // The effect executed synchronously but failed due to a defect (e.g., a missing dependency)
1643
+ return Either.left(new Forbidden(ast, actual, Cause.pretty(cause)))
1644
+ }
1645
+
1646
+ // The effect could not be resolved synchronously, meaning it performs async work
1647
+ return Either.left(
1648
+ new Forbidden(
1649
+ ast,
1650
+ actual,
1651
+ "cannot be be resolved synchronously, this is caused by using runSync on an effect that performs async work"
1652
+ )
1653
+ )
1654
+ }
1655
+
1656
+ const compare = ([a]: [number, ...Array<unknown>], [b]: [number, ...Array<unknown>]) => a > b ? 1 : a < b ? -1 : 0
1657
+
1658
+ function sortByIndex<T>(
1659
+ es: Arr.NonEmptyArray<[number, T]>
1660
+ ): Arr.NonEmptyArray<T>
1661
+ function sortByIndex<T>(es: Array<[number, T]>): Array<T>
1662
+ function sortByIndex(es: Array<[number, any]>) {
1663
+ return es.sort(compare).map((t) => t[1])
1664
+ }
1665
+
1666
+ // -------------------------------------------------------------------------------------
1667
+ // transformations interpreter
1668
+ // -------------------------------------------------------------------------------------
1669
+
1670
+ /** @internal */
1671
+ export const getFinalTransformation = (
1672
+ transformation: AST.TransformationKind,
1673
+ isDecoding: boolean
1674
+ ): (
1675
+ fromA: any,
1676
+ options: AST.ParseOptions,
1677
+ self: AST.Transformation,
1678
+ fromI: any
1679
+ ) => Effect.Effect<any, ParseIssue, any> => {
1680
+ switch (transformation._tag) {
1681
+ case "FinalTransformation":
1682
+ return isDecoding ? transformation.decode : transformation.encode
1683
+ case "ComposeTransformation":
1684
+ return Either.right
1685
+ case "TypeLiteralTransformation":
1686
+ return (input) => {
1687
+ let out: Effect.Effect<any, ParseIssue, any> = Either.right(input)
1688
+
1689
+ // ---------------------------------------------
1690
+ // handle property signature transformations
1691
+ // ---------------------------------------------
1692
+ for (const pst of transformation.propertySignatureTransformations) {
1693
+ const [from, to] = isDecoding ?
1694
+ [pst.from, pst.to] :
1695
+ [pst.to, pst.from]
1696
+ const transformation = isDecoding ? pst.decode : pst.encode
1697
+ const f = (input: any) => {
1698
+ const o = transformation(
1699
+ Object.prototype.hasOwnProperty.call(input, from) ?
1700
+ Option.some(input[from]) :
1701
+ Option.none()
1702
+ )
1703
+ delete input[from]
1704
+ if (Option.isSome(o)) {
1705
+ input[to] = o.value
1706
+ }
1707
+ return input
1708
+ }
1709
+ out = map(out, f)
1710
+ }
1711
+ return out
1712
+ }
1713
+ }
1714
+ }
1715
+
1716
+ // ----------------
1717
+ // Formatters
1718
+ // ----------------
1719
+
1720
+ interface Forest<A> extends ReadonlyArray<Tree<A>> {}
1721
+
1722
+ interface Tree<A> {
1723
+ readonly value: A
1724
+ readonly forest: Forest<A>
1725
+ }
1726
+
1727
+ const makeTree = <A>(value: A, forest: Forest<A> = []): Tree<A> => ({
1728
+ value,
1729
+ forest
1730
+ })
1731
+
1732
+ /**
1733
+ * @category formatting
1734
+ * @since 3.10.0
1735
+ */
1736
+ export interface ParseResultFormatter<A> {
1737
+ readonly formatIssue: (issue: ParseIssue) => Effect.Effect<A>
1738
+ readonly formatIssueSync: (issue: ParseIssue) => A
1739
+ readonly formatError: (error: ParseError) => Effect.Effect<A>
1740
+ readonly formatErrorSync: (error: ParseError) => A
1741
+ }
1742
+
1743
+ /**
1744
+ * @category formatting
1745
+ * @since 3.10.0
1746
+ */
1747
+ export const TreeFormatter: ParseResultFormatter<string> = {
1748
+ formatIssue: (issue) => map(formatTree(issue), drawTree),
1749
+ formatIssueSync: (issue) => {
1750
+ const e = TreeFormatter.formatIssue(issue)
1751
+ return isEither(e) ? Either.getOrThrow(e) : Effect.runSync(e)
1752
+ },
1753
+ formatError: (error) => TreeFormatter.formatIssue(error.issue),
1754
+ formatErrorSync: (error) => TreeFormatter.formatIssueSync(error.issue)
1755
+ }
1756
+
1757
+ const drawTree = (tree: Tree<string>): string => tree.value + draw("\n", tree.forest)
1758
+
1759
+ const draw = (indentation: string, forest: Forest<string>): string => {
1760
+ let r = ""
1761
+ const len = forest.length
1762
+ let tree: Tree<string>
1763
+ for (let i = 0; i < len; i++) {
1764
+ tree = forest[i]
1765
+ const isLast = i === len - 1
1766
+ r += indentation + (isLast ? "└" : "├") + "─ " + tree.value
1767
+ r += draw(indentation + (len > 1 && !isLast ? "│ " : " "), tree.forest)
1768
+ }
1769
+ return r
1770
+ }
1771
+
1772
+ const formatTransformationKind = (kind: Transformation["kind"]): string => {
1773
+ switch (kind) {
1774
+ case "Encoded":
1775
+ return "Encoded side transformation failure"
1776
+ case "Transformation":
1777
+ return "Transformation process failure"
1778
+ case "Type":
1779
+ return "Type side transformation failure"
1780
+ }
1781
+ }
1782
+
1783
+ const formatRefinementKind = (kind: Refinement["kind"]): string => {
1784
+ switch (kind) {
1785
+ case "From":
1786
+ return "From side refinement failure"
1787
+ case "Predicate":
1788
+ return "Predicate refinement failure"
1789
+ }
1790
+ }
1791
+
1792
+ const getAnnotated = (issue: ParseIssue): Option.Option<AST.Annotated> =>
1793
+ "ast" in issue ? Option.some(issue.ast) : Option.none()
1794
+
1795
+ interface CurrentMessage {
1796
+ readonly message: string
1797
+ readonly override: boolean
1798
+ }
1799
+
1800
+ // TODO: replace with Either.void when 3.13 lands
1801
+ const Either_void = Either.right(undefined)
1802
+
1803
+ const getCurrentMessage = (issue: ParseIssue): Effect.Effect<CurrentMessage | undefined> =>
1804
+ getAnnotated(issue).pipe(
1805
+ Option.flatMap(AST.getMessageAnnotation),
1806
+ Option.match({
1807
+ onNone: () => Either_void,
1808
+ onSome: (messageAnnotation) => {
1809
+ const union = messageAnnotation(issue)
1810
+ if (Predicate.isString(union)) {
1811
+ return Either.right({ message: union, override: false })
1812
+ }
1813
+ if (Effect.isEffect(union)) {
1814
+ return Effect.map(union, (message) => ({ message, override: false }))
1815
+ }
1816
+ if (Predicate.isString(union.message)) {
1817
+ return Either.right({ message: union.message, override: union.override })
1818
+ }
1819
+ return Effect.map(union.message, (message) => ({ message, override: union.override }))
1820
+ }
1821
+ })
1822
+ )
1823
+
1824
+ const createParseIssueGuard =
1825
+ <T extends ParseIssue["_tag"]>(tag: T) => (issue: ParseIssue): issue is Extract<ParseIssue, { _tag: T }> =>
1826
+ issue._tag === tag
1827
+
1828
+ /**
1829
+ * Returns `true` if the value is a `Composite`.
1830
+ *
1831
+ * @category guards
1832
+ * @since 3.10.0
1833
+ */
1834
+ export const isComposite = createParseIssueGuard("Composite")
1835
+
1836
+ const isRefinement = createParseIssueGuard("Refinement")
1837
+ const isTransformation = createParseIssueGuard("Transformation")
1838
+
1839
+ const getMessage = (issue: ParseIssue): Effect.Effect<string | undefined> =>
1840
+ flatMap(getCurrentMessage(issue), (currentMessage) => {
1841
+ if (currentMessage !== undefined) {
1842
+ const useInnerMessage = !currentMessage.override && (
1843
+ isComposite(issue) ||
1844
+ (isRefinement(issue) && issue.kind === "From") ||
1845
+ (isTransformation(issue) && issue.kind !== "Transformation")
1846
+ )
1847
+ return useInnerMessage
1848
+ ? isTransformation(issue) || isRefinement(issue) ? getMessage(issue.issue) : Either_void
1849
+ : Either.right(currentMessage.message)
1850
+ }
1851
+ return Either_void
1852
+ })
1853
+
1854
+ const getParseIssueTitleAnnotation = (issue: ParseIssue): string | undefined =>
1855
+ getAnnotated(issue).pipe(
1856
+ Option.flatMap(AST.getParseIssueTitleAnnotation),
1857
+ Option.flatMapNullable((annotation) => annotation(issue)),
1858
+ Option.getOrUndefined
1859
+ )
1860
+
1861
+ /** @internal */
1862
+ export function getRefinementExpected(ast: AST.Refinement): string {
1863
+ return AST.getDescriptionAnnotation(ast).pipe(
1864
+ Option.orElse(() => AST.getTitleAnnotation(ast)),
1865
+ Option.orElse(() => AST.getAutoTitleAnnotation(ast)),
1866
+ Option.orElse(() => AST.getIdentifierAnnotation(ast)),
1867
+ Option.getOrElse(() => `{ ${ast.from} | filter }`)
1868
+ )
1869
+ }
1870
+
1871
+ function getDefaultTypeMessage(issue: Type): string {
1872
+ if (issue.message !== undefined) {
1873
+ return issue.message
1874
+ }
1875
+ const expected = AST.isRefinement(issue.ast) ? getRefinementExpected(issue.ast) : String(issue.ast)
1876
+ return `Expected ${expected}, actual ${Inspectable.formatUnknown(issue.actual)}`
1877
+ }
1878
+
1879
+ const formatTypeMessage = (issue: Type): Effect.Effect<string> =>
1880
+ map(
1881
+ getMessage(issue),
1882
+ (message) => message ?? getParseIssueTitleAnnotation(issue) ?? getDefaultTypeMessage(issue)
1883
+ )
1884
+
1885
+ const getParseIssueTitle = (
1886
+ issue: Forbidden | Transformation | Refinement | Composite
1887
+ ): string => getParseIssueTitleAnnotation(issue) ?? String(issue.ast)
1888
+
1889
+ const formatForbiddenMessage = (issue: Forbidden): string => issue.message ?? "is forbidden"
1890
+
1891
+ const formatUnexpectedMessage = (issue: Unexpected): string => issue.message ?? "is unexpected"
1892
+
1893
+ const formatMissingMessage = (issue: Missing): Effect.Effect<string> => {
1894
+ const missingMessageAnnotation = AST.getMissingMessageAnnotation(issue.ast)
1895
+ if (Option.isSome(missingMessageAnnotation)) {
1896
+ const annotation = missingMessageAnnotation.value()
1897
+ return Predicate.isString(annotation) ? Either.right(annotation) : annotation
1898
+ }
1899
+ return Either.right(issue.message ?? "is missing")
1900
+ }
1901
+
1902
+ const formatTree = (issue: ParseIssue): Effect.Effect<Tree<string>> => {
1903
+ switch (issue._tag) {
1904
+ case "Type":
1905
+ return map(formatTypeMessage(issue), makeTree)
1906
+ case "Forbidden":
1907
+ return Either.right(makeTree(getParseIssueTitle(issue), [makeTree(formatForbiddenMessage(issue))]))
1908
+ case "Unexpected":
1909
+ return Either.right(makeTree(formatUnexpectedMessage(issue)))
1910
+ case "Missing":
1911
+ return map(formatMissingMessage(issue), makeTree)
1912
+ case "Transformation":
1913
+ return flatMap(getMessage(issue), (message) => {
1914
+ if (message !== undefined) {
1915
+ return Either.right(makeTree(message))
1916
+ }
1917
+ return map(
1918
+ formatTree(issue.issue),
1919
+ (tree) => makeTree(getParseIssueTitle(issue), [makeTree(formatTransformationKind(issue.kind), [tree])])
1920
+ )
1921
+ })
1922
+ case "Refinement":
1923
+ return flatMap(getMessage(issue), (message) => {
1924
+ if (message !== undefined) {
1925
+ return Either.right(makeTree(message))
1926
+ }
1927
+ return map(
1928
+ formatTree(issue.issue),
1929
+ (tree) => makeTree(getParseIssueTitle(issue), [makeTree(formatRefinementKind(issue.kind), [tree])])
1930
+ )
1931
+ })
1932
+ case "Pointer":
1933
+ return map(formatTree(issue.issue), (tree) => makeTree(util_.formatPath(issue.path), [tree]))
1934
+ case "Composite":
1935
+ return flatMap(getMessage(issue), (message) => {
1936
+ if (message !== undefined) {
1937
+ return Either.right(makeTree(message))
1938
+ }
1939
+ const parseIssueTitle = getParseIssueTitle(issue)
1940
+ return util_.isNonEmpty(issue.issues)
1941
+ ? map(Effect.forEach(issue.issues, formatTree), (forest) => makeTree(parseIssueTitle, forest))
1942
+ : map(formatTree(issue.issues), (tree) => makeTree(parseIssueTitle, [tree]))
1943
+ })
1944
+ }
1945
+ }
1946
+
1947
+ /**
1948
+ * Represents an issue returned by the {@link ArrayFormatter} formatter.
1949
+ *
1950
+ * @category model
1951
+ * @since 3.10.0
1952
+ */
1953
+ export interface ArrayFormatterIssue {
1954
+ /**
1955
+ * The tag identifying the type of parse issue.
1956
+ */
1957
+ readonly _tag: ParseIssue["_tag"]
1958
+
1959
+ /**
1960
+ * The path to the property where the issue occurred.
1961
+ */
1962
+ readonly path: ReadonlyArray<PropertyKey>
1963
+
1964
+ /**
1965
+ * A descriptive message explaining the issue.
1966
+ */
1967
+ readonly message: string
1968
+ }
1969
+
1970
+ const makeArrayFormatterIssue = (
1971
+ _tag: ArrayFormatterIssue["_tag"],
1972
+ path: ArrayFormatterIssue["path"],
1973
+ message: ArrayFormatterIssue["message"]
1974
+ ): ArrayFormatterIssue => ({ _tag, path, message })
1975
+
1976
+ /**
1977
+ * @category formatting
1978
+ * @since 3.10.0
1979
+ */
1980
+ export const ArrayFormatter: ParseResultFormatter<Array<ArrayFormatterIssue>> = {
1981
+ formatIssue: (issue) => getArrayFormatterIssues(issue, undefined, []),
1982
+ formatIssueSync: (issue) => {
1983
+ const e = ArrayFormatter.formatIssue(issue)
1984
+ return isEither(e) ? Either.getOrThrow(e) : Effect.runSync(e)
1985
+ },
1986
+ formatError: (error) => ArrayFormatter.formatIssue(error.issue),
1987
+ formatErrorSync: (error) => ArrayFormatter.formatIssueSync(error.issue)
1988
+ }
1989
+
1990
+ const getArrayFormatterIssues = (
1991
+ issue: ParseIssue,
1992
+ parentTag: ArrayFormatterIssue["_tag"] | undefined,
1993
+ path: ReadonlyArray<PropertyKey>
1994
+ ): Effect.Effect<Array<ArrayFormatterIssue>> => {
1995
+ const _tag = issue._tag
1996
+ switch (_tag) {
1997
+ case "Type":
1998
+ return map(formatTypeMessage(issue), (message) => [makeArrayFormatterIssue(parentTag ?? _tag, path, message)])
1999
+ case "Forbidden":
2000
+ return Either.right([makeArrayFormatterIssue(_tag, path, formatForbiddenMessage(issue))])
2001
+ case "Unexpected":
2002
+ return Either.right([makeArrayFormatterIssue(_tag, path, formatUnexpectedMessage(issue))])
2003
+ case "Missing":
2004
+ return map(formatMissingMessage(issue), (message) => [makeArrayFormatterIssue(_tag, path, message)])
2005
+ case "Pointer":
2006
+ return getArrayFormatterIssues(issue.issue, undefined, path.concat(issue.path))
2007
+ case "Composite":
2008
+ return flatMap(getMessage(issue), (message) => {
2009
+ if (message !== undefined) {
2010
+ return Either.right([makeArrayFormatterIssue(_tag, path, message)])
2011
+ }
2012
+ return util_.isNonEmpty(issue.issues)
2013
+ ? map(Effect.forEach(issue.issues, (issue) => getArrayFormatterIssues(issue, undefined, path)), Arr.flatten)
2014
+ : getArrayFormatterIssues(issue.issues, undefined, path)
2015
+ })
2016
+ case "Refinement":
2017
+ return flatMap(getMessage(issue), (message) => {
2018
+ if (message !== undefined) {
2019
+ return Either.right([makeArrayFormatterIssue(_tag, path, message)])
2020
+ }
2021
+ return getArrayFormatterIssues(issue.issue, issue.kind === "Predicate" ? _tag : undefined, path)
2022
+ })
2023
+ case "Transformation":
2024
+ return flatMap(getMessage(issue), (message) => {
2025
+ if (message !== undefined) {
2026
+ return Either.right([makeArrayFormatterIssue(_tag, path, message)])
2027
+ }
2028
+ return getArrayFormatterIssues(issue.issue, issue.kind === "Transformation" ? _tag : undefined, path)
2029
+ })
2030
+ }
2031
+ }