use crate::error::Result;
use crate::http::HttpClient;
use reqwest::Method;
use serde::{Deserialize, Serialize};
#[derive(Clone)]
pub struct WorktreeApi {
http: HttpClient,
}
impl WorktreeApi {
pub fn new(http: HttpClient) -> Self {
Self { http }
}
pub async fn create(&self, req: &CreateWorktreeRequest) -> Result<Worktree> {
let body = serde_json::to_value(req)?;
self.http
.request_json(Method::POST, "/experimental/worktree", Some(body))
.await
}
pub async fn list(&self) -> Result<Vec<Worktree>> {
self.http
.request_json(Method::GET, "/experimental/worktree", None)
.await
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateWorktreeRequest {
pub branch: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Worktree {
pub path: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub branch: Option<String>,
#[serde(default)]
pub is_main: bool,
}