diff options
| author | jacksonfu14 <jackson@fashionunited.com> | 2025-11-06 09:06:23 +0100 |
|---|---|---|
| committer | Jackson Moore <jacksonmoore@tuta.io> | 2025-11-06 09:06:23 +0100 |
| commit | 52d6f51b11ad7144ecef02ecd8710528076a67a4 (patch) | |
| tree | fd6e84925f5cac26adb3405be19b072f0a9176d8 /morphology/src | |
| parent | 743bf0f56e566f9d7b7671805a81704c3b7b14c2 (diff) | |
feat: pretty print meta-affine values
Diffstat (limited to 'morphology/src')
| -rw-r--r-- | morphology/src/value.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/morphology/src/value.rs b/morphology/src/value.rs index 71fc2c6..51e9e40 100644 --- a/morphology/src/value.rs +++ b/morphology/src/value.rs @@ -68,6 +68,43 @@ impl Value { pub const fn as_tuple(&self) -> (i32, i32) { (self.outer, self.inner) } + + /// Returns a pretty-printed representation of the value. + /// + /// For a value (outer, inner): + /// - If outer is positive, inner is nested in outer pairs of parentheses + /// - If outer is negative, inner is nested in |outer| pairs of square brackets + /// - If outer is 0, returns just the inner value + /// + /// # Examples + /// + /// ``` + /// use meta_affine::Value; + /// assert_eq!(Value::new(0, 0).pretty_print(), "0"); + /// assert_eq!(Value::new(0, 1).pretty_print(), "1"); + /// assert_eq!(Value::new(1, 0).pretty_print(), "(0)"); + /// assert_eq!(Value::new(-1, 0).pretty_print(), "[0]"); + /// assert_eq!(Value::new(2, 1).pretty_print(), "((1))"); + /// assert_eq!(Value::new(-3, 2).pretty_print(), "[[[2]]]"); + /// ``` + pub fn pretty_print(&self) -> String { + let mut result = self.inner.to_string(); + + if self.outer > 0 { + // Positive outer: nest in parentheses + for _ in 0..self.outer { + result = format!("({})", result); + } + } else if self.outer < 0 { + // Negative outer: nest in square brackets + for _ in 0..self.outer.abs() { + result = format!("[{}]", result); + } + } + // If outer == 0, just return the inner value as-is + + result + } } impl fmt::Display for Value { |