Skip to content
This repository has been archived by the owner on Dec 17, 2022. It is now read-only.

Commit

Permalink
feat: Actually upgrade dprint-core.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Aug 4, 2020
1 parent 1b09a78 commit 22860d8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -11,7 +11,7 @@ crate-type = ["lib", "cdylib"]
wasm = ["serde_json", "dprint-core/wasm"]

[dependencies]
dprint-core = { version = "0.25.0", features = [] }
dprint-core = { version = "0.26.2", features = [] }
rustfmt-nightly = { version = "=1.4.18" }
serde = { version = "1.0.88", features = ["derive"] }
serde_json = { version = "1.0", optional = true }
Expand Down
40 changes: 28 additions & 12 deletions src/config.rs
@@ -1,21 +1,20 @@
use rustfmt_nightly::{Config, NewlineStyle, EmitMode, Edition};

use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use dprint_core::configuration::{GlobalConfiguration, ResolveConfigurationResult, NewLineKind, ConfigurationDiagnostic};
use dprint_core::configuration::{GlobalConfiguration, ResolveConfigurationResult, NewLineKind, ConfigurationDiagnostic, ConfigKeyMap, ConfigKeyValue};

#[derive(Clone, Serialize, Deserialize)]
pub struct Configuration {
// Unfortunately no resolved configuration at the moment because serializing
// rustfmt's PartialConfig configuration kept causing a panic
#[serde(flatten)]
pub(crate) config: HashMap<String, String>,
pub(crate) config: ConfigKeyMap,
#[serde(skip_serializing, skip_deserializing)]
pub(crate) rustfmt_config: Config,
}

pub fn resolve_config(
config: HashMap<String, String>,
config: ConfigKeyMap,
global_config: &GlobalConfiguration,
) -> ResolveConfigurationResult<Configuration> {
let mut rustfmt_config = Config::default();
Expand Down Expand Up @@ -44,15 +43,23 @@ pub fn resolve_config(

for (key, value) in config.iter() {
if key == "newLineKind" {
match value.as_str() {
"auto" => rustfmt_config.set().newline_style(NewlineStyle::Auto),
"lf" => rustfmt_config.set().newline_style(NewlineStyle::Unix),
"crlf" => rustfmt_config.set().newline_style(NewlineStyle::Windows),
"system" => rustfmt_config.set().newline_style(NewlineStyle::Native),
match value {
ConfigKeyValue::String(value) => match value.as_str() {
"auto" => rustfmt_config.set().newline_style(NewlineStyle::Auto),
"lf" => rustfmt_config.set().newline_style(NewlineStyle::Unix),
"crlf" => rustfmt_config.set().newline_style(NewlineStyle::Windows),
"system" => rustfmt_config.set().newline_style(NewlineStyle::Native),
_ => {
diagnostics.push(ConfigurationDiagnostic {
property_name: String::from(key),
message: format!("Invalid newline kind: {}", value),
});
}
},
_ => {
diagnostics.push(ConfigurationDiagnostic {
property_name: String::from(key),
message: format!("Invalid newline kind: {}", value),
message: String::from("Newline kind must be a string."),
});
}
}
Expand All @@ -65,8 +72,9 @@ pub fn resolve_config(
"indentWidth" => "tab_spaces",
_ => key,
};
if Config::is_valid_key_val(key, value) {
rustfmt_config.override_value(key, value);
let value = key_value_to_string(value);
if Config::is_valid_key_val(key, &value) {
rustfmt_config.override_value(key, &value);
} else {
let message = format!("Invalid key or value in configuration. Key: {}, Value: {}", key, value);
diagnostics.push(ConfigurationDiagnostic {
Expand All @@ -83,3 +91,11 @@ pub fn resolve_config(
config: Configuration { config, rustfmt_config },
}
}

fn key_value_to_string(value: &ConfigKeyValue) -> String {
match value {
ConfigKeyValue::String(value) => value.clone(),
ConfigKeyValue::Number(value) => value.to_string(),
ConfigKeyValue::Bool(value) => value.to_string(),
}
}

0 comments on commit 22860d8

Please sign in to comment.