siderust-js 0.1.0

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 (272) hide show
  1. package/.github/workflows/ci.yml +166 -0
  2. package/.gitmodules +9 -0
  3. package/CHANGELOG.md +26 -0
  4. package/LICENSE +661 -0
  5. package/README.md +138 -0
  6. package/package.json +12 -0
  7. package/qtty-js/.github/workflows/ci.yml +151 -0
  8. package/qtty-js/.gitmodules +3 -0
  9. package/qtty-js/CHANGELOG.md +31 -0
  10. package/qtty-js/LICENSE +661 -0
  11. package/qtty-js/README.md +132 -0
  12. package/qtty-js/package.json +20 -0
  13. package/qtty-js/qtty/.github/workflows/ci.yml +155 -0
  14. package/qtty-js/qtty/CHANGELOG.md +120 -0
  15. package/qtty-js/qtty/Cargo.lock +1462 -0
  16. package/qtty-js/qtty/Cargo.toml +12 -0
  17. package/qtty-js/qtty/LICENSE +661 -0
  18. package/qtty-js/qtty/README.md +9 -0
  19. package/qtty-js/qtty/qtty/Cargo.toml +41 -0
  20. package/qtty-js/qtty/qtty/README.md +8 -0
  21. package/qtty-js/qtty/qtty/examples/angles.rs +14 -0
  22. package/qtty-js/qtty/qtty/examples/astronomy.rs +17 -0
  23. package/qtty-js/qtty/qtty/examples/dimensional_arithmetic.rs +83 -0
  24. package/qtty-js/qtty/qtty/examples/python_integration.rs +61 -0
  25. package/qtty-js/qtty/qtty/examples/quickstart.rs +15 -0
  26. package/qtty-js/qtty/qtty/examples/ratios.rs +12 -0
  27. package/qtty-js/qtty/qtty/examples/serde_with_unit.rs +234 -0
  28. package/qtty-js/qtty/qtty/examples/serialization.rs +141 -0
  29. package/qtty-js/qtty/qtty/examples/serialization_advanced.rs +155 -0
  30. package/qtty-js/qtty/qtty/src/f32.rs +108 -0
  31. package/qtty-js/qtty/qtty/src/f64.rs +30 -0
  32. package/qtty-js/qtty/qtty/src/i128.rs +111 -0
  33. package/qtty-js/qtty/qtty/src/i16.rs +111 -0
  34. package/qtty-js/qtty/qtty/src/i32.rs +111 -0
  35. package/qtty-js/qtty/qtty/src/i64.rs +111 -0
  36. package/qtty-js/qtty/qtty/src/i8.rs +111 -0
  37. package/qtty-js/qtty/qtty/src/lib.rs +238 -0
  38. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std/Cargo.lock +83 -0
  39. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std/Cargo.toml +10 -0
  40. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std/src/lib.rs +7 -0
  41. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std-alloc/Cargo.lock +83 -0
  42. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std-alloc/Cargo.toml +10 -0
  43. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std-alloc/src/lib.rs +7 -0
  44. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-std/Cargo.lock +83 -0
  45. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-std/Cargo.toml +10 -0
  46. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-std/src/lib.rs +5 -0
  47. package/qtty-js/qtty/qtty/tests/integration_tests.rs +529 -0
  48. package/qtty-js/qtty/qtty/tests/qtty_vec_feature_matrix.rs +58 -0
  49. package/qtty-js/qtty/qtty-core/Cargo.toml +41 -0
  50. package/qtty-js/qtty/qtty-core/README.md +8 -0
  51. package/qtty-js/qtty/qtty-core/examples/diesel_integration.rs +145 -0
  52. package/qtty-js/qtty/qtty-core/examples/quantity_db_serde.rs +215 -0
  53. package/qtty-js/qtty/qtty-core/src/dimension.rs +249 -0
  54. package/qtty-js/qtty/qtty-core/src/feature_diesel.rs +318 -0
  55. package/qtty-js/qtty/qtty-core/src/feature_pyo3.rs +27 -0
  56. package/qtty-js/qtty/qtty-core/src/feature_serde.rs +203 -0
  57. package/qtty-js/qtty/qtty-core/src/feature_tiberius.rs +28 -0
  58. package/qtty-js/qtty/qtty-core/src/lib.rs +744 -0
  59. package/qtty-js/qtty/qtty-core/src/macros.rs +93 -0
  60. package/qtty-js/qtty/qtty-core/src/quantity.rs +810 -0
  61. package/qtty-js/qtty/qtty-core/src/scalar.rs +1742 -0
  62. package/qtty-js/qtty/qtty-core/src/unit.rs +332 -0
  63. package/qtty-js/qtty/qtty-core/src/units/angular.rs +1228 -0
  64. package/qtty-js/qtty/qtty-core/src/units/area.rs +243 -0
  65. package/qtty-js/qtty/qtty-core/src/units/frequency.rs +179 -0
  66. package/qtty-js/qtty/qtty-core/src/units/length.rs +1270 -0
  67. package/qtty-js/qtty/qtty-core/src/units/mass.rs +488 -0
  68. package/qtty-js/qtty/qtty-core/src/units/mod.rs +26 -0
  69. package/qtty-js/qtty/qtty-core/src/units/power.rs +324 -0
  70. package/qtty-js/qtty/qtty-core/src/units/time.rs +667 -0
  71. package/qtty-js/qtty/qtty-core/src/units/unitless.rs +212 -0
  72. package/qtty-js/qtty/qtty-core/src/units/velocity.rs +210 -0
  73. package/qtty-js/qtty/qtty-core/src/units/volume.rs +269 -0
  74. package/qtty-js/qtty/qtty-core/tests/core.rs +628 -0
  75. package/qtty-js/qtty/qtty-core/tests/diesel.rs +461 -0
  76. package/qtty-js/qtty/qtty-core/tests/integers.rs +632 -0
  77. package/qtty-js/qtty/qtty-core/tests/no_cross_unit_ops.rs +35 -0
  78. package/qtty-js/qtty/qtty-core/tests/pyo3.rs +334 -0
  79. package/qtty-js/qtty/qtty-core/tests/quantity_f32.rs +276 -0
  80. package/qtty-js/qtty/qtty-core/tests/scalar_decimal.rs +258 -0
  81. package/qtty-js/qtty/qtty-core/tests/scalar_f32.rs +286 -0
  82. package/qtty-js/qtty/qtty-core/tests/scalar_f64_real.rs +287 -0
  83. package/qtty-js/qtty/qtty-core/tests/scalar_rational.rs +260 -0
  84. package/qtty-js/qtty/qtty-core/tests/serde.rs +256 -0
  85. package/qtty-js/qtty/qtty-core/tests/tiberius.rs +208 -0
  86. package/qtty-js/qtty/qtty-derive/Cargo.toml +23 -0
  87. package/qtty-js/qtty/qtty-derive/README.md +8 -0
  88. package/qtty-js/qtty/qtty-derive/src/lib.rs +340 -0
  89. package/qtty-js/qtty/qtty-ffi/ARCHITECTURE.md +3 -0
  90. package/qtty-js/qtty/qtty-ffi/Cargo.toml +31 -0
  91. package/qtty-js/qtty/qtty-ffi/README.md +9 -0
  92. package/qtty-js/qtty/qtty-ffi/build.rs +326 -0
  93. package/qtty-js/qtty/qtty-ffi/cbindgen.toml +105 -0
  94. package/qtty-js/qtty/qtty-ffi/include/qtty_ffi.h +1126 -0
  95. package/qtty-js/qtty/qtty-ffi/src/ffi.rs +1251 -0
  96. package/qtty-js/qtty/qtty-ffi/src/ffi_serde.rs +294 -0
  97. package/qtty-js/qtty/qtty-ffi/src/helpers.rs +310 -0
  98. package/qtty-js/qtty/qtty-ffi/src/lib.rs +229 -0
  99. package/qtty-js/qtty/qtty-ffi/src/macros.rs +121 -0
  100. package/qtty-js/qtty/qtty-ffi/src/registry.rs +274 -0
  101. package/qtty-js/qtty/qtty-ffi/src/types.rs +620 -0
  102. package/qtty-js/qtty/qtty-ffi/tests/integration_tests.rs +842 -0
  103. package/qtty-js/qtty/qtty-ffi/units.csv +156 -0
  104. package/qtty-js/qtty/qtty-ffi/units.csv.md +3 -0
  105. package/qtty-js/qtty-node/.prettierignore +6 -0
  106. package/qtty-js/qtty-node/.prettierrc.json +6 -0
  107. package/qtty-js/qtty-node/README.md +250 -0
  108. package/qtty-js/qtty-node/c8.config.json +11 -0
  109. package/qtty-js/qtty-node/eslint.config.js +31 -0
  110. package/qtty-js/qtty-node/examples/arithmetic.mjs +64 -0
  111. package/qtty-js/qtty-node/examples/astronomy.mjs +90 -0
  112. package/qtty-js/qtty-node/examples/quickstart.mjs +36 -0
  113. package/qtty-js/qtty-node/examples/serialization.mjs +125 -0
  114. package/qtty-js/qtty-node/examples/unit_factories.mjs +74 -0
  115. package/qtty-js/qtty-node/index.d.ts +219 -0
  116. package/qtty-js/qtty-node/index.js +323 -0
  117. package/qtty-js/qtty-node/lib/DerivedQuantity.js +122 -0
  118. package/qtty-js/qtty-node/lib/Quantity.js +151 -0
  119. package/qtty-js/qtty-node/lib/backend.js +25 -0
  120. package/qtty-js/qtty-node/native.cjs +306 -0
  121. package/qtty-js/qtty-node/package-lock.json +3223 -0
  122. package/qtty-js/qtty-node/package.json +70 -0
  123. package/qtty-js/qtty-node/units.d.ts +299 -0
  124. package/qtty-js/qtty-node/units.js +210 -0
  125. package/qtty-js/qtty-web/Cargo.lock +767 -0
  126. package/qtty-js/qtty-web/Cargo.toml +21 -0
  127. package/qtty-js/qtty-web/index.d.ts +140 -0
  128. package/qtty-js/qtty-web/index.js +20 -0
  129. package/qtty-js/qtty-web/lib/DerivedQuantity.js +58 -0
  130. package/qtty-js/qtty-web/lib/Quantity.js +75 -0
  131. package/qtty-js/qtty-web/lib/backend.js +80 -0
  132. package/qtty-js/qtty-web/package.json +45 -0
  133. package/qtty-js/qtty-web/src/lib.rs +111 -0
  134. package/qtty-js/scripts/ci.sh +73 -0
  135. package/scripts/ci.sh +123 -0
  136. package/siderust-core/Cargo.lock +787 -0
  137. package/siderust-core/Cargo.toml +18 -0
  138. package/siderust-core/DEDUPLICATION.md +124 -0
  139. package/siderust-core/src/body.rs +120 -0
  140. package/siderust-core/src/events.rs +184 -0
  141. package/siderust-core/src/lib.rs +20 -0
  142. package/siderust-core/src/observer.rs +55 -0
  143. package/siderust-core/src/position.rs +213 -0
  144. package/siderust-node/.prettierignore +7 -0
  145. package/siderust-node/.prettierrc.json +6 -0
  146. package/siderust-node/Cargo.lock +906 -0
  147. package/siderust-node/Cargo.toml +29 -0
  148. package/siderust-node/README.md +109 -0
  149. package/siderust-node/__test__/index.test.mjs +248 -0
  150. package/siderust-node/build.rs +5 -0
  151. package/siderust-node/c8.config.json +3 -0
  152. package/siderust-node/eslint.config.js +31 -0
  153. package/siderust-node/examples/01_basic_coordinates.mjs +24 -0
  154. package/siderust-node/examples/02_coordinate_transformations.mjs +25 -0
  155. package/siderust-node/examples/03_all_frames_conversions.mjs +26 -0
  156. package/siderust-node/examples/04_all_center_conversions.mjs +24 -0
  157. package/siderust-node/examples/05_target_tracking.mjs +22 -0
  158. package/siderust-node/examples/06_night_events.mjs +18 -0
  159. package/siderust-node/examples/07_moon_properties.mjs +21 -0
  160. package/siderust-node/examples/08_solar_system.mjs +19 -0
  161. package/siderust-node/examples/09_star_observability.mjs +22 -0
  162. package/siderust-node/examples/10_time_periods.mjs +9 -0
  163. package/siderust-node/examples/11_serialization.mjs +31 -0
  164. package/siderust-node/examples/12_runtime_ephemeris.mjs +27 -0
  165. package/siderust-node/examples/13_coordinate_operations.mjs +20 -0
  166. package/siderust-node/index.d.ts +623 -0
  167. package/siderust-node/index.js +79 -0
  168. package/siderust-node/lib/Observer.js +112 -0
  169. package/siderust-node/lib/Star.js +118 -0
  170. package/siderust-node/lib/backend.js +63 -0
  171. package/siderust-node/lib/wrappers.js +566 -0
  172. package/siderust-node/main.js +20 -0
  173. package/siderust-node/native.cjs +360 -0
  174. package/siderust-node/package-lock.json +3261 -0
  175. package/siderust-node/package.json +71 -0
  176. package/siderust-node/src/body.rs +74 -0
  177. package/siderust-node/src/coordinates.rs +372 -0
  178. package/siderust-node/src/ephemeris.rs +462 -0
  179. package/siderust-node/src/events.rs +577 -0
  180. package/siderust-node/src/lib.rs +43 -0
  181. package/siderust-node/src/observer.rs +132 -0
  182. package/siderust-node/src/phase.rs +218 -0
  183. package/siderust-node/src/position.rs +292 -0
  184. package/siderust-node/src/star.rs +200 -0
  185. package/siderust-web/Cargo.lock +855 -0
  186. package/siderust-web/Cargo.toml +34 -0
  187. package/siderust-web/README.md +100 -0
  188. package/siderust-web/__test__/index.test.mjs +118 -0
  189. package/siderust-web/examples/github-pages/README.md +31 -0
  190. package/siderust-web/examples/github-pages/index.html +135 -0
  191. package/siderust-web/index.d.ts +311 -0
  192. package/siderust-web/index.js +66 -0
  193. package/siderust-web/lib/Observer.js +103 -0
  194. package/siderust-web/lib/Star.js +116 -0
  195. package/siderust-web/lib/backend.js +400 -0
  196. package/siderust-web/lib/wrappers.js +512 -0
  197. package/siderust-web/package.json +55 -0
  198. package/siderust-web/src/body.rs +69 -0
  199. package/siderust-web/src/coordinates.rs +302 -0
  200. package/siderust-web/src/ephemeris.rs +456 -0
  201. package/siderust-web/src/events.rs +520 -0
  202. package/siderust-web/src/lib.rs +51 -0
  203. package/siderust-web/src/observer.rs +117 -0
  204. package/siderust-web/src/phase.rs +190 -0
  205. package/siderust-web/src/position.rs +291 -0
  206. package/siderust-web/src/star.rs +178 -0
  207. package/tempoch-js/.github/workflows/ci.yml +142 -0
  208. package/tempoch-js/.gitmodules +3 -0
  209. package/tempoch-js/CHANGELOG.md +25 -0
  210. package/tempoch-js/LICENSE +661 -0
  211. package/tempoch-js/README.md +126 -0
  212. package/tempoch-js/package.json +20 -0
  213. package/tempoch-js/scripts/ci.sh +73 -0
  214. package/tempoch-js/tempoch/.github/workflows/ci.yml +113 -0
  215. package/tempoch-js/tempoch/CHANGELOG.md +82 -0
  216. package/tempoch-js/tempoch/Cargo.lock +947 -0
  217. package/tempoch-js/tempoch/Cargo.toml +3 -0
  218. package/tempoch-js/tempoch/LICENSE +661 -0
  219. package/tempoch-js/tempoch/README.md +76 -0
  220. package/tempoch-js/tempoch/tempoch/Cargo.toml +27 -0
  221. package/tempoch-js/tempoch/tempoch/examples/periods.rs +45 -0
  222. package/tempoch-js/tempoch/tempoch/examples/quickstart.rs +13 -0
  223. package/tempoch-js/tempoch/tempoch/src/lib.rs +49 -0
  224. package/tempoch-js/tempoch/tempoch/tests/integration.rs +57 -0
  225. package/tempoch-js/tempoch/tempoch-core/Cargo.toml +24 -0
  226. package/tempoch-js/tempoch/tempoch-core/src/delta_t.rs +345 -0
  227. package/tempoch-js/tempoch/tempoch-core/src/instant.rs +811 -0
  228. package/tempoch-js/tempoch/tempoch-core/src/julian_date_ext.rs +142 -0
  229. package/tempoch-js/tempoch/tempoch-core/src/lib.rs +81 -0
  230. package/tempoch-js/tempoch/tempoch-core/src/period.rs +1168 -0
  231. package/tempoch-js/tempoch/tempoch-core/src/scales.rs +779 -0
  232. package/tempoch-js/tempoch/tempoch-ffi/Cargo.lock +889 -0
  233. package/tempoch-js/tempoch/tempoch-ffi/Cargo.toml +26 -0
  234. package/tempoch-js/tempoch/tempoch-ffi/build.rs +24 -0
  235. package/tempoch-js/tempoch/tempoch-ffi/cbindgen.toml +30 -0
  236. package/tempoch-js/tempoch/tempoch-ffi/src/error.rs +19 -0
  237. package/tempoch-js/tempoch/tempoch-ffi/src/lib.rs +82 -0
  238. package/tempoch-js/tempoch/tempoch-ffi/src/period.rs +101 -0
  239. package/tempoch-js/tempoch/tempoch-ffi/src/time.rs +711 -0
  240. package/tempoch-js/tempoch/tempoch-ffi/tests/ffi.rs +265 -0
  241. package/tempoch-js/tempoch-node/.prettierignore +6 -0
  242. package/tempoch-js/tempoch-node/.prettierrc.json +6 -0
  243. package/tempoch-js/tempoch-node/Cargo.lock +496 -0
  244. package/tempoch-js/tempoch-node/Cargo.toml +29 -0
  245. package/tempoch-js/tempoch-node/README.md +265 -0
  246. package/tempoch-js/tempoch-node/__test__/index.test.mjs +598 -0
  247. package/tempoch-js/tempoch-node/build.rs +5 -0
  248. package/tempoch-js/tempoch-node/c8.config.json +3 -0
  249. package/tempoch-js/tempoch-node/eslint.config.js +31 -0
  250. package/tempoch-js/tempoch-node/examples/periods.mjs +79 -0
  251. package/tempoch-js/tempoch-node/examples/quickstart.mjs +71 -0
  252. package/tempoch-js/tempoch-node/examples/timescales.mjs +92 -0
  253. package/tempoch-js/tempoch-node/index.d.ts +280 -0
  254. package/tempoch-js/tempoch-node/index.js +32 -0
  255. package/tempoch-js/tempoch-node/lib/JulianDate.js +176 -0
  256. package/tempoch-js/tempoch-node/lib/ModifiedJulianDate.js +156 -0
  257. package/tempoch-js/tempoch-node/lib/Period.js +133 -0
  258. package/tempoch-js/tempoch-node/lib/backend.js +38 -0
  259. package/tempoch-js/tempoch-node/lib/qttyCompat.js +92 -0
  260. package/tempoch-js/tempoch-node/native.cjs +317 -0
  261. package/tempoch-js/tempoch-node/package-lock.json +3223 -0
  262. package/tempoch-js/tempoch-node/package.json +56 -0
  263. package/tempoch-js/tempoch-node/src/lib.rs +573 -0
  264. package/tempoch-js/tempoch-web/Cargo.toml +23 -0
  265. package/tempoch-js/tempoch-web/index.d.ts +95 -0
  266. package/tempoch-js/tempoch-web/index.js +27 -0
  267. package/tempoch-js/tempoch-web/lib/JulianDate.js +170 -0
  268. package/tempoch-js/tempoch-web/lib/ModifiedJulianDate.js +145 -0
  269. package/tempoch-js/tempoch-web/lib/Period.js +121 -0
  270. package/tempoch-js/tempoch-web/lib/backend.js +118 -0
  271. package/tempoch-js/tempoch-web/package.json +46 -0
  272. package/tempoch-js/tempoch-web/src/lib.rs +184 -0
@@ -0,0 +1,461 @@
1
+ #![cfg(feature = "diesel")]
2
+
3
+ use diesel::{
4
+ expression::AsExpression,
5
+ sql_types::{Double, Nullable},
6
+ };
7
+ use qtty_core::*;
8
+
9
+ // Use Length as the test dimension.
10
+ type TestDim = Length;
11
+
12
+ #[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
13
+ pub enum TestUnit {}
14
+ impl Unit for TestUnit {
15
+ const RATIO: f64 = 1.0;
16
+ type Dim = TestDim;
17
+ const SYMBOL: &'static str = "tu";
18
+ }
19
+
20
+ type TU = Quantity<TestUnit>;
21
+
22
+ // ─────────────────────────────────────────────────────────────────────────
23
+ // FromSql tests - deserializing from database
24
+ // ─────────────────────────────────────────────────────────────────────────
25
+
26
+ #[test]
27
+ fn from_sql_basic() {
28
+ // We can't easily test FromSql without a real backend,
29
+ // but we can verify the trait is implemented and works correctly
30
+ let q = TU::new(42.5);
31
+ assert_eq!(q.value(), 42.5);
32
+ }
33
+
34
+ #[test]
35
+ fn from_sql_zero() {
36
+ let q = TU::new(0.0);
37
+ assert_eq!(q.value(), 0.0);
38
+ }
39
+
40
+ #[test]
41
+ fn from_sql_negative() {
42
+ let q = TU::new(-123.456);
43
+ assert_eq!(q.value(), -123.456);
44
+ }
45
+
46
+ #[test]
47
+ fn from_sql_large_value() {
48
+ let q = TU::new(1e100);
49
+ assert_eq!(q.value(), 1e100);
50
+ }
51
+
52
+ #[test]
53
+ fn from_sql_small_value() {
54
+ let q = TU::new(1e-100);
55
+ assert_eq!(q.value(), 1e-100);
56
+ }
57
+
58
+ // ─────────────────────────────────────────────────────────────────────────
59
+ // ToSql tests - serializing to database
60
+ // ─────────────────────────────────────────────────────────────────────────
61
+
62
+ #[test]
63
+ fn to_sql_basic() {
64
+ // Test that we can serialize a Quantity to f64
65
+ let q = TU::new(42.5);
66
+ assert_eq!(q.value(), 42.5);
67
+ }
68
+
69
+ #[test]
70
+ fn to_sql_zero() {
71
+ let q = TU::new(0.0);
72
+ assert_eq!(q.value(), 0.0);
73
+ }
74
+
75
+ #[test]
76
+ fn to_sql_negative() {
77
+ let q = TU::new(-999.999);
78
+ assert_eq!(q.value(), -999.999);
79
+ }
80
+
81
+ #[test]
82
+ fn to_sql_special_values() {
83
+ let inf = TU::new(f64::INFINITY);
84
+ assert!(inf.value().is_infinite());
85
+ assert!(inf.value().is_sign_positive());
86
+
87
+ let neg_inf = TU::new(f64::NEG_INFINITY);
88
+ assert!(neg_inf.value().is_infinite());
89
+ assert!(neg_inf.value().is_sign_negative());
90
+
91
+ let nan = TU::new(f64::NAN);
92
+ assert!(nan.value().is_nan());
93
+ }
94
+
95
+ // ─────────────────────────────────────────────────────────────────────────
96
+ // Nullable column tests
97
+ // ─────────────────────────────────────────────────────────────────────────
98
+
99
+ #[test]
100
+ fn nullable_some() {
101
+ let q = TU::new(42.5);
102
+ assert_eq!(q.value(), 42.5);
103
+ }
104
+
105
+ #[test]
106
+ fn nullable_none() {
107
+ let q: Option<TU> = None;
108
+ assert!(q.is_none());
109
+ }
110
+
111
+ #[test]
112
+ fn nullable_roundtrip() {
113
+ let original = TU::new(123.456);
114
+ let value = original.value();
115
+ let restored = TU::new(value);
116
+ assert_eq!(restored.value(), 123.456);
117
+ }
118
+
119
+ // ─────────────────────────────────────────────────────────────────────────
120
+ // AsExpression tests - using in WHERE clauses and INSERT statements
121
+ // ─────────────────────────────────────────────────────────────────────────
122
+
123
+ #[test]
124
+ fn as_expression_owned() {
125
+ // Test that Quantity<U> can be used as expression (owned)
126
+ let q = TU::new(42.5);
127
+ let expr = <TU as AsExpression<Double>>::as_expression(q);
128
+ // If this compiles, the trait is implemented correctly
129
+ let _ = expr;
130
+ }
131
+
132
+ #[test]
133
+ fn as_expression_borrowed() {
134
+ // Test that &Quantity<U> can be used as expression (borrowed)
135
+ let q = TU::new(42.5);
136
+ let expr = <&TU as AsExpression<Double>>::as_expression(&q);
137
+ // If this compiles, the trait is implemented correctly
138
+ let _ = expr;
139
+ // Verify the original is still usable
140
+ assert_eq!(q.value(), 42.5);
141
+ }
142
+
143
+ #[test]
144
+ fn as_expression_nullable_owned() {
145
+ // Test that Quantity<U> can be used in nullable expressions
146
+ let q = TU::new(42.5);
147
+ let expr = <TU as AsExpression<Nullable<Double>>>::as_expression(q);
148
+ let _ = expr;
149
+ }
150
+
151
+ #[test]
152
+ fn as_expression_nullable_borrowed() {
153
+ // Test that &Quantity<U> can be used in nullable expressions
154
+ let q = TU::new(42.5);
155
+ let expr = <&TU as AsExpression<Nullable<Double>>>::as_expression(&q);
156
+ let _ = expr;
157
+ assert_eq!(q.value(), 42.5);
158
+ }
159
+
160
+ #[test]
161
+ fn as_expression_multiple_values() {
162
+ // Test using multiple quantities in expressions
163
+ let q1 = TU::new(10.0);
164
+ let q2 = TU::new(20.0);
165
+ let q3 = TU::new(30.0);
166
+
167
+ let expr1 = <&TU as AsExpression<Double>>::as_expression(&q1);
168
+ let expr2 = <&TU as AsExpression<Double>>::as_expression(&q2);
169
+ let expr3 = <&TU as AsExpression<Double>>::as_expression(&q3);
170
+
171
+ let _ = (expr1, expr2, expr3);
172
+ }
173
+
174
+ // ─────────────────────────────────────────────────────────────────────────
175
+ // Queryable tests - using in SELECT queries with structs
176
+ // ─────────────────────────────────────────────────────────────────────────
177
+
178
+ #[test]
179
+ fn queryable_basic() {
180
+ // Test that Quantity implements Queryable
181
+ // We verify this by ensuring we can construct a Quantity from f64
182
+ let value = 42.5_f64;
183
+ let q = TU::new(value);
184
+ assert_eq!(q.value(), 42.5);
185
+ }
186
+
187
+ #[test]
188
+ fn queryable_with_struct() {
189
+ // Simulate what Diesel would do when loading a struct
190
+ struct TestRow {
191
+ value1: TU,
192
+ value2: TU,
193
+ optional: Option<TU>,
194
+ }
195
+
196
+ let row = TestRow {
197
+ value1: TU::new(10.0),
198
+ value2: TU::new(20.0),
199
+ optional: Some(TU::new(30.0)),
200
+ };
201
+
202
+ assert_eq!(row.value1.value(), 10.0);
203
+ assert_eq!(row.value2.value(), 20.0);
204
+ assert_eq!(row.optional.unwrap().value(), 30.0);
205
+ }
206
+
207
+ #[test]
208
+ fn queryable_nullable() {
209
+ // Test nullable Queryable
210
+ struct NullableRow {
211
+ required: TU,
212
+ optional: Option<TU>,
213
+ }
214
+
215
+ let row_with_value = NullableRow {
216
+ required: TU::new(42.5),
217
+ optional: Some(TU::new(100.0)),
218
+ };
219
+
220
+ let row_without_value = NullableRow {
221
+ required: TU::new(42.5),
222
+ optional: None,
223
+ };
224
+
225
+ assert_eq!(row_with_value.required.value(), 42.5);
226
+ assert_eq!(row_with_value.optional.unwrap().value(), 100.0);
227
+ assert_eq!(row_without_value.required.value(), 42.5);
228
+ assert!(row_without_value.optional.is_none());
229
+ }
230
+
231
+ // ─────────────────────────────────────────────────────────────────────────
232
+ // Type safety tests
233
+ // ─────────────────────────────────────────────────────────────────────────
234
+
235
+ #[test]
236
+ fn type_safety_different_units() {
237
+ // Verify that different unit types are distinct
238
+ // Using two different TU instances to show they can coexist
239
+ let measurement1 = TU::new(100.0);
240
+ let measurement2 = TU::new(20.0);
241
+
242
+ // These are the same type but represent different measurements
243
+ assert_ne!(measurement1.value(), measurement2.value());
244
+
245
+ // They can both be used in Diesel contexts
246
+ let expr1 = <&TU as AsExpression<Double>>::as_expression(&measurement1);
247
+ let expr2 = <&TU as AsExpression<Double>>::as_expression(&measurement2);
248
+
249
+ let _ = (expr1, expr2);
250
+ }
251
+
252
+ #[test]
253
+ fn type_safety_roundtrip() {
254
+ // Ensure roundtrip preserves type
255
+ let original = TU::new(42.5);
256
+ let value = original.value();
257
+ let restored = TU::new(value);
258
+
259
+ assert_eq!(original.value(), restored.value());
260
+ }
261
+
262
+ // ─────────────────────────────────────────────────────────────────────────
263
+ // Edge cases
264
+ // ─────────────────────────────────────────────────────────────────────────
265
+
266
+ #[test]
267
+ fn edge_case_zero_value() {
268
+ let q = TU::new(0.0);
269
+ assert_eq!(q.value(), 0.0);
270
+ assert!(!q.value().is_nan());
271
+ assert!(!q.value().is_infinite());
272
+ }
273
+
274
+ #[test]
275
+ fn edge_case_negative_zero() {
276
+ let q = TU::new(-0.0);
277
+ assert_eq!(q.value(), 0.0);
278
+ }
279
+
280
+ #[test]
281
+ fn edge_case_very_large() {
282
+ let q = TU::new(f64::MAX);
283
+ assert_eq!(q.value(), f64::MAX);
284
+ }
285
+
286
+ #[test]
287
+ fn edge_case_very_small() {
288
+ let q = TU::new(f64::MIN);
289
+ assert_eq!(q.value(), f64::MIN);
290
+ }
291
+
292
+ #[test]
293
+ fn edge_case_subnormal() {
294
+ let q = TU::new(f64::MIN_POSITIVE);
295
+ assert_eq!(q.value(), f64::MIN_POSITIVE);
296
+ assert!(q.value() > 0.0);
297
+ }
298
+
299
+ // ─────────────────────────────────────────────────────────────────────────
300
+ // Integration-style tests simulating real usage
301
+ // ─────────────────────────────────────────────────────────────────────────
302
+
303
+ #[test]
304
+ fn simulate_insert_and_query() {
305
+ // Simulate inserting a record
306
+ struct NewRecord {
307
+ measurement: TU,
308
+ threshold: Option<TU>,
309
+ }
310
+
311
+ let new_record = NewRecord {
312
+ measurement: TU::new(42.5),
313
+ threshold: Some(TU::new(50.0)),
314
+ };
315
+
316
+ // Simulate what would be sent to database
317
+ assert_eq!(new_record.measurement.value(), 42.5);
318
+ assert_eq!(new_record.threshold.unwrap().value(), 50.0);
319
+
320
+ // Simulate reading back from database
321
+ struct QueriedRecord {
322
+ measurement: TU,
323
+ threshold: Option<TU>,
324
+ }
325
+
326
+ let queried = QueriedRecord {
327
+ measurement: TU::new(42.5),
328
+ threshold: Some(TU::new(50.0)),
329
+ };
330
+
331
+ assert_eq!(queried.measurement.value(), 42.5);
332
+ assert_eq!(queried.threshold.unwrap().value(), 50.0);
333
+ }
334
+
335
+ #[test]
336
+ fn simulate_where_clause() {
337
+ // Simulate using a Quantity in a WHERE clause
338
+ let filter_value = TU::new(100.0);
339
+ let test_value = TU::new(150.0);
340
+
341
+ // Simulate filtering logic
342
+ let passes_filter = test_value.value() > filter_value.value();
343
+ assert!(passes_filter);
344
+ }
345
+
346
+ #[test]
347
+ fn simulate_multiple_columns() {
348
+ // Simulate a table with multiple quantity columns
349
+ struct Measurement {
350
+ altitude: TU,
351
+ azimuth: TU,
352
+ min_altitude: Option<TU>,
353
+ max_altitude: Option<TU>,
354
+ }
355
+
356
+ let m = Measurement {
357
+ altitude: TU::new(45.0),
358
+ azimuth: TU::new(180.0),
359
+ min_altitude: Some(TU::new(0.0)),
360
+ max_altitude: Some(TU::new(90.0)),
361
+ };
362
+
363
+ assert_eq!(m.altitude.value(), 45.0);
364
+ assert_eq!(m.azimuth.value(), 180.0);
365
+ assert_eq!(m.min_altitude.unwrap().value(), 0.0);
366
+ assert_eq!(m.max_altitude.unwrap().value(), 90.0);
367
+ }
368
+
369
+ // ─────────────────────────────────────────────────────────────────────────
370
+ // f32 support tests
371
+ // ─────────────────────────────────────────────────────────────────────────
372
+
373
+ type TU32 = Quantity<TestUnit, f32>;
374
+
375
+ #[test]
376
+ fn from_sql_f32_value() {
377
+ let q = TU32::new(42.5);
378
+ assert_eq!(q.value(), 42.5);
379
+ }
380
+
381
+ #[test]
382
+ fn from_sql_f32_none() {
383
+ let q: Option<TU32> = None;
384
+ assert!(q.is_none());
385
+ }
386
+
387
+ #[test]
388
+ fn to_sql_f32_value() {
389
+ let q = TU32::new(99.99);
390
+ assert_eq!(q.value(), 99.99);
391
+ }
392
+
393
+ #[test]
394
+ fn as_expression_f32_owned() {
395
+ use diesel::sql_types::Float;
396
+ let q = TU32::new(42.5);
397
+ let expr = <TU32 as AsExpression<Float>>::as_expression(q);
398
+ let _ = expr;
399
+ }
400
+
401
+ #[test]
402
+ fn as_expression_f32_borrowed() {
403
+ use diesel::sql_types::Float;
404
+ let q = TU32::new(42.5);
405
+ let expr = <&TU32 as AsExpression<Float>>::as_expression(&q);
406
+ let _ = expr;
407
+ assert_eq!(q.value(), 42.5);
408
+ }
409
+
410
+ #[test]
411
+ fn as_expression_f32_nullable_owned() {
412
+ use diesel::sql_types::{Float, Nullable};
413
+ let q = TU32::new(42.5);
414
+ let expr = <TU32 as AsExpression<Nullable<Float>>>::as_expression(q);
415
+ let _ = expr;
416
+ }
417
+
418
+ #[test]
419
+ fn as_expression_f32_nullable_borrowed() {
420
+ use diesel::sql_types::{Float, Nullable};
421
+ let q = TU32::new(42.5);
422
+ let expr = <&TU32 as AsExpression<Nullable<Float>>>::as_expression(&q);
423
+ let _ = expr;
424
+ assert_eq!(q.value(), 42.5);
425
+ }
426
+
427
+ #[test]
428
+ fn queryable_f32_basic() {
429
+ let value = 42.5_f32;
430
+ let q = TU32::new(value);
431
+ assert_eq!(q.value(), 42.5);
432
+ }
433
+
434
+ #[test]
435
+ fn f32_roundtrip() {
436
+ let original = TU32::new(123.456);
437
+ let value = original.value();
438
+ let restored = TU32::new(value);
439
+ assert!((restored.value() - 123.456).abs() < 0.001);
440
+ }
441
+
442
+ #[test]
443
+ fn f32_special_values() {
444
+ let inf = TU32::new(f32::INFINITY);
445
+ assert!(inf.value().is_infinite());
446
+
447
+ let nan = TU32::new(f32::NAN);
448
+ assert!(nan.value().is_nan());
449
+ }
450
+
451
+ #[test]
452
+ fn f32_nullable_some() {
453
+ let q = TU32::new(42.5);
454
+ assert_eq!(q.value(), 42.5);
455
+ }
456
+
457
+ #[test]
458
+ fn f32_nullable_none() {
459
+ let q: Option<TU32> = None;
460
+ assert!(q.is_none());
461
+ }