feat: add normalized document model and output writer

This commit is contained in:
2026-04-08 22:47:17 +08:00
parent 0ce8786148
commit f4b21c182f
7 changed files with 236 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
use thiserror::Error;
#[derive(Debug, Error)]
pub enum AppError {
#[error("missing required environment variable: {0}")]
MissingEnv(&'static str),
#[error("api error: {0}")]
Api(String),
#[error("invalid input json: {0}")]
InvalidInputJson(String),
}
+3
View File
@@ -1,4 +1,7 @@
pub mod cli;
pub mod error;
pub mod model;
pub mod output;
pub fn run() -> anyhow::Result<()> {
Ok(())
+73
View File
@@ -0,0 +1,73 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrReviewDocument {
pub meta: PrMeta,
pub commits: Vec<CommitItem>,
pub diff_stat: DiffStat,
pub reviews: Vec<ReviewItem>,
pub threads: Vec<CommentThread>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrMeta {
pub repo: String,
pub pr_index: i64,
pub title: String,
pub state: String,
pub author: String,
pub base_branch: String,
pub head_branch: String,
pub created_at: String,
pub updated_at: String,
pub merged_at: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommitItem {
pub sha: String,
pub short_sha: String,
pub title: String,
pub author: String,
pub date: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffStat {
pub files_changed: usize,
pub additions: i64,
pub deletions: i64,
pub files: Vec<FileStat>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileStat {
pub path: String,
pub additions: i64,
pub deletions: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReviewItem {
pub id: i64,
pub state: String,
pub reviewer: String,
pub submitted_at: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommentThread {
pub thread_id: String,
pub file_path: Option<String>,
pub line: Option<i64>,
pub root_comment: CommentItem,
pub replies: Vec<CommentItem>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommentItem {
pub id: i64,
pub user: String,
pub created_at: String,
pub body: String,
}
+14
View File
@@ -0,0 +1,14 @@
use std::io::Write;
use std::path::Path;
pub fn write_output(out: Option<&Path>, content: &str) -> anyhow::Result<()> {
if let Some(path) = out {
std::fs::write(path, content)?;
return Ok(());
}
let mut stdout = std::io::stdout().lock();
stdout.write_all(content.as_bytes())?;
stdout.flush()?;
Ok(())
}