Skip to main content

gitlab/api/projects/repository/files/
file.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7use derive_builder::Builder;
8
9use crate::api::common::{self, NameOrId};
10use crate::api::endpoint_prelude::*;
11
12/// Get a single file from repository.
13#[derive(Debug, Builder, Clone)]
14pub struct File<'a> {
15    /// The project to get a file within.
16    #[builder(setter(into))]
17    project: NameOrId<'a>,
18    /// The path to the file in the repository.
19    ///
20    /// This is automatically escaped as needed.
21    #[builder(setter(into))]
22    file_path: Cow<'a, str>,
23    /// The ref to get a file from.
24    #[builder(setter(into))]
25    ref_: Cow<'a, str>,
26}
27
28impl<'a> File<'a> {
29    /// Create a builder for the endpoint.
30    pub fn builder() -> FileBuilder<'a> {
31        FileBuilder::default()
32    }
33}
34
35impl Endpoint for File<'_> {
36    fn method(&self) -> Method {
37        Method::GET
38    }
39
40    fn endpoint(&self) -> Cow<'static, str> {
41        format!(
42            "projects/{}/repository/files/{}",
43            self.project,
44            common::path_escaped(&self.file_path),
45        )
46        .into()
47    }
48
49    fn parameters(&self) -> QueryParams<'_> {
50        let mut params = QueryParams::default();
51
52        params.push("ref", &self.ref_);
53
54        params
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use http::Method;
61
62    use crate::api::projects::repository::files::{File, FileBuilderError};
63    use crate::api::{self, Query};
64    use crate::test::client::{ExpectedUrl, SingleTestClient};
65
66    #[test]
67    fn all_parameters_are_needed() {
68        let err = File::builder().build().unwrap_err();
69        crate::test::assert_missing_field!(err, FileBuilderError, "project");
70    }
71
72    #[test]
73    fn project_is_required() {
74        let err = File::builder()
75            .file_path("new/file")
76            .ref_("master")
77            .build()
78            .unwrap_err();
79        crate::test::assert_missing_field!(err, FileBuilderError, "project");
80    }
81
82    #[test]
83    fn file_path_is_required() {
84        let err = File::builder()
85            .project(1)
86            .ref_("master")
87            .build()
88            .unwrap_err();
89        crate::test::assert_missing_field!(err, FileBuilderError, "file_path");
90    }
91
92    #[test]
93    fn ref_is_required() {
94        let err = File::builder()
95            .project(1)
96            .file_path("new/file")
97            .build()
98            .unwrap_err();
99        crate::test::assert_missing_field!(err, FileBuilderError, "ref_");
100    }
101
102    #[test]
103    fn sufficient_parameters() {
104        File::builder()
105            .project(1)
106            .file_path("new/file")
107            .ref_("master")
108            .build()
109            .unwrap();
110    }
111
112    #[test]
113    fn endpoint() {
114        let endpoint = ExpectedUrl::builder()
115            .method(Method::GET)
116            .endpoint("projects/simple%2Fproject/repository/files/path%2Fto%2Ffile")
117            .add_query_params(&[("ref", "branch")])
118            .build()
119            .unwrap();
120        let client = SingleTestClient::new_raw(endpoint, "");
121
122        let endpoint = File::builder()
123            .project("simple/project")
124            .file_path("path/to/file")
125            .ref_("branch")
126            .build()
127            .unwrap();
128        api::ignore(endpoint).query(&client).unwrap();
129    }
130}