parley-cli 0.2.0

Terminal-first review tool for AI-generated code changes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use crate::domain::config::AppConfig;
use crate::domain::review::ReviewSession;
use std::io::Error;
use std::io::ErrorKind;
use std::path::Path;
use std::path::PathBuf;
use tokio::fs;

#[derive(Debug, thiserror::Error)]
pub enum StoreError {
    #[error("invalid review name: {0}")]
    InvalidReviewName(String),
    #[error("review not found: {0}")]
    ReviewNotFound(String),
    #[error("io error: {0}")]
    Io(#[from] Error),
    #[error("json error: {0}")]
    Json(#[from] serde_json::Error),
    #[error("toml deserialize error: {0}")]
    TomlDeserialize(#[from] toml::de::Error),
    #[error("toml serialize error: {0}")]
    TomlSerialize(#[from] toml::ser::Error),
}

pub type StoreResult<T> = Result<T, StoreError>;

#[derive(Debug, Clone)]
pub struct Store {
    root: PathBuf,
}

impl Store {
    pub fn from_project_root(project_root: impl AsRef<Path>) -> Self {
        Self {
            root: project_root.as_ref().join(".parley"),
        }
    }

    /// # Errors
    ///
    /// Returns an error when the `.parley` review directories cannot be created.
    pub async fn ensure_dirs(&self) -> StoreResult<()> {
        fs::create_dir_all(self.reviews_dir()).await?;
        Ok(())
    }

    /// # Errors
    ///
    /// Returns an error when the review name is invalid or the review cannot be written.
    pub async fn create_review(&self, session: &ReviewSession) -> StoreResult<()> {
        self.save_review(session).await
    }

    /// # Errors
    ///
    /// Returns an error when the review name is invalid, directories cannot be created, the session
    /// cannot be serialized, or the review file cannot be written.
    pub async fn save_review(&self, session: &ReviewSession) -> StoreResult<()> {
        self.ensure_dirs().await?;

        let path = self.review_path(&session.name)?;
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).await?;
        }
        let data = serde_json::to_vec_pretty(session)?;
        fs::write(path, data).await?;
        Ok(())
    }

    /// # Errors
    ///
    /// Returns an error when the review name is invalid, the review is missing, or review data
    /// cannot be read or deserialized.
    pub async fn load_review(&self, name: &str) -> StoreResult<ReviewSession> {
        let review_path = self.review_path(name)?;
        if let Some(review) = read_review_file(&review_path).await? {
            return Ok(review);
        }

        if let Some(review) = self.load_legacy_review(name).await? {
            return Ok(review);
        }

        Err(StoreError::ReviewNotFound(name.to_string()))
    }

    /// # Errors
    ///
    /// Returns an error when review directories cannot be read or persisted review files cannot be
    /// deserialized.
    pub async fn list_reviews(&self) -> StoreResult<Vec<String>> {
        self.ensure_dirs().await?;
        let mut dir = fs::read_dir(self.reviews_dir()).await?;
        let mut result = Vec::new();

        while let Some(entry) = dir.next_entry().await? {
            let path = entry.path();
            let file_type = entry.file_type().await?;
            if file_type.is_dir() {
                let review_path = path.join("review.json");
                if let Some(review) = read_review_file(&review_path).await? {
                    result.push(review.name);
                }
            } else if let Some(name) = self.legacy_review_name(&path).await? {
                result.push(name);
            }
        }

        result.sort_unstable();
        result.dedup();
        Ok(result)
    }

    /// # Errors
    ///
    /// Returns an error when `review_name` is invalid.
    pub fn review_log_path(&self, review_name: &str) -> StoreResult<PathBuf> {
        Ok(self.review_dir(review_name)?.join("logs").join("tui.log"))
    }

    fn review_path(&self, name: &str) -> StoreResult<PathBuf> {
        Ok(self.review_dir(name)?.join("review.json"))
    }

    fn review_dir(&self, name: &str) -> StoreResult<PathBuf> {
        Ok(self.reviews_dir().join(normalize_review_name(name)?))
    }

    fn reviews_dir(&self) -> PathBuf {
        self.root.join("reviews")
    }

    /// # Errors
    ///
    /// Returns an error when config directories cannot be created or config data cannot be read or
    /// deserialized.
    pub async fn load_config(&self) -> StoreResult<AppConfig> {
        self.ensure_dirs().await?;
        let path = self.config_path();

        let Some(bytes) = read_optional_file(&path).await? else {
            return Ok(AppConfig::default());
        };
        let text = String::from_utf8(bytes).map_err(|error| {
            StoreError::Io(Error::new(
                ErrorKind::InvalidData,
                format!("invalid utf-8 in config.toml: {error}"),
            ))
        })?;
        Ok(toml::from_str(&text)?)
    }

    /// # Errors
    ///
    /// Returns an error when config directories cannot be created, config data cannot be serialized,
    /// or the config file cannot be written.
    pub async fn save_config(&self, config: &AppConfig) -> StoreResult<()> {
        self.ensure_dirs().await?;
        let data = toml::to_string_pretty(config)?;
        fs::write(self.config_path(), data).await?;
        Ok(())
    }

    fn config_path(&self) -> PathBuf {
        self.root.join("config.toml")
    }

    // Legacy compatibility for flat review files that predate per-review directories.
    async fn load_legacy_review(&self, name: &str) -> StoreResult<Option<ReviewSession>> {
        let legacy_path = self.legacy_review_path(name)?;
        read_review_file(&legacy_path).await
    }

    async fn legacy_review_name(&self, path: &Path) -> StoreResult<Option<String>> {
        if path.extension().and_then(|value| value.to_str()) != Some("json") {
            return Ok(None);
        }

        let Some(stem) = path.file_stem().and_then(|value| value.to_str()) else {
            return Ok(None);
        };

        let normalized_path = self.review_path(stem)?;
        if fs::try_exists(normalized_path).await? {
            Ok(None)
        } else {
            Ok(Some(stem.to_string()))
        }
    }

    fn legacy_review_path(&self, name: &str) -> StoreResult<PathBuf> {
        validate_review_name(name)?;
        Ok(self.reviews_dir().join(format!("{name}.json")))
    }
}

async fn read_review_file(path: &Path) -> StoreResult<Option<ReviewSession>> {
    let Some(bytes) = read_optional_file(path).await? else {
        return Ok(None);
    };
    Ok(Some(serde_json::from_slice(&bytes)?))
}

async fn read_optional_file(path: &Path) -> StoreResult<Option<Vec<u8>>> {
    match fs::read(path).await {
        Ok(bytes) => Ok(Some(bytes)),
        Err(error) if error.kind() == ErrorKind::NotFound => Ok(None),
        Err(error) => Err(StoreError::Io(error)),
    }
}

/// # Errors
///
/// Returns an error when the review name is empty after trimming or contains unsupported
/// characters.
pub fn normalize_review_name(name: &str) -> StoreResult<String> {
    validate_review_name(name)?;
    let normalized = name.trim_matches(|ch| matches!(ch, '_' | '.')).to_string();
    if normalized.is_empty() {
        return Err(StoreError::InvalidReviewName(name.to_string()));
    }
    Ok(normalized)
}

/// # Errors
///
/// Returns an error when the review name is empty or contains characters other than ASCII
/// alphanumerics, `.`, `_`, or `-`.
pub fn validate_review_name(name: &str) -> StoreResult<()> {
    if name.is_empty() {
        return Err(StoreError::InvalidReviewName(name.to_string()));
    }

    if name
        .chars()
        .all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '.' | '_' | '-'))
    {
        Ok(())
    } else {
        Err(StoreError::InvalidReviewName(name.to_string()))
    }
}

#[cfg(test)]
mod tests {
    use crate::domain::config::{AiConfig, DiffViewMode};
    use crate::domain::review::{
        Author, DiffSide, NewLineComment, SourceAnchorSnapshot, StoredAnchorSnapshot,
    };
    use anyhow::Result;
    use tempfile::tempdir;

    #[tokio::test]
    async fn save_and_load_review_should_round_trip() -> Result<()> {
        let tmp = tempdir()?;
        let store = super::Store::from_project_root(tmp.path());
        let review = super::ReviewSession::new("r1".into(), 1);

        store.save_review(&review).await?;
        let loaded = store.load_review("r1").await?;

        assert_eq!(loaded.name, "r1");
        assert_eq!(loaded.state, review.state);
        Ok(())
    }

    #[tokio::test]
    async fn save_review_should_use_normalized_review_directory() -> Result<()> {
        let tmp = tempdir()?;
        let store = super::Store::from_project_root(tmp.path());
        let review = super::ReviewSession::new("__r1__".into(), 1);

        store.save_review(&review).await?;

        let path = tmp.path().join(".parley/reviews/r1/review.json");
        assert!(path.exists());
        Ok(())
    }

    #[tokio::test]
    async fn load_and_list_reviews_should_support_legacy_flat_files() -> Result<()> {
        let tmp = tempdir()?;
        let store = super::Store::from_project_root(tmp.path());
        store.ensure_dirs().await?;
        let review = super::ReviewSession::new("legacy".into(), 1);
        let data = serde_json::to_vec_pretty(&review)?;
        tokio::fs::write(tmp.path().join(".parley/reviews/legacy.json"), data).await?;

        let loaded = store.load_review("legacy").await?;
        let reviews = store.list_reviews().await?;

        assert_eq!(loaded.name, "legacy");
        assert_eq!(reviews, vec!["legacy"]);
        Ok(())
    }

    #[tokio::test]
    async fn load_review_should_default_missing_original_anchor() -> Result<()> {
        let tmp = tempdir()?;
        let store = super::Store::from_project_root(tmp.path());
        store.ensure_dirs().await?;
        let review_dir = tmp.path().join(".parley/reviews/old");
        super::fs::create_dir_all(&review_dir).await?;
        super::fs::write(
            review_dir.join("review.json"),
            r#"{
  "name": "old",
  "state": "open",
  "created_at_ms": 1,
  "updated_at_ms": 1,
  "comments": [
    {
      "id": 1,
      "file_path": "src/lib.rs",
      "old_line": null,
      "new_line": 1,
      "line_range": null,
      "side": "right",
      "line_anchor": null,
      "detached": false,
      "body": "old",
      "author": "user",
      "status": "open",
      "replies": [],
      "created_at_ms": 1,
      "updated_at_ms": 1,
      "addressed_at_ms": null
    }
  ],
  "next_comment_id": 2,
  "next_reply_id": 1
}"#,
        )
        .await?;

        let loaded = store.load_review("old").await?;

        assert_eq!(loaded.comments[0].original_anchor, None);
        Ok(())
    }

    #[tokio::test]
    async fn save_and_load_review_should_round_trip_original_anchor() -> Result<()> {
        let tmp = tempdir()?;
        let store = super::Store::from_project_root(tmp.path());
        let mut review = super::ReviewSession::new("anchored".into(), 1);
        let original_anchor = StoredAnchorSnapshot {
            file_path: "src/lib.rs".into(),
            side: DiffSide::Right,
            old_line: None,
            new_line: Some(10),
            line_range: None,
            selected_text: "let value = 1;".into(),
            before_context: vec!["fn main() {".into()],
            after_context: vec!["}".into()],
            diff: None,
            source: Some(SourceAnchorSnapshot {
                file_content_hash: Some("file-hash".into()),
                selected_text_hash: Some("text-hash".into()),
            }),
            base_rev: Some("base".into()),
            head_rev: Some("head".into()),
        };
        review.add_comment(
            NewLineComment {
                file_path: "src/lib.rs".into(),
                old_line: None,
                new_line: Some(10),
                line_range: None,
                side: DiffSide::Right,
                line_anchor: None,
                original_anchor: Some(original_anchor.clone()),
                body: "anchor".into(),
                author: Author::User,
            },
            2,
        );

        store.save_review(&review).await?;
        let loaded = store.load_review("anchored").await?;

        assert_eq!(loaded.comments[0].original_anchor, Some(original_anchor));
        Ok(())
    }

    #[test]
    fn validate_review_name_should_reject_slash() {
        let result = super::validate_review_name("bad/name");

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn save_and_load_config_should_round_trip() -> Result<()> {
        let tmp = tempdir()?;
        let store = super::Store::from_project_root(tmp.path());
        let config = super::AppConfig {
            user_name: "User".to_string(),
            theme: "nord".to_string(),
            diff_view: DiffViewMode::Unified,
            ignore_parley_dir: true,
            log_level: "debug".to_string(),
            ai: AiConfig::default(),
        };

        store.save_config(&config).await?;
        let loaded = store.load_config().await?;

        assert_eq!(loaded, config);
        Ok(())
    }

    #[tokio::test]
    async fn load_config_should_support_legacy_name_field() -> Result<()> {
        let tmp = tempdir()?;
        let store = super::Store::from_project_root(tmp.path());
        store.ensure_dirs().await?;

        super::fs::write(
            tmp.path().join(".parley").join("config.toml"),
            "name = \"User\"\ntheme = \"nord\"\n",
        )
        .await?;

        let loaded = store.load_config().await?;

        assert_eq!(loaded.user_name, "User");
        assert_eq!(loaded.theme, "nord");
        assert_eq!(loaded.diff_view, DiffViewMode::SideBySide);
        assert!(loaded.ignore_parley_dir);
        assert_eq!(loaded.log_level, "info");
        Ok(())
    }
}