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
//! Helpers to build advanced queries

use failure::Error as FailureError;
use serde;

use client_internals::path::{Name, Path as PrivatePath};
use client_internals::InternalAdvancedQueryParams;

// pub use client_internals::path::Name;
pub use client_internals::AdvancedQuery;
pub use client_internals::{error, Error};
pub use client_internals::{TreeBuilder, TreeQueryParam};

use build;

/// Path to an object in Jenkins
#[derive(Debug, PartialEq)]
pub enum Path<'a> {
    /// Path to the home
    Home,
    /// Path to a view
    View {
        /// The view name
        name: &'a str,
    },
    /// Path to a job
    Job {
        /// The job name
        name: &'a str,
        /// The job configuration
        configuration: Option<&'a str>,
    },
    /// Path to a job build
    Build {
        /// The job name
        job_name: &'a str,
        /// The build number
        number: build::BuildNumber,
        /// The build configuration
        configuration: Option<&'a str>,
    },
    /// Path to the Jenkins queue
    Queue,
    /// Path to an item in the queue
    QueueItem {
        /// The item id
        id: i32,
    },
    /// Path to a build's maven artifacts
    MavenArtifactRecord {
        /// The job name
        job_name: &'a str,
        /// The build number
        number: build::BuildNumber,
        /// The build configuration
        configuration: Option<&'a str>,
    },
    /// Path to the computers linked to Jenkins
    Computers,
    /// Path to a computer
    Computer {
        /// The computer name
        name: &'a str,
    },
    /// Unknown path
    Raw {
        /// The path itself
        path: &'a str,
    },
}

impl<'a> Into<PrivatePath<'a>> for Path<'a> {
    fn into(self) -> PrivatePath<'a> {
        match self {
            Path::Home => PrivatePath::Home,
            Path::View { name } => PrivatePath::View {
                name: Name::Name(name),
            },
            Path::Job {
                name,
                configuration,
            } => PrivatePath::Job {
                name: Name::Name(name),
                configuration: configuration.map(|v| Name::Name(v)),
            },
            Path::Build {
                job_name,
                number,
                configuration,
            } => PrivatePath::Build {
                job_name: Name::Name(job_name),
                number,
                configuration: configuration.map(|v| Name::Name(v)),
            },
            Path::Queue => PrivatePath::Queue,
            Path::QueueItem { id } => PrivatePath::QueueItem { id },
            Path::MavenArtifactRecord {
                job_name,
                number,
                configuration,
            } => PrivatePath::MavenArtifactRecord {
                job_name: Name::Name(job_name),
                number,
                configuration: configuration.map(|v| Name::Name(v)),
            },
            Path::Computers => PrivatePath::Computers,
            Path::Computer { name } => PrivatePath::Computer {
                name: Name::Name(name),
            },
            Path::Raw { path } => PrivatePath::Raw { path },
        }
    }
}

impl super::Jenkins {
    /// Get a `Path` from Jenkins, specifying the depth or tree parameters
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate failure;
    /// # extern crate serde;
    /// # #[macro_use]
    /// # extern crate serde_derive;
    /// #
    /// # extern crate jenkins_api;
    /// #
    /// # use jenkins_api::JenkinsBuilder;
    /// #
    /// #[derive(Deserialize)]
    /// #[serde(rename_all = "camelCase")]
    /// struct LastBuild {
    ///     number: u32,
    ///     duration: u32,
    ///     result: String,
    /// }
    /// #[derive(Deserialize)]
    /// #[serde(rename_all = "camelCase")]
    /// struct LastBuildOfJob {
    ///     display_name: String,
    ///     last_build: LastBuild,
    /// }
    ///
    /// # fn main() -> Result<(), failure::Error> {
    /// #    let jenkins = JenkinsBuilder::new("http://localhost:8080")
    /// #        .with_user("user", Some("password"))
    /// #        .build()?;
    /// let _: LastBuildOfJob = jenkins.get_object_as(
    ///     jenkins_api::client::Path::Job {
    ///         name: "job name",
    ///         configuration: None,
    ///     },
    ///     jenkins_api::client::TreeBuilder::new()
    ///         .with_field("displayName")
    ///         .with_field(
    ///             jenkins_api::client::TreeBuilder::object("lastBuild")
    ///                 .with_subfield("number")
    ///                 .with_subfield("duration")
    ///                 .with_subfield("result"),
    ///         )
    ///         .build(),
    /// )?;
    /// #    Ok(())
    /// # }
    /// ```
    ///
    pub fn get_object_as<Q, T>(&self, object: Path, parameters: Q) -> Result<T, FailureError>
    where
        Q: Into<Option<AdvancedQuery>>,
        for<'de> T: serde::Deserialize<'de>,
    {
        Ok(self
            .get_with_params(
                &object.into(),
                parameters.into().map(InternalAdvancedQueryParams::from),
            )?.json()?)
    }
}