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.
- package/.github/workflows/ci.yml +166 -0
- package/.gitmodules +9 -0
- package/CHANGELOG.md +26 -0
- package/LICENSE +661 -0
- package/README.md +138 -0
- package/package.json +12 -0
- package/qtty-js/.github/workflows/ci.yml +151 -0
- package/qtty-js/.gitmodules +3 -0
- package/qtty-js/CHANGELOG.md +31 -0
- package/qtty-js/LICENSE +661 -0
- package/qtty-js/README.md +132 -0
- package/qtty-js/package.json +20 -0
- package/qtty-js/qtty/.github/workflows/ci.yml +155 -0
- package/qtty-js/qtty/CHANGELOG.md +120 -0
- package/qtty-js/qtty/Cargo.lock +1462 -0
- package/qtty-js/qtty/Cargo.toml +12 -0
- package/qtty-js/qtty/LICENSE +661 -0
- package/qtty-js/qtty/README.md +9 -0
- package/qtty-js/qtty/qtty/Cargo.toml +41 -0
- package/qtty-js/qtty/qtty/README.md +8 -0
- package/qtty-js/qtty/qtty/examples/angles.rs +14 -0
- package/qtty-js/qtty/qtty/examples/astronomy.rs +17 -0
- package/qtty-js/qtty/qtty/examples/dimensional_arithmetic.rs +83 -0
- package/qtty-js/qtty/qtty/examples/python_integration.rs +61 -0
- package/qtty-js/qtty/qtty/examples/quickstart.rs +15 -0
- package/qtty-js/qtty/qtty/examples/ratios.rs +12 -0
- package/qtty-js/qtty/qtty/examples/serde_with_unit.rs +234 -0
- package/qtty-js/qtty/qtty/examples/serialization.rs +141 -0
- package/qtty-js/qtty/qtty/examples/serialization_advanced.rs +155 -0
- package/qtty-js/qtty/qtty/src/f32.rs +108 -0
- package/qtty-js/qtty/qtty/src/f64.rs +30 -0
- package/qtty-js/qtty/qtty/src/i128.rs +111 -0
- package/qtty-js/qtty/qtty/src/i16.rs +111 -0
- package/qtty-js/qtty/qtty/src/i32.rs +111 -0
- package/qtty-js/qtty/qtty/src/i64.rs +111 -0
- package/qtty-js/qtty/qtty/src/i8.rs +111 -0
- package/qtty-js/qtty/qtty/src/lib.rs +238 -0
- package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std/Cargo.lock +83 -0
- package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std/Cargo.toml +10 -0
- package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std/src/lib.rs +7 -0
- package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std-alloc/Cargo.lock +83 -0
- package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std-alloc/Cargo.toml +10 -0
- package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std-alloc/src/lib.rs +7 -0
- package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-std/Cargo.lock +83 -0
- package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-std/Cargo.toml +10 -0
- package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-std/src/lib.rs +5 -0
- package/qtty-js/qtty/qtty/tests/integration_tests.rs +529 -0
- package/qtty-js/qtty/qtty/tests/qtty_vec_feature_matrix.rs +58 -0
- package/qtty-js/qtty/qtty-core/Cargo.toml +41 -0
- package/qtty-js/qtty/qtty-core/README.md +8 -0
- package/qtty-js/qtty/qtty-core/examples/diesel_integration.rs +145 -0
- package/qtty-js/qtty/qtty-core/examples/quantity_db_serde.rs +215 -0
- package/qtty-js/qtty/qtty-core/src/dimension.rs +249 -0
- package/qtty-js/qtty/qtty-core/src/feature_diesel.rs +318 -0
- package/qtty-js/qtty/qtty-core/src/feature_pyo3.rs +27 -0
- package/qtty-js/qtty/qtty-core/src/feature_serde.rs +203 -0
- package/qtty-js/qtty/qtty-core/src/feature_tiberius.rs +28 -0
- package/qtty-js/qtty/qtty-core/src/lib.rs +744 -0
- package/qtty-js/qtty/qtty-core/src/macros.rs +93 -0
- package/qtty-js/qtty/qtty-core/src/quantity.rs +810 -0
- package/qtty-js/qtty/qtty-core/src/scalar.rs +1742 -0
- package/qtty-js/qtty/qtty-core/src/unit.rs +332 -0
- package/qtty-js/qtty/qtty-core/src/units/angular.rs +1228 -0
- package/qtty-js/qtty/qtty-core/src/units/area.rs +243 -0
- package/qtty-js/qtty/qtty-core/src/units/frequency.rs +179 -0
- package/qtty-js/qtty/qtty-core/src/units/length.rs +1270 -0
- package/qtty-js/qtty/qtty-core/src/units/mass.rs +488 -0
- package/qtty-js/qtty/qtty-core/src/units/mod.rs +26 -0
- package/qtty-js/qtty/qtty-core/src/units/power.rs +324 -0
- package/qtty-js/qtty/qtty-core/src/units/time.rs +667 -0
- package/qtty-js/qtty/qtty-core/src/units/unitless.rs +212 -0
- package/qtty-js/qtty/qtty-core/src/units/velocity.rs +210 -0
- package/qtty-js/qtty/qtty-core/src/units/volume.rs +269 -0
- package/qtty-js/qtty/qtty-core/tests/core.rs +628 -0
- package/qtty-js/qtty/qtty-core/tests/diesel.rs +461 -0
- package/qtty-js/qtty/qtty-core/tests/integers.rs +632 -0
- package/qtty-js/qtty/qtty-core/tests/no_cross_unit_ops.rs +35 -0
- package/qtty-js/qtty/qtty-core/tests/pyo3.rs +334 -0
- package/qtty-js/qtty/qtty-core/tests/quantity_f32.rs +276 -0
- package/qtty-js/qtty/qtty-core/tests/scalar_decimal.rs +258 -0
- package/qtty-js/qtty/qtty-core/tests/scalar_f32.rs +286 -0
- package/qtty-js/qtty/qtty-core/tests/scalar_f64_real.rs +287 -0
- package/qtty-js/qtty/qtty-core/tests/scalar_rational.rs +260 -0
- package/qtty-js/qtty/qtty-core/tests/serde.rs +256 -0
- package/qtty-js/qtty/qtty-core/tests/tiberius.rs +208 -0
- package/qtty-js/qtty/qtty-derive/Cargo.toml +23 -0
- package/qtty-js/qtty/qtty-derive/README.md +8 -0
- package/qtty-js/qtty/qtty-derive/src/lib.rs +340 -0
- package/qtty-js/qtty/qtty-ffi/ARCHITECTURE.md +3 -0
- package/qtty-js/qtty/qtty-ffi/Cargo.toml +31 -0
- package/qtty-js/qtty/qtty-ffi/README.md +9 -0
- package/qtty-js/qtty/qtty-ffi/build.rs +326 -0
- package/qtty-js/qtty/qtty-ffi/cbindgen.toml +105 -0
- package/qtty-js/qtty/qtty-ffi/include/qtty_ffi.h +1126 -0
- package/qtty-js/qtty/qtty-ffi/src/ffi.rs +1251 -0
- package/qtty-js/qtty/qtty-ffi/src/ffi_serde.rs +294 -0
- package/qtty-js/qtty/qtty-ffi/src/helpers.rs +310 -0
- package/qtty-js/qtty/qtty-ffi/src/lib.rs +229 -0
- package/qtty-js/qtty/qtty-ffi/src/macros.rs +121 -0
- package/qtty-js/qtty/qtty-ffi/src/registry.rs +274 -0
- package/qtty-js/qtty/qtty-ffi/src/types.rs +620 -0
- package/qtty-js/qtty/qtty-ffi/tests/integration_tests.rs +842 -0
- package/qtty-js/qtty/qtty-ffi/units.csv +156 -0
- package/qtty-js/qtty/qtty-ffi/units.csv.md +3 -0
- package/qtty-js/qtty-node/.prettierignore +6 -0
- package/qtty-js/qtty-node/.prettierrc.json +6 -0
- package/qtty-js/qtty-node/README.md +250 -0
- package/qtty-js/qtty-node/c8.config.json +11 -0
- package/qtty-js/qtty-node/eslint.config.js +31 -0
- package/qtty-js/qtty-node/examples/arithmetic.mjs +64 -0
- package/qtty-js/qtty-node/examples/astronomy.mjs +90 -0
- package/qtty-js/qtty-node/examples/quickstart.mjs +36 -0
- package/qtty-js/qtty-node/examples/serialization.mjs +125 -0
- package/qtty-js/qtty-node/examples/unit_factories.mjs +74 -0
- package/qtty-js/qtty-node/index.d.ts +219 -0
- package/qtty-js/qtty-node/index.js +323 -0
- package/qtty-js/qtty-node/lib/DerivedQuantity.js +122 -0
- package/qtty-js/qtty-node/lib/Quantity.js +151 -0
- package/qtty-js/qtty-node/lib/backend.js +25 -0
- package/qtty-js/qtty-node/native.cjs +306 -0
- package/qtty-js/qtty-node/package-lock.json +3223 -0
- package/qtty-js/qtty-node/package.json +70 -0
- package/qtty-js/qtty-node/units.d.ts +299 -0
- package/qtty-js/qtty-node/units.js +210 -0
- package/qtty-js/qtty-web/Cargo.lock +767 -0
- package/qtty-js/qtty-web/Cargo.toml +21 -0
- package/qtty-js/qtty-web/index.d.ts +140 -0
- package/qtty-js/qtty-web/index.js +20 -0
- package/qtty-js/qtty-web/lib/DerivedQuantity.js +58 -0
- package/qtty-js/qtty-web/lib/Quantity.js +75 -0
- package/qtty-js/qtty-web/lib/backend.js +80 -0
- package/qtty-js/qtty-web/package.json +45 -0
- package/qtty-js/qtty-web/src/lib.rs +111 -0
- package/qtty-js/scripts/ci.sh +73 -0
- package/scripts/ci.sh +123 -0
- package/siderust-core/Cargo.lock +787 -0
- package/siderust-core/Cargo.toml +18 -0
- package/siderust-core/DEDUPLICATION.md +124 -0
- package/siderust-core/src/body.rs +120 -0
- package/siderust-core/src/events.rs +184 -0
- package/siderust-core/src/lib.rs +20 -0
- package/siderust-core/src/observer.rs +55 -0
- package/siderust-core/src/position.rs +213 -0
- package/siderust-node/.prettierignore +7 -0
- package/siderust-node/.prettierrc.json +6 -0
- package/siderust-node/Cargo.lock +906 -0
- package/siderust-node/Cargo.toml +29 -0
- package/siderust-node/README.md +109 -0
- package/siderust-node/__test__/index.test.mjs +248 -0
- package/siderust-node/build.rs +5 -0
- package/siderust-node/c8.config.json +3 -0
- package/siderust-node/eslint.config.js +31 -0
- package/siderust-node/examples/01_basic_coordinates.mjs +24 -0
- package/siderust-node/examples/02_coordinate_transformations.mjs +25 -0
- package/siderust-node/examples/03_all_frames_conversions.mjs +26 -0
- package/siderust-node/examples/04_all_center_conversions.mjs +24 -0
- package/siderust-node/examples/05_target_tracking.mjs +22 -0
- package/siderust-node/examples/06_night_events.mjs +18 -0
- package/siderust-node/examples/07_moon_properties.mjs +21 -0
- package/siderust-node/examples/08_solar_system.mjs +19 -0
- package/siderust-node/examples/09_star_observability.mjs +22 -0
- package/siderust-node/examples/10_time_periods.mjs +9 -0
- package/siderust-node/examples/11_serialization.mjs +31 -0
- package/siderust-node/examples/12_runtime_ephemeris.mjs +27 -0
- package/siderust-node/examples/13_coordinate_operations.mjs +20 -0
- package/siderust-node/index.d.ts +623 -0
- package/siderust-node/index.js +79 -0
- package/siderust-node/lib/Observer.js +112 -0
- package/siderust-node/lib/Star.js +118 -0
- package/siderust-node/lib/backend.js +63 -0
- package/siderust-node/lib/wrappers.js +566 -0
- package/siderust-node/main.js +20 -0
- package/siderust-node/native.cjs +360 -0
- package/siderust-node/package-lock.json +3261 -0
- package/siderust-node/package.json +71 -0
- package/siderust-node/src/body.rs +74 -0
- package/siderust-node/src/coordinates.rs +372 -0
- package/siderust-node/src/ephemeris.rs +462 -0
- package/siderust-node/src/events.rs +577 -0
- package/siderust-node/src/lib.rs +43 -0
- package/siderust-node/src/observer.rs +132 -0
- package/siderust-node/src/phase.rs +218 -0
- package/siderust-node/src/position.rs +292 -0
- package/siderust-node/src/star.rs +200 -0
- package/siderust-web/Cargo.lock +855 -0
- package/siderust-web/Cargo.toml +34 -0
- package/siderust-web/README.md +100 -0
- package/siderust-web/__test__/index.test.mjs +118 -0
- package/siderust-web/examples/github-pages/README.md +31 -0
- package/siderust-web/examples/github-pages/index.html +135 -0
- package/siderust-web/index.d.ts +311 -0
- package/siderust-web/index.js +66 -0
- package/siderust-web/lib/Observer.js +103 -0
- package/siderust-web/lib/Star.js +116 -0
- package/siderust-web/lib/backend.js +400 -0
- package/siderust-web/lib/wrappers.js +512 -0
- package/siderust-web/package.json +55 -0
- package/siderust-web/src/body.rs +69 -0
- package/siderust-web/src/coordinates.rs +302 -0
- package/siderust-web/src/ephemeris.rs +456 -0
- package/siderust-web/src/events.rs +520 -0
- package/siderust-web/src/lib.rs +51 -0
- package/siderust-web/src/observer.rs +117 -0
- package/siderust-web/src/phase.rs +190 -0
- package/siderust-web/src/position.rs +291 -0
- package/siderust-web/src/star.rs +178 -0
- package/tempoch-js/.github/workflows/ci.yml +142 -0
- package/tempoch-js/.gitmodules +3 -0
- package/tempoch-js/CHANGELOG.md +25 -0
- package/tempoch-js/LICENSE +661 -0
- package/tempoch-js/README.md +126 -0
- package/tempoch-js/package.json +20 -0
- package/tempoch-js/scripts/ci.sh +73 -0
- package/tempoch-js/tempoch/.github/workflows/ci.yml +113 -0
- package/tempoch-js/tempoch/CHANGELOG.md +82 -0
- package/tempoch-js/tempoch/Cargo.lock +947 -0
- package/tempoch-js/tempoch/Cargo.toml +3 -0
- package/tempoch-js/tempoch/LICENSE +661 -0
- package/tempoch-js/tempoch/README.md +76 -0
- package/tempoch-js/tempoch/tempoch/Cargo.toml +27 -0
- package/tempoch-js/tempoch/tempoch/examples/periods.rs +45 -0
- package/tempoch-js/tempoch/tempoch/examples/quickstart.rs +13 -0
- package/tempoch-js/tempoch/tempoch/src/lib.rs +49 -0
- package/tempoch-js/tempoch/tempoch/tests/integration.rs +57 -0
- package/tempoch-js/tempoch/tempoch-core/Cargo.toml +24 -0
- package/tempoch-js/tempoch/tempoch-core/src/delta_t.rs +345 -0
- package/tempoch-js/tempoch/tempoch-core/src/instant.rs +811 -0
- package/tempoch-js/tempoch/tempoch-core/src/julian_date_ext.rs +142 -0
- package/tempoch-js/tempoch/tempoch-core/src/lib.rs +81 -0
- package/tempoch-js/tempoch/tempoch-core/src/period.rs +1168 -0
- package/tempoch-js/tempoch/tempoch-core/src/scales.rs +779 -0
- package/tempoch-js/tempoch/tempoch-ffi/Cargo.lock +889 -0
- package/tempoch-js/tempoch/tempoch-ffi/Cargo.toml +26 -0
- package/tempoch-js/tempoch/tempoch-ffi/build.rs +24 -0
- package/tempoch-js/tempoch/tempoch-ffi/cbindgen.toml +30 -0
- package/tempoch-js/tempoch/tempoch-ffi/src/error.rs +19 -0
- package/tempoch-js/tempoch/tempoch-ffi/src/lib.rs +82 -0
- package/tempoch-js/tempoch/tempoch-ffi/src/period.rs +101 -0
- package/tempoch-js/tempoch/tempoch-ffi/src/time.rs +711 -0
- package/tempoch-js/tempoch/tempoch-ffi/tests/ffi.rs +265 -0
- package/tempoch-js/tempoch-node/.prettierignore +6 -0
- package/tempoch-js/tempoch-node/.prettierrc.json +6 -0
- package/tempoch-js/tempoch-node/Cargo.lock +496 -0
- package/tempoch-js/tempoch-node/Cargo.toml +29 -0
- package/tempoch-js/tempoch-node/README.md +265 -0
- package/tempoch-js/tempoch-node/__test__/index.test.mjs +598 -0
- package/tempoch-js/tempoch-node/build.rs +5 -0
- package/tempoch-js/tempoch-node/c8.config.json +3 -0
- package/tempoch-js/tempoch-node/eslint.config.js +31 -0
- package/tempoch-js/tempoch-node/examples/periods.mjs +79 -0
- package/tempoch-js/tempoch-node/examples/quickstart.mjs +71 -0
- package/tempoch-js/tempoch-node/examples/timescales.mjs +92 -0
- package/tempoch-js/tempoch-node/index.d.ts +280 -0
- package/tempoch-js/tempoch-node/index.js +32 -0
- package/tempoch-js/tempoch-node/lib/JulianDate.js +176 -0
- package/tempoch-js/tempoch-node/lib/ModifiedJulianDate.js +156 -0
- package/tempoch-js/tempoch-node/lib/Period.js +133 -0
- package/tempoch-js/tempoch-node/lib/backend.js +38 -0
- package/tempoch-js/tempoch-node/lib/qttyCompat.js +92 -0
- package/tempoch-js/tempoch-node/native.cjs +317 -0
- package/tempoch-js/tempoch-node/package-lock.json +3223 -0
- package/tempoch-js/tempoch-node/package.json +56 -0
- package/tempoch-js/tempoch-node/src/lib.rs +573 -0
- package/tempoch-js/tempoch-web/Cargo.toml +23 -0
- package/tempoch-js/tempoch-web/index.d.ts +95 -0
- package/tempoch-js/tempoch-web/index.js +27 -0
- package/tempoch-js/tempoch-web/lib/JulianDate.js +170 -0
- package/tempoch-js/tempoch-web/lib/ModifiedJulianDate.js +145 -0
- package/tempoch-js/tempoch-web/lib/Period.js +121 -0
- package/tempoch-js/tempoch-web/lib/backend.js +118 -0
- package/tempoch-js/tempoch-web/package.json +46 -0
- package/tempoch-js/tempoch-web/src/lib.rs +184 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
#![cfg(feature = "serde")]
|
|
2
|
+
|
|
3
|
+
use qtty_core::*;
|
|
4
|
+
use serde::{Deserialize, Serialize};
|
|
5
|
+
|
|
6
|
+
// Use Length as the test dimension.
|
|
7
|
+
type TestDim = Length;
|
|
8
|
+
|
|
9
|
+
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
|
|
10
|
+
pub enum TestUnit {}
|
|
11
|
+
impl Unit for TestUnit {
|
|
12
|
+
const RATIO: f64 = 1.0;
|
|
13
|
+
type Dim = TestDim;
|
|
14
|
+
const SYMBOL: &'static str = "tu";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type TU = Quantity<TestUnit>;
|
|
18
|
+
|
|
19
|
+
#[test]
|
|
20
|
+
fn serialize_quantity() {
|
|
21
|
+
let q = TU::new(42.5);
|
|
22
|
+
let json = serde_json::to_string(&q).unwrap();
|
|
23
|
+
assert_eq!(json, "42.5");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
#[test]
|
|
27
|
+
fn deserialize_quantity() {
|
|
28
|
+
let json = "42.5";
|
|
29
|
+
let q: TU = serde_json::from_str(json).unwrap();
|
|
30
|
+
assert_eq!(q.value(), 42.5);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#[test]
|
|
34
|
+
fn serde_roundtrip() {
|
|
35
|
+
let original = TU::new(123.456);
|
|
36
|
+
let json = serde_json::to_string(&original).unwrap();
|
|
37
|
+
let restored: TU = serde_json::from_str(&json).unwrap();
|
|
38
|
+
assert!((restored.value() - original.value()).abs() < 1e-12);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
42
|
+
// serde_with_unit module tests
|
|
43
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
44
|
+
|
|
45
|
+
#[derive(Serialize, Deserialize, Debug)]
|
|
46
|
+
struct TestStruct {
|
|
47
|
+
#[serde(with = "crate::serde_with_unit")]
|
|
48
|
+
distance: TU,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
#[test]
|
|
52
|
+
fn serde_with_unit_serialize() {
|
|
53
|
+
let data = TestStruct {
|
|
54
|
+
distance: TU::new(42.5),
|
|
55
|
+
};
|
|
56
|
+
let json = serde_json::to_string(&data).unwrap();
|
|
57
|
+
assert!(json.contains("\"value\""));
|
|
58
|
+
assert!(json.contains("\"unit\""));
|
|
59
|
+
assert!(json.contains("42.5"));
|
|
60
|
+
assert!(json.contains("\"tu\""));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
#[test]
|
|
64
|
+
fn serde_with_unit_deserialize() {
|
|
65
|
+
let json = r#"{"distance":{"value":42.5,"unit":"tu"}}"#;
|
|
66
|
+
let data: TestStruct = serde_json::from_str(json).unwrap();
|
|
67
|
+
assert_eq!(data.distance.value(), 42.5);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#[test]
|
|
71
|
+
fn serde_with_unit_deserialize_no_unit_field() {
|
|
72
|
+
// Should work without unit field for backwards compatibility
|
|
73
|
+
let json = r#"{"distance":{"value":42.5}}"#;
|
|
74
|
+
let data: TestStruct = serde_json::from_str(json).unwrap();
|
|
75
|
+
assert_eq!(data.distance.value(), 42.5);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
#[test]
|
|
79
|
+
fn serde_with_unit_deserialize_wrong_unit() {
|
|
80
|
+
let json = r#"{"distance":{"value":42.5,"unit":"wrong"}}"#;
|
|
81
|
+
let result: Result<TestStruct, _> = serde_json::from_str(json);
|
|
82
|
+
assert!(result.is_err());
|
|
83
|
+
let err_msg = result.unwrap_err().to_string();
|
|
84
|
+
assert!(err_msg.contains("unit mismatch") || err_msg.contains("expected"));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
#[test]
|
|
88
|
+
fn serde_with_unit_deserialize_missing_value() {
|
|
89
|
+
let json = r#"{"distance":{"unit":"tu"}}"#;
|
|
90
|
+
let result: Result<TestStruct, _> = serde_json::from_str(json);
|
|
91
|
+
assert!(result.is_err());
|
|
92
|
+
let err_msg = result.unwrap_err().to_string();
|
|
93
|
+
assert!(err_msg.contains("missing field") || err_msg.contains("value"));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
#[test]
|
|
97
|
+
fn serde_with_unit_deserialize_duplicate_value() {
|
|
98
|
+
let json = r#"{"distance":{"value":42.5,"value":100.0,"unit":"tu"}}"#;
|
|
99
|
+
let result: Result<TestStruct, _> = serde_json::from_str(json);
|
|
100
|
+
// This should either error or use one of the values (implementation-dependent)
|
|
101
|
+
// but we're testing that it doesn't panic
|
|
102
|
+
let _ = result;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
#[test]
|
|
106
|
+
fn serde_with_unit_deserialize_duplicate_unit() {
|
|
107
|
+
let json = r#"{"distance":{"value":42.5,"unit":"tu","unit":"tu"}}"#;
|
|
108
|
+
let result: Result<TestStruct, _> = serde_json::from_str(json);
|
|
109
|
+
// Similar to above - just ensure no panic
|
|
110
|
+
let _ = result;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
#[test]
|
|
114
|
+
fn serde_with_unit_deserialize_invalid_format() {
|
|
115
|
+
// Test the expecting() method by providing wrong format
|
|
116
|
+
let json = r#"{"distance":"not_an_object"}"#;
|
|
117
|
+
let result: Result<TestStruct, _> = serde_json::from_str(json);
|
|
118
|
+
assert!(result.is_err());
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
#[test]
|
|
122
|
+
fn serde_with_unit_deserialize_array() {
|
|
123
|
+
// Test the expecting() method with array format
|
|
124
|
+
let json = r#"{"distance":[42.5, "tu"]}"#;
|
|
125
|
+
let result: Result<TestStruct, _> = serde_json::from_str(json);
|
|
126
|
+
assert!(result.is_err());
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
#[test]
|
|
130
|
+
fn serde_with_unit_roundtrip() {
|
|
131
|
+
let original = TestStruct {
|
|
132
|
+
distance: TU::new(123.456),
|
|
133
|
+
};
|
|
134
|
+
let json = serde_json::to_string(&original).unwrap();
|
|
135
|
+
let restored: TestStruct = serde_json::from_str(&json).unwrap();
|
|
136
|
+
assert!((restored.distance.value() - original.distance.value()).abs() < 1e-12);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
#[test]
|
|
140
|
+
fn serde_with_unit_special_values() {
|
|
141
|
+
// Note: JSON doesn't support Infinity and NaN natively.
|
|
142
|
+
// serde_json serializes them as null, which can't be deserialized
|
|
143
|
+
// back to f64. So we'll test with very large numbers instead.
|
|
144
|
+
let test_large = TestStruct {
|
|
145
|
+
distance: TU::new(1e100),
|
|
146
|
+
};
|
|
147
|
+
let json = serde_json::to_string(&test_large).unwrap();
|
|
148
|
+
let restored: TestStruct = serde_json::from_str(&json).unwrap();
|
|
149
|
+
assert!((restored.distance.value() - 1e100).abs() < 1e88);
|
|
150
|
+
|
|
151
|
+
let test_small = TestStruct {
|
|
152
|
+
distance: TU::new(-1e-100),
|
|
153
|
+
};
|
|
154
|
+
let json = serde_json::to_string(&test_small).unwrap();
|
|
155
|
+
let restored: TestStruct = serde_json::from_str(&json).unwrap();
|
|
156
|
+
assert!((restored.distance.value() + 1e-100).abs() < 1e-112);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
160
|
+
// Additional serde edge cases
|
|
161
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
162
|
+
|
|
163
|
+
#[test]
|
|
164
|
+
fn serde_negative_value() {
|
|
165
|
+
let q = TU::new(-999.999);
|
|
166
|
+
let json = serde_json::to_string(&q).unwrap();
|
|
167
|
+
let restored: TU = serde_json::from_str(&json).unwrap();
|
|
168
|
+
assert_eq!(restored.value(), -999.999);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
#[test]
|
|
172
|
+
fn serde_zero_value() {
|
|
173
|
+
let q = TU::new(0.0);
|
|
174
|
+
let json = serde_json::to_string(&q).unwrap();
|
|
175
|
+
let restored: TU = serde_json::from_str(&json).unwrap();
|
|
176
|
+
assert_eq!(restored.value(), 0.0);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
#[test]
|
|
180
|
+
fn serde_with_unit_negative() {
|
|
181
|
+
let data = TestStruct {
|
|
182
|
+
distance: TU::new(-42.5),
|
|
183
|
+
};
|
|
184
|
+
let json = serde_json::to_string(&data).unwrap();
|
|
185
|
+
let restored: TestStruct = serde_json::from_str(&json).unwrap();
|
|
186
|
+
assert_eq!(restored.distance.value(), -42.5);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
#[test]
|
|
190
|
+
fn serde_with_f32() {
|
|
191
|
+
let q = Quantity::<TestUnit, f32>::new(42.5);
|
|
192
|
+
let json = serde_json::to_string(&q).unwrap();
|
|
193
|
+
let restored: Quantity<TestUnit, f32> = serde_json::from_str(&json).unwrap();
|
|
194
|
+
assert!((restored.value() - 42.5).abs() < 0.01);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
198
|
+
// serde_scalar module tests
|
|
199
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
200
|
+
|
|
201
|
+
mod serde_with_unit {
|
|
202
|
+
use super::*;
|
|
203
|
+
|
|
204
|
+
pub fn serialize<U, S, Ser>(
|
|
205
|
+
quantity: &Quantity<U, S>,
|
|
206
|
+
serializer: Ser,
|
|
207
|
+
) -> Result<Ser::Ok, Ser::Error>
|
|
208
|
+
where
|
|
209
|
+
U: Unit,
|
|
210
|
+
S: qtty_core::scalar::Real,
|
|
211
|
+
Ser: serde::Serializer,
|
|
212
|
+
{
|
|
213
|
+
qtty_core::serde_with_unit::serialize(quantity, serializer)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
pub fn deserialize<'de, U, S, D>(deserializer: D) -> Result<Quantity<U, S>, D::Error>
|
|
217
|
+
where
|
|
218
|
+
U: Unit,
|
|
219
|
+
S: qtty_core::scalar::Real,
|
|
220
|
+
D: serde::Deserializer<'de>,
|
|
221
|
+
{
|
|
222
|
+
qtty_core::serde_with_unit::deserialize(deserializer)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
#[derive(Serialize, Deserialize, Debug)]
|
|
227
|
+
struct ScalarStruct {
|
|
228
|
+
#[serde(with = "qtty_core::serde_scalar")]
|
|
229
|
+
distance: TU,
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
#[test]
|
|
233
|
+
fn serde_scalar_serialize() {
|
|
234
|
+
let data = ScalarStruct {
|
|
235
|
+
distance: TU::new(42.5),
|
|
236
|
+
};
|
|
237
|
+
let json = serde_json::to_string(&data).unwrap();
|
|
238
|
+
assert!(json.contains("42.5"));
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
#[test]
|
|
242
|
+
fn serde_scalar_deserialize() {
|
|
243
|
+
let json = r#"{"distance":42.5}"#;
|
|
244
|
+
let data: ScalarStruct = serde_json::from_str(json).unwrap();
|
|
245
|
+
assert_eq!(data.distance.value(), 42.5);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
#[test]
|
|
249
|
+
fn serde_scalar_roundtrip() {
|
|
250
|
+
let original = ScalarStruct {
|
|
251
|
+
distance: TU::new(123.456),
|
|
252
|
+
};
|
|
253
|
+
let json = serde_json::to_string(&original).unwrap();
|
|
254
|
+
let restored: ScalarStruct = serde_json::from_str(&json).unwrap();
|
|
255
|
+
assert!((restored.distance.value() - 123.456).abs() < 1e-12);
|
|
256
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
#![cfg(feature = "tiberius")]
|
|
2
|
+
|
|
3
|
+
use qtty_core::*;
|
|
4
|
+
use tiberius::{ColumnData, FromSql, ToSql};
|
|
5
|
+
|
|
6
|
+
// Use Length as the test dimension.
|
|
7
|
+
type TestDim = Length;
|
|
8
|
+
|
|
9
|
+
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
|
|
10
|
+
pub enum TestUnit {}
|
|
11
|
+
impl Unit for TestUnit {
|
|
12
|
+
const RATIO: f64 = 1.0;
|
|
13
|
+
type Dim = TestDim;
|
|
14
|
+
const SYMBOL: &'static str = "tu";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type TU = Quantity<TestUnit>;
|
|
18
|
+
|
|
19
|
+
// Helper function for roundtrip testing
|
|
20
|
+
fn test_roundtrip(original: TU) {
|
|
21
|
+
let column_data = original.to_sql();
|
|
22
|
+
match column_data {
|
|
23
|
+
ColumnData::F64(Some(val)) => {
|
|
24
|
+
let column_data_copy = ColumnData::F64(Some(val));
|
|
25
|
+
let restored = TU::from_sql(&column_data_copy).unwrap();
|
|
26
|
+
assert!(restored.is_some());
|
|
27
|
+
assert!((restored.unwrap().value() - original.value()).abs() < 1e-12);
|
|
28
|
+
}
|
|
29
|
+
_ => panic!("Expected ColumnData::F64"),
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#[test]
|
|
34
|
+
fn to_sql_converts_to_f64() {
|
|
35
|
+
let q = TU::new(42.5);
|
|
36
|
+
let column_data = q.to_sql();
|
|
37
|
+
match column_data {
|
|
38
|
+
ColumnData::F64(Some(val)) => assert_eq!(val, 42.5),
|
|
39
|
+
_ => panic!("Expected ColumnData::F64(Some(42.5))"),
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
#[test]
|
|
44
|
+
fn to_sql_handles_negative_values() {
|
|
45
|
+
let q = TU::new(-123.456);
|
|
46
|
+
let column_data = q.to_sql();
|
|
47
|
+
match column_data {
|
|
48
|
+
ColumnData::F64(Some(val)) => assert_eq!(val, -123.456),
|
|
49
|
+
_ => panic!("Expected ColumnData::F64(Some(-123.456))"),
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
#[test]
|
|
54
|
+
fn to_sql_handles_zero() {
|
|
55
|
+
let q = TU::new(0.0);
|
|
56
|
+
let column_data = q.to_sql();
|
|
57
|
+
match column_data {
|
|
58
|
+
ColumnData::F64(Some(val)) => assert_eq!(val, 0.0),
|
|
59
|
+
_ => panic!("Expected ColumnData::F64(Some(0.0))"),
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
#[test]
|
|
64
|
+
fn from_sql_f64_value() {
|
|
65
|
+
let column_data = ColumnData::F64(Some(42.5));
|
|
66
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
67
|
+
assert!(result.is_some());
|
|
68
|
+
assert_eq!(result.unwrap().value(), 42.5);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#[test]
|
|
72
|
+
fn from_sql_f32_value() {
|
|
73
|
+
let column_data = ColumnData::F32(Some(42.5f32));
|
|
74
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
75
|
+
assert!(result.is_some());
|
|
76
|
+
// Use 1e-6 tolerance for f32 since it has less precision than f64
|
|
77
|
+
assert!((result.unwrap().value() - 42.5).abs() < 1e-6);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
#[test]
|
|
81
|
+
fn from_sql_i32_value() {
|
|
82
|
+
let column_data = ColumnData::I32(Some(42));
|
|
83
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
84
|
+
assert!(result.is_some());
|
|
85
|
+
assert_eq!(result.unwrap().value(), 42.0);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
#[test]
|
|
89
|
+
fn from_sql_i64_value() {
|
|
90
|
+
let column_data = ColumnData::I64(Some(12345));
|
|
91
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
92
|
+
assert!(result.is_some());
|
|
93
|
+
assert_eq!(result.unwrap().value(), 12345.0);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
#[test]
|
|
97
|
+
fn from_sql_f64_none() {
|
|
98
|
+
let column_data = ColumnData::F64(None);
|
|
99
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
100
|
+
assert!(result.is_none());
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
#[test]
|
|
104
|
+
fn from_sql_f32_none() {
|
|
105
|
+
let column_data = ColumnData::F32(None);
|
|
106
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
107
|
+
assert!(result.is_none());
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#[test]
|
|
111
|
+
fn from_sql_i32_none() {
|
|
112
|
+
let column_data = ColumnData::I32(None);
|
|
113
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
114
|
+
assert!(result.is_none());
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
#[test]
|
|
118
|
+
fn from_sql_i64_none() {
|
|
119
|
+
let column_data = ColumnData::I64(None);
|
|
120
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
121
|
+
assert!(result.is_none());
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
#[test]
|
|
125
|
+
fn from_sql_negative_i32() {
|
|
126
|
+
let column_data = ColumnData::I32(Some(-42));
|
|
127
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
128
|
+
assert!(result.is_some());
|
|
129
|
+
assert_eq!(result.unwrap().value(), -42.0);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
#[test]
|
|
133
|
+
fn from_sql_negative_i64() {
|
|
134
|
+
let column_data = ColumnData::I64(Some(-12345));
|
|
135
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
136
|
+
assert!(result.is_some());
|
|
137
|
+
assert_eq!(result.unwrap().value(), -12345.0);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
#[test]
|
|
141
|
+
fn roundtrip_to_sql_from_sql() {
|
|
142
|
+
test_roundtrip(TU::new(123.456));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
#[test]
|
|
146
|
+
fn roundtrip_with_large_values() {
|
|
147
|
+
test_roundtrip(TU::new(1e100));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
#[test]
|
|
151
|
+
fn roundtrip_with_small_values() {
|
|
152
|
+
test_roundtrip(TU::new(1e-100));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#[test]
|
|
156
|
+
fn roundtrip_with_zero() {
|
|
157
|
+
test_roundtrip(TU::new(0.0));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
161
|
+
// Additional FromSql variants for coverage
|
|
162
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
163
|
+
|
|
164
|
+
#[test]
|
|
165
|
+
fn from_sql_i16_value() {
|
|
166
|
+
let column_data = ColumnData::I16(Some(42));
|
|
167
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
168
|
+
assert!(result.is_some());
|
|
169
|
+
assert_eq!(result.unwrap().value(), 42.0);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
#[test]
|
|
173
|
+
fn from_sql_i16_none() {
|
|
174
|
+
let column_data = ColumnData::I16(None);
|
|
175
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
176
|
+
assert!(result.is_none());
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
#[test]
|
|
180
|
+
fn from_sql_u8_value() {
|
|
181
|
+
let column_data = ColumnData::U8(Some(255));
|
|
182
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
183
|
+
assert!(result.is_some());
|
|
184
|
+
assert_eq!(result.unwrap().value(), 255.0);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
#[test]
|
|
188
|
+
fn from_sql_u8_none() {
|
|
189
|
+
let column_data = ColumnData::U8(None);
|
|
190
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
191
|
+
assert!(result.is_none());
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
#[test]
|
|
195
|
+
fn from_sql_default_arm() {
|
|
196
|
+
// Using Bit variant which is not handled -> should return None via default arm
|
|
197
|
+
let column_data = ColumnData::Bit(Some(true));
|
|
198
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
199
|
+
assert!(result.is_none());
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
#[test]
|
|
203
|
+
fn from_sql_negative_i16() {
|
|
204
|
+
let column_data = ColumnData::I16(Some(-100));
|
|
205
|
+
let result = TU::from_sql(&column_data).unwrap();
|
|
206
|
+
assert!(result.is_some());
|
|
207
|
+
assert_eq!(result.unwrap().value(), -100.0);
|
|
208
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "qtty-derive"
|
|
3
|
+
description = "Procedural macro derives for qtty-core."
|
|
4
|
+
version.workspace = true
|
|
5
|
+
edition.workspace = true
|
|
6
|
+
authors.workspace = true
|
|
7
|
+
license.workspace = true
|
|
8
|
+
repository.workspace = true
|
|
9
|
+
keywords.workspace = true
|
|
10
|
+
categories.workspace = true
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
documentation = "https://docs.rs/qtty-derive"
|
|
13
|
+
|
|
14
|
+
[lib]
|
|
15
|
+
proc-macro = true
|
|
16
|
+
|
|
17
|
+
[dependencies]
|
|
18
|
+
syn = { version = "2.0", features = ["full", "extra-traits"] }
|
|
19
|
+
quote = "1.0"
|
|
20
|
+
proc-macro2 = "1.0"
|
|
21
|
+
|
|
22
|
+
[dev-dependencies]
|
|
23
|
+
qtty-core = { path = "../qtty-core" }
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# qtty-derive crate
|
|
2
|
+
|
|
3
|
+
Centralized documentation lives under the repository `doc/` tree.
|
|
4
|
+
|
|
5
|
+
- Workspace overview: [`../../doc/users/rust-workspace.md`](../../doc/users/rust-workspace.md)
|
|
6
|
+
- Repository layout: [`../../doc/architecture/repository-layout.md`](../../doc/architecture/repository-layout.md)
|
|
7
|
+
|
|
8
|
+
This README remains as the crate metadata entrypoint.
|