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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// SPDX-FileCopyrightText: 2023 Xiretza <xiretza@xiretza.xyz>
// SPDX-License-Identifier: LGPL-3.0-or-later
use std::{
    fmt::{Display, Formatter},
    path::{Path, PathBuf},
    str::FromStr,
};

use url::Url;

use crate::Error;

/// Represents a single (relative) filename, without any directories.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Filename {
    inner: PathBuf,
}

impl Filename {
    /// Checks that the given string is a single filename.
    ///
    /// ## Errors
    ///
    /// Returns an error if the string contains directories, is absolute, or is otherwise an
    /// invalid path.
    pub fn new(s: String) -> Result<Self, Error> {
        if s.is_empty() || s.contains([std::path::MAIN_SEPARATOR, '\0']) {
            Err(Error::InvalidFilename(s))
        } else {
            Ok(Self { inner: s.into() })
        }
    }

    /// Returns a reference to the filename as a `&str`.
    pub fn as_str(&self) -> &str {
        // Can only be constructed from valid strings
        self.inner.as_os_str().to_str().unwrap()
    }

    /// Consumes the `Filename` and returns the filename as a `String`.
    pub fn into_string(self) -> String {
        // Can only be constructed from valid strings
        self.inner.into_os_string().into_string().unwrap()
    }

    /// Returns a reference to the filename as a `&Path`.
    pub fn inner(&self) -> &Path {
        &self.inner
    }

    /// Consumes the `Filename` and returns the filename as a `PathBuf`.
    pub fn into_inner(self) -> PathBuf {
        self.inner
    }
}

impl FromStr for Filename {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s.to_owned())
    }
}

/// Represents the location that a source file should be retrieved from - either a local file (next
/// to the PKGBUILD) or a URL.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SourceLocation {
    File(Filename),
    Url(Url),
}

impl SourceLocation {
    /// Parses a source location from a string. It must be either a valid URL or a plain
    /// filename.
    pub fn new(s: &str) -> Result<Self, Error> {
        match s.parse() {
            Ok(url) => Ok(Self::Url(url)),
            Err(url::ParseError::RelativeUrlWithoutBase) => {
                Filename::new(s.to_owned()).map(Self::File)
            }
            Err(_e) => Err(Error::InvalidUrl(s.to_owned())),
        }
    }
}

impl FromStr for SourceLocation {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s)
    }
}

/// Represents a source directive. Consists of an optional local filename and a [`SourceLocation`]
/// to get the file from.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Source {
    pub filename: Option<Filename>,
    pub location: SourceLocation,
}

impl Source {
    /// Parses a source directive. It is either a filename (in the same directory as the PKGBUILD)
    /// or a url, optionally prefixed by a destination file name (separated by `::`).
    ///
    /// ## Errors
    ///
    /// This function returns an error if the destination file name or url/source file name are
    /// malformed.
    ///
    /// ## Examples
    ///
    /// ```
    /// use alpm_types::{Source, SourceLocation};
    /// use url::Url;
    ///
    /// let source = Source::new("foopkg-1.2.3.tar.gz::https://example.com/download").unwrap();
    /// assert_eq!(source.filename.unwrap().as_str(), "foopkg-1.2.3.tar.gz");
    /// let SourceLocation::Url(url) = source.location else { panic!() };
    /// assert_eq!(url.host_str(), Some("example.com"));
    /// ```
    pub fn new(s: &str) -> Result<Self, Error> {
        if let Some((filename, loc)) = s.split_once("::") {
            Ok(Self {
                filename: Some(filename.parse()?),
                location: loc.parse()?,
            })
        } else {
            Ok(Self {
                filename: None,
                location: s.parse()?,
            })
        }
    }
}

impl FromStr for Source {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s)
    }
}

impl Display for Source {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if let Some(filename) = &self.filename {
            write!(f, "{}::", filename.as_str())?;
        }
        match &self.location {
            SourceLocation::File(file) => write!(f, "{}", file.as_str()),
            SourceLocation::Url(u) => write!(f, "{u}"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use rstest::rstest;

    #[rstest]
    #[case("bikeshed_colour.patch", Ok(Filename {
        inner: PathBuf::from("bikeshed_colour.patch"),
    }))]
    #[case("c:foo", Ok(Filename {
        inner: PathBuf::from("c:foo"),
    }))]
    #[case("./bikeshed_colour.patch", Err(Error::InvalidFilename("./bikeshed_colour.patch".to_owned())))]
    #[case("", Err(Error::InvalidFilename("".to_owned())))]
    #[case("with\0null", Err(Error::InvalidFilename("with\0null".to_owned())))]
    fn parse_filename(#[case] input: &str, #[case] expected: Result<Filename, Error>) {
        let filename = input.parse();
        assert_eq!(filename, expected);

        if let Ok(filename) = filename {
            assert_eq!(filename.as_str(), input);
        }
    }

    #[rstest]
    #[case("bikeshed_colour.patch", Ok(Source {
        filename: None,
        location: SourceLocation::File("bikeshed_colour.patch".parse().unwrap()),
    }))]
    #[case("renamed::local", Ok(Source {
        filename: Some("renamed".parse().unwrap()),
        location: SourceLocation::File("local".parse().unwrap()),
    }))]
    #[case("foo-1.2.3.tar.gz::https://example.com/download", Ok(Source {
        filename: Some("foo-1.2.3.tar.gz".parse().unwrap()),
        location: SourceLocation::Url(Url::parse("https://example.com/download").unwrap()),
    }))]
    #[case("my-git-repo::git+https://example.com/project/repo.git#commit=deadbeef?signed", Ok(Source {
        filename: Some("my-git-repo".parse().unwrap()),
        location: SourceLocation::Url(Url::parse("git+https://example.com/project/repo.git#commit=deadbeef?signed").unwrap()),
    }))]
    #[case("file:///somewhere/else", Ok(Source {
        filename: None,
        location: SourceLocation::Url(Url::parse("file:///somewhere/else").unwrap()),
    }))]
    #[case("/absolute/path", Err(Error::InvalidFilename("/absolute/path".to_owned())))]
    #[case("foo:::/absolute/path", Err(Error::InvalidFilename(":/absolute/path".to_owned())))]
    fn parse_source(#[case] input: &str, #[case] expected: Result<Source, Error>) {
        let source = input.parse();
        assert_eq!(source, expected);

        if let Ok(source) = source {
            assert_eq!(source.to_string(), input);
        }
    }
}