docker-client-async 0.1.0

A modern async/await Docker client written in Rust.
Documentation
/*
 * Copyright 2020 Damian Peckett <damian@pecke.tt>.
 * Copyright 2013-2018 Docker, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! Docker client versioning utilities.

use std::cmp;

/// compare compares two version strings.
/// returns -1 if v1 < v2, 1 if v1 > v2, 0 otherwise.
fn compare(v1: &str, v2: &str) -> i32 {
    let curr_tab = v1.split('.').collect::<Vec<_>>();
    let other_tab = v2.split('.').collect::<Vec<_>>();
    for i in 0..cmp::max(curr_tab.len(), other_tab.len()) {
        let mut curr_int = 0i32;
        let mut other_int = 0i32;

        if curr_tab.len() > i {
            curr_int = curr_tab[i].parse().unwrap();
        }
        if other_tab.len() > i {
            other_int = other_tab[i].parse().unwrap();
        }
        if curr_int > other_int {
            return 1;
        }
        if other_int > curr_int {
            return -1;
        }
    }
    0
}

/// less_than checks if a version is less than another.
pub fn less_than(v: &str, other: &str) -> bool {
    compare(v, other) == -1
}

/// less_than_or_equal_to checks if a version is less than or equal to another.
pub fn less_than_or_equal_to(v: &str, other: &str) -> bool {
    compare(v, other) <= 0
}

/// greater_than checks if a version is greater than another
pub fn greater_than(v: &str, other: &str) -> bool {
    compare(v, other) == 1
}

/// greater_than_or_equal_to checks if a version is greater than or equal to another
pub fn greater_than_or_equal_to(v: &str, other: &str) -> bool {
    compare(v, other) >= 0
}

/// equal checks if a version is equal to another
pub fn equal(v: &str, other: &str) -> bool {
    compare(v, other) == 0
}