1#[crate::polydat_node(category = Conversions)]
28fn __u64_to_string(input: u64) -> String {
29 input.to_string()
30}
31
32#[crate::polydat_node(category = Conversions)]
33fn __f64_to_string(input: f64) -> String {
34 input.to_string()
35}
36
37#[crate::polydat_node(category = Conversions)]
38fn __u64_to_f64(input: u64) -> f64 {
39 input as f64
40}
41
42#[crate::polydat_node(category = Conversions)]
43fn __bool_to_str(input: bool) -> String {
44 if input { "true".into() } else { "false".into() }
45}
46
47#[crate::polydat_node(category = Conversions)]
48fn __bool_to_u64(input: bool) -> u64 {
49 if input { 1 } else { 0 }
50}
51
52#[crate::polydat_node(category = Conversions)]
53fn __u64_to_bool(input: u64) -> bool {
54 input != 0
55}
56
57#[crate::polydat_node(category = Conversions)]
58fn __u32_to_u64(input: u32) -> u64 {
59 input as u64
60}
61
62#[crate::polydat_node(category = Conversions)]
65fn __u32_to_i64(input: u32) -> i64 {
66 input as i64
67}
68
69#[crate::polydat_node(category = Conversions)]
70fn __i32_to_i64(input: i32) -> i64 {
71 input as i64
72}
73
74#[crate::polydat_node(category = Conversions)]
75fn __f32_to_f64(input: f32) -> f64 {
76 input as f64
77}
78
79#[crate::polydat_node(category = Conversions)]
80fn __i32_to_f64(input: i32) -> f64 {
81 input as f64
82}
83
84#[crate::polydat_node(category = Conversions)]
85fn __u32_to_f64(input: u32) -> f64 {
86 input as f64
87}
88
89#[crate::polydat_node(category = Conversions)]
90fn __i64_to_f64(input: i64) -> f64 {
91 input as f64
92}
93
94#[crate::polydat_node(category = Conversions)]
95fn __i32_to_string(input: i32) -> String {
96 input.to_string()
97}
98
99#[crate::polydat_node(category = Conversions)]
100fn __i64_to_string(input: i64) -> String {
101 input.to_string()
102}
103
104#[crate::polydat_node(category = Conversions)]
105fn __f32_to_string(input: f32) -> String {
106 input.to_string()
107}
108
109#[crate::polydat_node(category = Conversions)]
110fn __u32_to_string(input: u32) -> String {
111 input.to_string()
112}
113
114#[crate::polydat_node(category = Conversions)]
129fn trunc_u64(input: f64) -> u64 {
130 if input.is_nan() {
131 0
132 } else {
133 input.trunc().max(0.0).min(u64::MAX as f64) as u64
134 }
135}
136
137#[crate::polydat_node(category = Conversions)]
140fn round_u64(input: f64) -> u64 {
141 if input.is_nan() {
142 0
143 } else {
144 input.round().max(0.0).min(u64::MAX as f64) as u64
145 }
146}
147
148fn coercion_diagnostic(raw: &str, target: &str, detail: &str) -> String {
179 let braced = format!("{{{raw}}}"); format!(
181 "value {raw:?} is not {target}.\n\n\
182 This usually means a name was written where its VALUE was intended. In a \
183 scenario `set:` block, values are text-templates, so an iteration variable \
184 must be BRACED to substitute its value:\n \
185 set: {{ field: \"{braced}\" }} # the value of `{raw}`\n \
186 set: {{ field: {raw} }} # the literal text \"{raw}\" <-- likely the bug\n\
187 In a `bindings:` block, reference names unquoted instead: `const field := {raw}`.\n\n\
188 (coercion detail: {detail})"
189 )
190}
191
192pub(crate) fn parse_bool(input: &str) -> bool {
204 let raw = input.trim();
205 match raw.to_ascii_lowercase().as_str() {
206 "true" | "1" => true,
207 "false" | "0" => false,
208 _ => panic!(
209 "{}",
210 coercion_diagnostic(
211 raw,
212 "a boolean",
213 "__str_to_bool expected case-insensitive true/false or 1/0",
214 )
215 ),
216 }
217}
218
219#[crate::polydat_node(category = Conversions)]
220fn __str_to_bool(input: &str) -> bool {
221 parse_bool(input)
222}
223
224pub(crate) fn parse_u64(input: &str) -> u64 {
227 let raw = input.trim();
228 raw.parse::<u64>().unwrap_or_else(|e| {
229 panic!(
230 "{}",
231 coercion_diagnostic(raw, "a whole number", &format!("__str_to_u64: {e}"))
232 )
233 })
234}
235
236#[crate::polydat_node(category = Conversions)]
237fn __str_to_u64(input: &str) -> u64 {
238 parse_u64(input)
239}
240
241pub(crate) fn parse_f64(input: &str) -> f64 {
244 let raw = input.trim();
245 raw.parse::<f64>().unwrap_or_else(|e| {
246 panic!(
247 "{}",
248 coercion_diagnostic(raw, "a number", &format!("__str_to_f64: {e}"))
249 )
250 })
251}
252
253#[crate::polydat_node(category = Conversions)]
254fn __str_to_f64(input: &str) -> f64 {
255 parse_f64(input)
256}
257
258#[crate::polydat_node(category = Conversions)]
274fn f64_to_u64(input: f64) -> u64 {
275 input as u64
276}
277
278#[crate::polydat_node(category = Conversions)]
279fn round_to_u64(input: f64) -> u64 {
280 input.round() as u64
281}
282
283#[crate::polydat_node(category = Conversions)]
284fn floor_to_u64(input: f64) -> u64 {
285 input.floor() as u64
286}
287
288#[crate::polydat_node(category = Conversions)]
298fn ceil_to_u64(input: f64) -> u64 {
299 input.ceil() as u64
300}
301
302#[crate::polydat_node(category = Conversions)]
325fn discretize(
326 input: f64,
327 #[poly_default(100.0f64)]
328 #[constraint(PositiveFiniteF64)]
329 range: crate::derive_support::Const<f64>,
330 #[poly_default(10u64)]
331 #[constraint(NonZeroU64)]
332 buckets: crate::derive_support::Const<u64>,
333) -> u64 {
334 let r = *range;
335 let b = *buckets;
336 let v = input.clamp(0.0, r);
337 let bucket = (v / r * b as f64) as u64;
338 bucket.min(b - 1)
339}
340
341#[crate::polydat_node(category = Conversions)]
357fn format_u64(
358 input: u64,
359 #[poly_default(10u64)]
360 #[constraint(AllowedU64(&[2, 8, 10, 16]))]
361 radix: crate::derive_support::Const<u64>,
362) -> String {
363 match *radix {
364 2 => format!("0b{input:b}"),
365 8 => format!("0o{input:o}"),
366 16 => format!("0x{input:x}"),
367 10 => input.to_string(),
368 other => unreachable!("format_u64: radix {other} passed its constraint"),
369 }
370}
371
372impl FormatU64 {
373 pub fn decimal() -> Self {
375 Self::new(10)
376 }
377 pub fn hex() -> Self {
379 Self::new(16)
380 }
381 pub fn octal() -> Self {
383 Self::new(8)
384 }
385 pub fn binary() -> Self {
387 Self::new(2)
388 }
389 pub fn with_radix(radix: u32) -> Self {
391 Self::new(radix as u64)
392 }
393}
394
395#[crate::polydat_node(category = Conversions)]
404fn format_f64(
405 input: f64,
406 #[poly_default(2)] precision: crate::derive_support::Const<u64>,
407) -> String {
408 format!("{:.prec$}", input, prec = *precision as usize)
409}
410
411#[crate::polydat_node(category = Conversions)]
415fn zero_pad_u64(
416 input: u64,
417 #[poly_default(10)] width: crate::derive_support::Const<u64>,
418) -> String {
419 format!("{:0>width$}", input, width = *width as usize)
420}
421
422#[crate::polydat_node(category = Conversions)]
424fn to_f64(input: u64) -> f64 {
425 input as f64
426}
427
428#[crate::polydat_node(category = Conversions)]
441fn to_i64(input: u64) -> i64 {
442 if input > i64::MAX as u64 {
443 panic!("to_i64: value {input} exceeds i64::MAX ({})", i64::MAX);
444 }
445 input as i64
446}
447#[cfg(test)]
448mod tests {
449 use super::*;
450 use crate::ast::{PolydatNode, Value};
451
452 #[test]
453 fn f64_to_u64_truncates() {
454 let node = F64ToU64::new();
455 let mut out = [Value::None];
456 node.eval(&[Value::F64(3.7)], &mut out);
457 assert_eq!(out[0].as_u64(), 3);
458 node.eval(&[Value::F64(3.2)], &mut out);
459 assert_eq!(out[0].as_u64(), 3);
460 }
461
462 #[test]
463 fn round_to_u64_rounds() {
464 let node = RoundToU64::new();
465 let mut out = [Value::None];
466 node.eval(&[Value::F64(3.7)], &mut out);
467 assert_eq!(out[0].as_u64(), 4);
468 node.eval(&[Value::F64(3.2)], &mut out);
469 assert_eq!(out[0].as_u64(), 3);
470 }
471
472 #[test]
473 fn floor_to_u64_floors() {
474 let node = FloorToU64::new();
475 let mut out = [Value::None];
476 node.eval(&[Value::F64(3.9)], &mut out);
477 assert_eq!(out[0].as_u64(), 3);
478 }
479
480 #[test]
481 fn ceil_to_u64_ceils() {
482 let node = CeilToU64::new();
483 let mut out = [Value::None];
484 node.eval(&[Value::F64(3.1)], &mut out);
485 assert_eq!(out[0].as_u64(), 4);
486 }
487
488 #[test]
489 fn discretize_basic() {
490 let node = Discretize::new(100.0, 10);
491 let mut out = [Value::None];
492 node.eval(&[Value::F64(0.0)], &mut out);
493 assert_eq!(out[0].as_u64(), 0);
494 node.eval(&[Value::F64(55.0)], &mut out);
495 assert_eq!(out[0].as_u64(), 5);
496 node.eval(&[Value::F64(99.0)], &mut out);
497 assert_eq!(out[0].as_u64(), 9);
498 }
499
500 #[test]
501 fn discretize_clamps() {
502 let node = Discretize::new(100.0, 10);
503 let mut out = [Value::None];
504 node.eval(&[Value::F64(-5.0)], &mut out);
505 assert_eq!(out[0].as_u64(), 0);
506 node.eval(&[Value::F64(200.0)], &mut out);
507 assert_eq!(out[0].as_u64(), 9);
508 }
509
510 #[test]
511 fn format_u64_hex() {
512 let node = FormatU64::hex();
513 let mut out = [Value::None];
514 node.eval(&[Value::U64(255)], &mut out);
515 assert_eq!(out[0].as_str(), "0xff");
516 }
517
518 #[test]
519 fn format_u64_binary() {
520 let node = FormatU64::binary();
521 let mut out = [Value::None];
522 node.eval(&[Value::U64(42)], &mut out);
523 assert_eq!(out[0].as_str(), "0b101010");
524 }
525
526 #[test]
527 fn format_u64_decimal() {
528 let node = FormatU64::decimal();
529 let mut out = [Value::None];
530 node.eval(&[Value::U64(12345)], &mut out);
531 assert_eq!(out[0].as_str(), "12345");
532 }
533
534 #[test]
535 fn format_f64_precision() {
536 let node = FormatF64::new(2);
537 let mut out = [Value::None];
538 node.eval(&[Value::F64(3.14159)], &mut out);
539 assert_eq!(out[0].as_str(), "3.14");
540 }
541
542 #[test]
543 fn format_f64_zero_precision() {
544 let node = FormatF64::new(0);
545 let mut out = [Value::None];
546 node.eval(&[Value::F64(3.7)], &mut out);
547 assert_eq!(out[0].as_str(), "4");
548 }
549
550 #[test]
551 fn zero_pad() {
552 let node = ZeroPadU64::new(8);
553 let mut out = [Value::None];
554 node.eval(&[Value::U64(42)], &mut out);
555 assert_eq!(out[0].as_str(), "00000042");
556 }
557
558 #[test]
559 fn zero_pad_no_truncation() {
560 let node = ZeroPadU64::new(3);
561 let mut out = [Value::None];
562 node.eval(&[Value::U64(12345)], &mut out);
563 assert_eq!(out[0].as_str(), "12345");
564 }
565
566 #[test]
569 fn u32_to_u64_zero_extends() {
570 let node = U32ToU64::new();
571 let mut out = [Value::None];
572 node.eval(&[Value::U64(42)], &mut out);
573 assert_eq!(out[0].as_u64(), 42);
574 node.eval(&[Value::U64(0xFFFF_FFFF_0000_0001)], &mut out);
576 assert_eq!(out[0].as_u64(), 1);
577 }
578
579 #[test]
580 fn i32_to_i64_sign_extends() {
581 let node = I32ToI64::new();
582 let mut out = [Value::None];
583 node.eval(&[Value::U64(42)], &mut out);
586 assert_eq!(out[0], Value::I64(42));
587 node.eval(&[Value::U64(0xFFFF_FFFF)], &mut out);
590 assert_eq!(out[0], Value::I64(-1));
591 node.eval(&[Value::I64(-1)], &mut out);
593 assert_eq!(out[0], Value::I64(-1));
594 }
595
596 #[test]
597 fn f32_to_f64_widens() {
598 let node = F32ToF64::new();
599 let mut out = [Value::None];
600 let f32_bits = 3.14f32.to_bits() as u64;
601 node.eval(&[Value::U64(f32_bits)], &mut out);
602 let result = out[0].as_f64();
604 assert!((result - 3.14).abs() < 0.001, "got {result}");
605 }
606
607 #[test]
608 fn i32_to_f64_converts() {
609 let node = I32ToF64::new();
610 let mut out = [Value::None];
611 node.eval(&[Value::U64(42)], &mut out);
612 assert_eq!(out[0].as_f64(), 42.0);
613 node.eval(&[Value::U64((-10i32) as u32 as u64)], &mut out);
615 assert_eq!(out[0].as_f64(), -10.0);
616 }
617
618 #[test]
619 fn u32_to_f64_converts() {
620 let node = U32ToF64::new();
621 let mut out = [Value::None];
622 node.eval(&[Value::U64(1000)], &mut out);
623 assert_eq!(out[0].as_f64(), 1000.0);
624 }
625
626 #[test]
627 fn i64_to_f64_converts() {
628 let node = I64ToF64::new();
629 let mut out = [Value::None];
630 node.eval(&[Value::U64(42)], &mut out);
631 assert_eq!(out[0].as_f64(), 42.0);
632 node.eval(&[Value::U64((-1i64) as u64)], &mut out);
634 assert_eq!(out[0].as_f64(), -1.0);
635 }
636
637 #[test]
640 fn i32_to_string_formats_signed() {
641 let node = I32ToString::new();
642 let mut out = [Value::None];
643 node.eval(&[Value::U64(42)], &mut out);
644 assert_eq!(out[0].as_str(), "42");
645 node.eval(&[Value::U64((-7i32) as u32 as u64)], &mut out);
646 assert_eq!(out[0].as_str(), "-7");
647 }
648
649 #[test]
650 fn i64_to_string_formats_signed() {
651 let node = I64ToString::new();
652 let mut out = [Value::None];
653 node.eval(&[Value::U64(100)], &mut out);
654 assert_eq!(out[0].as_str(), "100");
655 node.eval(&[Value::U64((-42i64) as u64)], &mut out);
656 assert_eq!(out[0].as_str(), "-42");
657 }
658
659 #[test]
660 fn f32_to_string_formats() {
661 let node = F32ToString::new();
662 let mut out = [Value::None];
663 let bits = 2.5f32.to_bits() as u64;
664 node.eval(&[Value::U64(bits)], &mut out);
665 assert_eq!(out[0].as_str(), "2.5");
666 }
667
668 #[test]
669 fn u32_to_string_formats() {
670 let node = U32ToString::new();
671 let mut out = [Value::None];
672 node.eval(&[Value::U64(12345)], &mut out);
673 assert_eq!(out[0].as_str(), "12345");
674 }
675
676 #[test]
681 fn str_to_bool_canonical_forms() {
682 let node = StrToBool::new();
683 let mut out = [Value::None];
684 for (input, expected) in [
685 ("true", true),
686 ("false", false),
687 ("True", true),
688 ("False", false),
689 ("TRUE", true),
690 ("FALSE", false),
691 ("1", true),
692 ("0", false),
693 ] {
694 node.eval(&[Value::Str(input.into())], &mut out);
695 assert_eq!(out[0].as_bool(), expected, "input={input:?}");
696 }
697 }
698
699 #[test]
700 fn str_to_bool_trims_whitespace() {
701 let node = StrToBool::new();
702 let mut out = [Value::None];
703 node.eval(&[Value::Str(" true ".into())], &mut out);
704 assert!(out[0].as_bool());
705 node.eval(&[Value::Str("\tfalse\n".into())], &mut out);
706 assert!(!out[0].as_bool());
707 }
708
709 #[test]
710 #[should_panic(expected = "__str_to_bool")]
711 fn str_to_bool_panics_on_unparseable() {
712 let node = StrToBool::new();
713 let mut out = [Value::None];
714 node.eval(&[Value::Str("yes".into())], &mut out);
715 }
716
717 #[test]
718 fn str_to_u64_basic() {
719 let node = StrToU64::new();
720 let mut out = [Value::None];
721 node.eval(&[Value::Str("0".into())], &mut out);
722 assert_eq!(out[0].as_u64(), 0);
723 node.eval(&[Value::Str("42".into())], &mut out);
724 assert_eq!(out[0].as_u64(), 42);
725 node.eval(&[Value::Str("18446744073709551615".into())], &mut out);
726 assert_eq!(out[0].as_u64(), u64::MAX);
727 }
728
729 #[test]
730 fn str_to_u64_trims_whitespace() {
731 let node = StrToU64::new();
732 let mut out = [Value::None];
733 node.eval(&[Value::Str(" 42 ".into())], &mut out);
734 assert_eq!(out[0].as_u64(), 42);
735 }
736
737 #[test]
738 #[should_panic(expected = "__str_to_u64")]
739 fn str_to_u64_panics_on_negative() {
740 let node = StrToU64::new();
741 let mut out = [Value::None];
742 node.eval(&[Value::Str("-1".into())], &mut out);
743 }
744
745 #[test]
746 #[should_panic(expected = "__str_to_u64")]
747 fn str_to_u64_panics_on_garbage() {
748 let node = StrToU64::new();
749 let mut out = [Value::None];
750 node.eval(&[Value::Str("abc".into())], &mut out);
751 }
752
753 #[test]
754 fn str_to_f64_basic() {
755 let node = StrToF64::new();
756 let mut out = [Value::None];
757 node.eval(&[Value::Str("0.0".into())], &mut out);
758 assert_eq!(out[0].as_f64(), 0.0);
759 node.eval(&[Value::Str("3.14".into())], &mut out);
760 assert!((out[0].as_f64() - 3.14).abs() < 1e-12);
761 node.eval(&[Value::Str("-2.5e3".into())], &mut out);
762 assert_eq!(out[0].as_f64(), -2500.0);
763 node.eval(&[Value::Str("inf".into())], &mut out);
764 assert!(out[0].as_f64().is_infinite());
765 }
766
767 #[test]
768 fn str_to_f64_trims_whitespace() {
769 let node = StrToF64::new();
770 let mut out = [Value::None];
771 node.eval(&[Value::Str(" 1.5 ".into())], &mut out);
772 assert_eq!(out[0].as_f64(), 1.5);
773 }
774
775 #[test]
776 #[should_panic(expected = "__str_to_f64")]
777 fn str_to_f64_panics_on_garbage() {
778 let node = StrToF64::new();
779 let mut out = [Value::None];
780 node.eval(&[Value::Str("not-a-number".into())], &mut out);
781 }
782
783 #[test]
787 fn narrowing_casts_saturate() {
788 let t = TruncU64::new();
789 let r = RoundU64::new();
790 let mut out = [Value::None];
791 t.eval(&[Value::F64(900.9)], &mut out);
792 assert_eq!(out[0].as_u64(), 900, "trunc drops the fraction");
793 r.eval(&[Value::F64(900.9)], &mut out);
794 assert_eq!(out[0].as_u64(), 901, "round goes to nearest");
795 t.eval(&[Value::F64(-5.0)], &mut out);
796 assert_eq!(out[0].as_u64(), 0, "negative saturates to 0");
797 r.eval(&[Value::F64(f64::NAN)], &mut out);
798 assert_eq!(out[0].as_u64(), 0, "NaN saturates to 0");
799 t.eval(&[Value::F64(f64::INFINITY)], &mut out);
800 assert_eq!(out[0].as_u64(), u64::MAX, "overflow saturates to MAX");
801 }
802}