gitea_sdk/api/repos/
branches.rs

1use build_it::Builder;
2use serde::Serialize;
3
4use crate::{error::Result, model::repos::Branch, Client};
5
6#[derive(Debug, Clone, Serialize, Builder)]
7#[build_it(into)]
8pub struct ListBranchesBuilder {
9    #[serde(skip)]
10    #[build_it(skip)]
11    owner: String,
12    #[serde(skip)]
13    #[build_it(skip)]
14    repo: String,
15
16    /// Page number of results to return (1-based).
17    #[serde(skip_serializing_if = "Option::is_none")]
18    page: Option<i64>,
19
20    /// Page size of results.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    limit: Option<i64>,
23}
24
25#[derive(Debug, Clone, Serialize, Builder)]
26#[build_it(into)]
27pub struct CreateBranchBuilder {
28    #[serde(skip)]
29    #[build_it(skip)]
30    owner: String,
31    #[serde(skip)]
32    #[build_it(skip)]
33    repo: String,
34    #[build_it(skip)]
35    new_branch_name: String,
36
37    old_ref_name: Option<String>,
38}
39
40#[derive(Debug, Clone, Builder)]
41#[build_it(into)]
42pub struct GetBranchBuilder {
43    #[build_it(skip)]
44    owner: String,
45    #[build_it(skip)]
46    repo: String,
47    #[build_it(skip)]
48    branch: String,
49}
50
51#[derive(Debug, Clone, Builder)]
52#[build_it(into)]
53pub struct DeleteBranchBuilder {
54    #[build_it(skip)]
55    owner: String,
56    #[build_it(skip)]
57    repo: String,
58    #[build_it(skip)]
59    branch: String,
60}
61
62impl ListBranchesBuilder {
63    pub fn new(owner: impl ToString, repo: impl ToString) -> Self {
64        Self {
65            owner: owner.to_string(),
66            repo: repo.to_string(),
67            page: None,
68            limit: None,
69        }
70    }
71    /// Sends the request to list a repository's branches.
72    pub async fn send(&self, client: &Client) -> Result<Vec<Branch>> {
73        let owner = &self.owner;
74        let repo = &self.repo;
75        let req = client
76            .get(format!("repos/{owner}/{repo}/branches"))
77            .query(&self)
78            .build()?;
79        let res = client.make_request(req).await?;
80        client.parse_response(res).await
81    }
82}
83
84impl CreateBranchBuilder {
85    pub fn new(owner: impl ToString, repo: impl ToString, new_branch_name: impl ToString) -> Self {
86        Self {
87            owner: owner.to_string(),
88            repo: repo.to_string(),
89            new_branch_name: new_branch_name.to_string(),
90            old_ref_name: None,
91        }
92    }
93    /// Sends the request to create a branch.
94    pub async fn send(&self, client: &Client) -> Result<Branch> {
95        let owner = &self.owner;
96        let repo = &self.repo;
97        let req = client
98            .post(format!("repos/{owner}/{repo}/branches"))
99            .json(&self)
100            .build()?;
101        let res = client.make_request(req).await?;
102        client.parse_response(res).await
103    }
104}
105
106impl GetBranchBuilder {
107    pub fn new(owner: impl ToString, repo: impl ToString, branch: impl ToString) -> Self {
108        Self {
109            owner: owner.to_string(),
110            repo: repo.to_string(),
111            branch: branch.to_string(),
112        }
113    }
114    /// Sends the request to get a branch.
115    pub async fn send(&self, client: &Client) -> Result<Branch> {
116        let Self {
117            owner,
118            repo,
119            branch,
120        } = self;
121        let req = client
122            .get(format!("repos/{owner}/{repo}/branches/{branch}"))
123            .build()?;
124        let res = client.make_request(req).await?;
125        client.parse_response(res).await
126    }
127}
128
129impl DeleteBranchBuilder {
130    pub fn new(owner: impl ToString, repo: impl ToString, branch: impl ToString) -> Self {
131        Self {
132            owner: owner.to_string(),
133            repo: repo.to_string(),
134            branch: branch.to_string(),
135        }
136    }
137    /// Sends the request to get a branch.
138    pub async fn send(&self, client: &Client) -> Result<()> {
139        let Self {
140            owner,
141            repo,
142            branch,
143        } = self;
144        let req = client
145            .delete(format!("repos/{owner}/{repo}/branches/{branch}"))
146            .build()?;
147        let _ = client.make_request(req).await?;
148        Ok(())
149    }
150}