docker_client_async/version.rs
1/*
2 * Copyright 2020 Damian Peckett <damian@pecke.tt>.
3 * Copyright 2013-2018 Docker, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18//! Docker client versioning utilities.
19
20use std::cmp;
21
22/// compare compares two version strings.
23/// returns -1 if v1 < v2, 1 if v1 > v2, 0 otherwise.
24fn compare(v1: &str, v2: &str) -> i32 {
25 let curr_tab = v1.split('.').collect::<Vec<_>>();
26 let other_tab = v2.split('.').collect::<Vec<_>>();
27 for i in 0..cmp::max(curr_tab.len(), other_tab.len()) {
28 let mut curr_int = 0i32;
29 let mut other_int = 0i32;
30
31 if curr_tab.len() > i {
32 curr_int = curr_tab[i].parse().unwrap();
33 }
34 if other_tab.len() > i {
35 other_int = other_tab[i].parse().unwrap();
36 }
37 if curr_int > other_int {
38 return 1;
39 }
40 if other_int > curr_int {
41 return -1;
42 }
43 }
44 0
45}
46
47/// less_than checks if a version is less than another.
48pub fn less_than(v: &str, other: &str) -> bool {
49 compare(v, other) == -1
50}
51
52/// less_than_or_equal_to checks if a version is less than or equal to another.
53pub fn less_than_or_equal_to(v: &str, other: &str) -> bool {
54 compare(v, other) <= 0
55}
56
57/// greater_than checks if a version is greater than another
58pub fn greater_than(v: &str, other: &str) -> bool {
59 compare(v, other) == 1
60}
61
62/// greater_than_or_equal_to checks if a version is greater than or equal to another
63pub fn greater_than_or_equal_to(v: &str, other: &str) -> bool {
64 compare(v, other) >= 0
65}
66
67/// equal checks if a version is equal to another
68pub fn equal(v: &str, other: &str) -> bool {
69 compare(v, other) == 0
70}