forked from hexpm/hexdocs-search
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.gleam
More file actions
30 lines (26 loc) · 879 Bytes
/
version.gleam
File metadata and controls
30 lines (26 loc) · 879 Bytes
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
import gleam/option.{type Option, None, Some}
import gleam/regexp
const version_regexp = "^#([a-zA-Z_0-9]+)(:((([0-9]+|\\.){1,5})|latest))?"
pub fn match_package(word: String) -> Result(#(String, Option(String)), Nil) {
let regexp = version_search()
case regexp.scan(regexp, word) {
[regexp.Match(content: _, submatches:)] -> {
case submatches {
[Some(package), _, Some(version), ..] -> Ok(#(package, Some(version)))
[Some(package)] -> Ok(#(package, None))
_ -> Error(Nil)
}
}
_ -> Error(Nil)
}
}
pub fn to_string(package: #(String, String)) {
let #(package, version) = package
let package = "#" <> package
package <> ":" <> version
}
fn version_search() {
let options = regexp.Options(case_insensitive: False, multi_line: False)
let assert Ok(regexp) = regexp.compile(version_regexp, with: options)
regexp
}