Merge branch 'master' into conditional-style

This commit is contained in:
Filip Bachul 2022-11-22 09:53:10 +01:00
commit d78a845065
6 changed files with 86 additions and 28 deletions

View File

@ -50,9 +50,6 @@ jobs:
- name: Setup | Checkout
uses: actions/checkout@v3
- name: Setup | Cache
uses: Swatinem/rust-cache@v2
- name: Setup | Rust
uses: actions-rs/toolchain@v1.0.7
with:
@ -61,6 +58,9 @@ jobs:
profile: minimal
components: clippy
- name: Setup | Cache
uses: Swatinem/rust-cache@v2
- name: Build | Lint
uses: actions-rs/cargo@v1.0.3
with:
@ -75,9 +75,6 @@ jobs:
- name: Setup | Checkout
uses: actions/checkout@v3
- name: Setup | Cache
uses: Swatinem/rust-cache@v2
- name: Setup | Rust
uses: actions-rs/toolchain@v1.0.7
with:
@ -85,6 +82,9 @@ jobs:
profile: minimal
override: true
- name: Setup | Cache
uses: Swatinem/rust-cache@v2
- name: Build | Check
run: cargo check --workspace --locked
@ -97,9 +97,6 @@ jobs:
- name: Setup | Checkout
uses: actions/checkout@v3
- name: Setup | Cache
uses: Swatinem/rust-cache@v2
- name: Setup | Rust
uses: actions-rs/toolchain@v1.0.7
with:
@ -107,6 +104,9 @@ jobs:
profile: minimal
override: true
- name: Setup | Cache
uses: Swatinem/rust-cache@v2
- name: Build | Check
run: cargo check --workspace --locked --no-default-features
@ -119,9 +119,6 @@ jobs:
- name: Setup | Checkout
uses: actions/checkout@v3
- name: Setup | Cache
uses: Swatinem/rust-cache@v2
- name: Setup | Rust
uses: actions-rs/toolchain@v1.0.7
with:
@ -129,6 +126,9 @@ jobs:
profile: minimal
override: true
- name: Setup | Cache
uses: Swatinem/rust-cache@v2
- name: Build | Check
run: cargo check --workspace --locked --all-features
@ -141,9 +141,6 @@ jobs:
- name: Setup | Checkout
uses: actions/checkout@v3
- name: Setup | Cache
uses: Swatinem/rust-cache@v2
- name: Setup | Rust
uses: actions-rs/toolchain@v1.0.7
with:
@ -151,6 +148,9 @@ jobs:
profile: minimal
override: true
- name: Setup | Cache
uses: Swatinem/rust-cache@v2
- name: Run | Generate Schema
run: cargo run --locked --features config-schema -- config-schema > .github/config-schema.json
@ -177,9 +177,6 @@ jobs:
- name: Setup | Checkout
uses: actions/checkout@v3
- name: Setup | Cache
uses: Swatinem/rust-cache@v2
# Install all the required dependencies for testing
- name: Setup | Rust
uses: actions-rs/toolchain@v1.0.7
@ -189,6 +186,9 @@ jobs:
profile: minimal
override: true
- name: Setup | Cache
uses: Swatinem/rust-cache@v2
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov

6
Cargo.lock generated
View File

@ -1917,12 +1917,12 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "open"
version = "3.0.3"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4a3100141f1733ea40b53381b0ae3117330735ef22309a190ac57b9576ea716"
checksum = "2078c0039e6a54a0c42c28faa984e115fb4c2d5bf2208f77d1961002df8576f8"
dependencies = [
"pathdiff",
"windows-sys 0.36.1",
"windows-sys 0.42.0",
]
[[package]]

View File

@ -59,7 +59,7 @@ log = { version = "0.4.17", features = ["std"] }
notify-rust = { version = "4.5.10", optional = true }
nu-ansi-term = "0.46.0"
once_cell = "1.16.0"
open = "3.0.3"
open = "3.2.0"
# update os module config and tests when upgrading os_info
os_info = "3.5.1"
path-slash = "0.2.1"

View File

@ -48,7 +48,17 @@ impl<'a, T: Deserialize<'a> + Default> ModuleConfig<'a, ValueError> for T {
/// Create `ValueDeserializer` wrapper and use it to call `Deserialize::deserialize` on it.
fn from_config(config: &'a Value) -> Result<Self, ValueError> {
let deserializer = ValueDeserializer::new(config);
T::deserialize(deserializer)
T::deserialize(deserializer).or_else(|err| {
// If the error is an unrecognized key, print a warning and run
// deserialize ignoring that error. Otherwise, just return the error
if err.to_string().contains("Unknown key") {
log::warn!("{}", err);
let deserializer2 = ValueDeserializer::new(config).with_allow_unknown_keys();
T::deserialize(deserializer2)
} else {
Err(err)
}
})
}
}
@ -582,6 +592,24 @@ mod tests {
assert_eq!(rust_config.switch_c, Switch::Off);
}
#[test]
fn test_load_unknown_key_config() {
#[derive(Clone, Default, Deserialize)]
#[serde(default)]
struct TestConfig<'a> {
pub foo: &'a str,
}
let config = toml::toml! {
foo = "test"
bar = "ignore me"
};
let rust_config = TestConfig::from_config(&config);
assert!(rust_config.is_ok());
assert_eq!(rust_config.unwrap().foo, "test");
}
#[test]
fn test_from_string() {
let config = Value::String(String::from("S"));

View File

@ -5,7 +5,7 @@ use crate::utils::get_command_string_output;
use std::path::PathBuf;
use regex::Regex;
const JAVA_VERSION_PATTERN: &str = "(?P<version>[\\d\\.]+)[^\\s]*\\s(?:built|from)";
const JAVA_VERSION_PATTERN: &str = "(?:JRE.*\\(|OpenJ9 )(?P<version>[\\d]+(?:\\.\\d+){0,2})";
/// Creates a module with the current Java version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
@ -120,15 +120,17 @@ mod tests {
fn test_parse_java_version_zulu() {
let java_8 = "OpenJDK 64-Bit Server VM (25.222-b10) for linux-amd64 JRE (Zulu 8.40.0.25-CA-linux64) (1.8.0_222-b10), built on Jul 11 2019 11:36:39 by \"zulu_re\" with gcc 4.4.7 20120313 (Red Hat 4.4.7-3)";
let java_11 = "OpenJDK 64-Bit Server VM (11.0.4+11-LTS) for linux-amd64 JRE (Zulu11.33+15-CA) (11.0.4+11-LTS), built on Jul 11 2019 21:37:17 by \"zulu_re\" with gcc 4.9.2 20150212 (Red Hat 4.9.2-6)";
let java_17 = "OpenJDK 64-Bit Server VM (17.0.5+8-LTS) for bsd-amd64 JRE (17.0.5+8-LTS) (Zulu17.38+21-CA), built on Oct 7 2022 06:03:12 by \"zulu_re\" with clang 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.17)";
assert_eq!(parse_java_version(java_8), Some("1.8.0".to_string()));
assert_eq!(parse_java_version(java_11), Some("11.0.4".to_string()));
assert_eq!(parse_java_version(java_17), Some("17.0.5".to_string()));
}
#[test]
fn test_parse_java_version_eclipse_openj9() {
let java_8 = "Eclipse OpenJ9 OpenJDK 64-bit Server VM (1.8.0_222-b10) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 8.0.222.0, built on Jul 17 2019 21:29:18 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)";
let java_11 = "Eclipse OpenJ9 OpenJDK 64-bit Server VM (11.0.4+11) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 11.0.4.0, built on Jul 17 2019 21:51:37 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)";
assert_eq!(parse_java_version(java_8), Some("1.8.0".to_string()));
assert_eq!(parse_java_version(java_8), Some("8.0.222".to_string()));
assert_eq!(parse_java_version(java_11), Some("11.0.4".to_string()));
}

View File

@ -13,6 +13,7 @@ pub struct ValueDeserializer<'de> {
value: &'de Value,
info: Option<StructInfo>,
current_key: Option<&'de str>,
error_on_ignored: bool,
}
/// When deserializing a struct, this struct stores information about the struct.
@ -28,14 +29,28 @@ impl<'de> ValueDeserializer<'de> {
value,
info: None,
current_key: None,
error_on_ignored: true,
}
}
fn with_info(value: &'de Value, info: Option<StructInfo>, current_key: &'de str) -> Self {
fn with_info(
value: &'de Value,
info: Option<StructInfo>,
current_key: &'de str,
ignored: bool,
) -> Self {
ValueDeserializer {
value,
info,
current_key: Some(current_key),
error_on_ignored: ignored,
}
}
pub fn with_allow_unknown_keys(self) -> Self {
ValueDeserializer {
error_on_ignored: false,
..self
}
}
}
@ -83,7 +98,12 @@ impl<'de> Deserializer<'de> for ValueDeserializer<'de> {
let map = MapDeserializer::new(t.iter().map(|(k, v)| {
(
k.as_str(),
ValueDeserializer::with_info(v, self.info, k.as_str()),
ValueDeserializer::with_info(
v,
self.info,
k.as_str(),
self.error_on_ignored,
),
)
}));
map.deserialize_map(visitor)
@ -131,6 +151,10 @@ impl<'de> Deserializer<'de> for ValueDeserializer<'de> {
return visitor.visit_none();
}
if !self.error_on_ignored {
return visitor.visit_none();
}
let did_you_mean = match (self.current_key, self.info) {
(Some(key), Some(StructInfo { fields, .. })) => fields
.iter()
@ -322,13 +346,17 @@ mod test {
let value = toml::toml! {
unknown_key = "foo"
};
let deserializer = ValueDeserializer::new(&value);
let deserializer = ValueDeserializer::new(&value);
let result = StarshipRootConfig::deserialize(deserializer).unwrap_err();
assert_eq!(
format!("{result}"),
"Error in 'StarshipRoot' at 'unknown_key': Unknown key"
);
let deserializer2 = ValueDeserializer::new(&value).with_allow_unknown_keys();
let result = StarshipRootConfig::deserialize(deserializer2);
assert!(result.is_ok());
}
#[test]