Repository: casey/just (rust)

Issue:
Formatter strips `[private]` attribute from variables
Given a `justfile` like:

```just
[private]
foo := 'one'
bar := 'two'
_baz := 'three'

@test:
    echo {{ foo }}
    echo {{ bar }}
    echo {{ _baz }}
```
Running the formatter results in the `[private]` attribute being removed.
```sh
$ just --unstable --fmt --check
-[private]
 foo := 'one'
 bar := 'two'
 _baz := 'three'

 @test:
     echo {{ foo }}
     echo {{ bar }}
     echo {{ _baz }}
error: Formatted justfile differs from original.
```

Pull request description:
Keep `[private]` attribute when formatting assignments
Fixes #2591.

Patch:
diff --git a/src/assignment.rs b/src/assignment.rs
index 5822fc6a..c41ecbab 100644
--- a/src/assignment.rs
+++ b/src/assignment.rs
@@ -5,9 +5,14 @@ pub(crate) type Assignment<'src> = Binding<'src, Expression<'src>>;
 
 impl Display for Assignment<'_> {
   fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+    if self.private {
+      writeln!(f, "[private]")?;
+    }
+
     if self.export {
       write!(f, "export ")?;
     }
+
     write!(f, "{} := {}", self.name, self.value)
   }
 }


Test patch:
diff --git a/tests/format.rs b/tests/format.rs
index 93ccd996..75da38ba 100644
--- a/tests/format.rs
+++ b/tests/format.rs
@@ -1155,3 +1155,22 @@ fn if_else() {
     )
     .run();
 }
+
+#[test]
+fn private_variable() {
+  Test::new()
+    .justfile(
+      "
+        [private]
+        foo := 'bar'
+      ",
+    )
+    .arg("--dump")
+    .stdout(
+      "
+        [private]
+        foo := 'bar'
+      ",
+    )
+    .run();
+}