use dprint_core::configuration::ConfigurationDiagnostic;
use crate::utils::get_table_text;
pub fn handle_project_type_diagnostic(project_type: &Option<String>) -> Option<ConfigurationDiagnostic> {
let property_name = "projectType";
let project_type_infos = get_project_type_infos();
let has_project_type_config = match project_type {
Some(project_type) => project_type_infos.iter().any(|info| info.name.to_lowercase() == project_type.to_lowercase()),
_ => false,
};
if has_project_type_config {
None
} else {
Some(ConfigurationDiagnostic {
property_name: String::from(property_name),
message: build_message(&project_type_infos, property_name),
})
}
}
fn build_message(project_type_infos: &Vec<ProjectTypeInfo>, property_name: &str) -> String {
let mut message = String::new();
message.push_str(&format!("The '{}' property is missing in the configuration file.\n\n", property_name));
message.push_str("You may specify any of the following values and that will suppress this error:\n");
let option_texts = get_table_text(project_type_infos.iter().map(|info| (info.name, info.description)).collect(), 3);
for option_text in option_texts {
message.push_str(&format!("\n * {}", option_text))
}
message.push_str("\n\nMore information: https://dprint.dev/sponsor");
message
}
pub struct ProjectTypeInfo {
pub name: &'static str,
pub description: &'static str,
}
pub fn get_project_type_infos() -> Vec<ProjectTypeInfo> {
vec![ProjectTypeInfo {
name: "commercialEvaluation",
description: concat!(
"Dprint is formatting a project whose primary maintainer is a for-profit\n",
"company or individual and it is being evaluated for 30 days.",
)
}, ProjectTypeInfo {
name: "commercialSponsored",
description: concat!(
"Dprint is formatting a project whose primary maintainer is a for-profit\n",
"company or individual AND the primary maintainer sponsored the project.\n",
"Thank you for being part of moving this project forward!"
),
}, ProjectTypeInfo {
name: "openSource",
description: concat!(
"Dprint is formatting an open source project whose primary\n",
"maintainer is not a for-profit company (no sponsorship requirement)."
)
}, ProjectTypeInfo {
name: "educational",
description: concat!(
"Dprint is formatting a project run by a student or being used for\n",
"educational purposes (no sponsorship requirement)."
)
}, ProjectTypeInfo {
name: "nonProfit",
description: concat!(
"Dprint is formatting a project whose primary maintainer is a non-profit\n",
"organization (no sponsorship requirement)."
)
}]
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use super::handle_project_type_diagnostic;
#[test]
fn it_should_handle_when_project_type_exists() {
let result = handle_project_type_diagnostic(&Some(String::from("openSource")));
assert_eq!(result.is_none(), true);
}
#[test]
fn it_should_be_case_insensitive() {
let result = handle_project_type_diagnostic(&Some(String::from("opensource")));
assert_eq!(result.is_none(), true);
}
#[test]
fn it_should_handle_when_project_type_not_exists() {
let result = handle_project_type_diagnostic(&None);
assert_eq!(result.is_some(), true);
let result = result.unwrap();
assert_eq!(result.property_name, "projectType");
assert_eq!(result.message, r#"The 'projectType' property is missing in the configuration file.
You may specify any of the following values and that will suppress this error:
* commercialEvaluation Dprint is formatting a project whose primary maintainer is a for-profit
company or individual and it is being evaluated for 30 days.
* commercialSponsored Dprint is formatting a project whose primary maintainer is a for-profit
company or individual AND the primary maintainer sponsored the project.
Thank you for being part of moving this project forward!
* openSource Dprint is formatting an open source project whose primary
maintainer is not a for-profit company (no sponsorship requirement).
* educational Dprint is formatting a project run by a student or being used for
educational purposes (no sponsorship requirement).
* nonProfit Dprint is formatting a project whose primary maintainer is a non-profit
organization (no sponsorship requirement).
More information: https://dprint.dev/sponsor"#);
}
#[test]
fn it_should_handle_when_project_type_not_valid_option() {
let result = handle_project_type_diagnostic(&Some(String::from("test")));
assert_eq!(result.is_some(), true);
}
}