Debian Codegraph @

third_party/rust/firefox-versioning/src/compare.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use crate::error::VersionParsingError;
6 use crate::version::Version;
7 use serde_json::{json, Value};
8 use std::convert::TryFrom;
9
10 pub type Result<T, E = VersionParsingError> = std::result::Result<T, E>;
11
12 pub fn version_compare(args: &[Value]) -> Result<Value> {
13 let curr_version = args.first().ok_or_else(|| {
14 VersionParsingError::ParseError("current version doesn't exist in jexl transform".into())
15 })?;
16 let curr_version = curr_version.as_str().ok_or_else(|| {
17 VersionParsingError::ParseError("current version in jexl transform is not a string".into())
18 })?;
19 let min_version = args.get(1).ok_or_else(|| {
20 VersionParsingError::ParseError("minimum version doesn't exist in jexl transform".into())
21 })?;
22 let min_version = min_version.as_str().ok_or_else(|| {
23 VersionParsingError::ParseError("minimum version is not a string in jexl transform".into())
24 })?;
25 let min_version = Version::try_from(min_version)?;
26 let curr_version = Version::try_from(curr_version)?;
27 Ok(json!(if curr_version > min_version {
28 1
29 } else if curr_version < min_version {
30 -1
31 } else {
32 0
33 }))
34 }