Skip to content

Commit 78b0728

Browse files
refactor: remove panics on misconfigured widths
1 parent a168d92 commit 78b0728

File tree

2 files changed

+47
-54
lines changed

2 files changed

+47
-54
lines changed

Diff for: src/config/config_type.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,12 @@ macro_rules! create_config {
309309
return heuristic_value;
310310
}
311311
if override_value > max_width {
312-
panic!("`{}` cannot have a value that exceeds `max_width`", config_key);
312+
eprintln!(
313+
"`{0}` cannot have a value that exceeds `max_width`. \
314+
`{0}` will be set to the same value as `max_width`",
315+
config_key,
316+
);
317+
return max_width;
313318
}
314319
override_value
315320
};

Diff for: src/config/mod.rs

+41-53
Original file line numberDiff line numberDiff line change
@@ -842,134 +842,122 @@ make_backup = false
842842
}
843843

844844
#[test]
845-
#[should_panic(expected = "`fn_call_width` cannot have a value that exceeds `max_width")]
846-
fn test_panics_when_fn_call_width_config_exceeds_max_width() {
845+
fn test_fn_call_width_config_exceeds_max_width() {
847846
let toml = r#"
848-
max_width = 80
849-
fn_call_width = 90
847+
max_width = 90
848+
fn_call_width = 95
850849
"#;
851-
Config::from_toml(toml, Path::new("")).unwrap();
850+
let config = Config::from_toml(toml, Path::new("")).unwrap();
851+
assert_eq!(config.fn_call_width(), 90);
852852
}
853853

854854
#[test]
855-
#[should_panic(
856-
expected = "`attr_fn_like_width` cannot have a value that exceeds `max_width"
857-
)]
858-
fn test_panics_when_attr_fn_like_width_config_exceeds_max_width() {
855+
fn test_attr_fn_like_width_config_exceeds_max_width() {
859856
let toml = r#"
860857
max_width = 80
861858
attr_fn_like_width = 90
862859
"#;
863-
Config::from_toml(toml, Path::new("")).unwrap();
860+
let config = Config::from_toml(toml, Path::new("")).unwrap();
861+
assert_eq!(config.attr_fn_like_width(), 80);
864862
}
865863

866864
#[test]
867-
#[should_panic(expected = "`struct_lit_width` cannot have a value that exceeds `max_width")]
868-
fn test_panics_when_struct_lit_config_exceeds_max_width() {
865+
fn test_struct_lit_config_exceeds_max_width() {
869866
let toml = r#"
870-
max_width = 80
867+
max_width = 78
871868
struct_lit_width = 90
872869
"#;
873-
Config::from_toml(toml, Path::new("")).unwrap();
870+
let config = Config::from_toml(toml, Path::new("")).unwrap();
871+
assert_eq!(config.struct_lit_width(), 78);
874872
}
875873

876874
#[test]
877-
#[should_panic(
878-
expected = "`struct_variant_width` cannot have a value that exceeds `max_width"
879-
)]
880-
fn test_panics_when_struct_variant_width_config_exceeds_max_width() {
875+
fn test_struct_variant_width_config_exceeds_max_width() {
881876
let toml = r#"
882877
max_width = 80
883878
struct_variant_width = 90
884879
"#;
885-
Config::from_toml(toml, Path::new("")).unwrap();
880+
let config = Config::from_toml(toml, Path::new("")).unwrap();
881+
assert_eq!(config.struct_variant_width(), 80);
886882
}
887883

888884
#[test]
889-
#[should_panic(expected = "`array_width` cannot have a value that exceeds `max_width")]
890-
fn test_panics_when_array_width_config_exceeds_max_width() {
885+
fn test_array_width_config_exceeds_max_width() {
891886
let toml = r#"
892-
max_width = 80
893-
array_width = 90
887+
max_width = 60
888+
array_width = 80
894889
"#;
895-
Config::from_toml(toml, Path::new("")).unwrap();
890+
let config = Config::from_toml(toml, Path::new("")).unwrap();
891+
assert_eq!(config.array_width(), 60);
896892
}
897893

898894
#[test]
899-
#[should_panic(expected = "`chain_width` cannot have a value that exceeds `max_width")]
900-
fn test_panics_when_chain_width_config_exceeds_max_width() {
895+
fn test_chain_width_config_exceeds_max_width() {
901896
let toml = r#"
902897
max_width = 80
903898
chain_width = 90
904899
"#;
905-
Config::from_toml(toml, Path::new("")).unwrap();
900+
let config = Config::from_toml(toml, Path::new("")).unwrap();
901+
assert_eq!(config.chain_width(), 80);
906902
}
907903

908904
#[test]
909-
#[should_panic(
910-
expected = "`single_line_if_else_max_width` cannot have a value that exceeds `max_width"
911-
)]
912-
fn test_panics_when_single_line_if_else_max_width_config_exceeds_max_width() {
905+
fn test_single_line_if_else_max_width_config_exceeds_max_width() {
913906
let toml = r#"
914-
max_width = 80
907+
max_width = 70
915908
single_line_if_else_max_width = 90
916909
"#;
917-
Config::from_toml(toml, Path::new("")).unwrap();
910+
let config = Config::from_toml(toml, Path::new("")).unwrap();
911+
assert_eq!(config.single_line_if_else_max_width(), 70);
918912
}
919913

920914
#[test]
921-
#[should_panic(expected = "`fn_call_width` cannot have a value that exceeds `max_width")]
922-
fn test_panics_when_fn_call_width_override_exceeds_max_width() {
915+
fn test_override_fn_call_width_exceeds_max_width() {
923916
let mut config = Config::default();
924917
config.override_value("fn_call_width", "101");
918+
assert_eq!(config.fn_call_width(), 100);
925919
}
926920

927921
#[test]
928-
#[should_panic(
929-
expected = "`attr_fn_like_width` cannot have a value that exceeds `max_width"
930-
)]
931-
fn test_panics_when_attr_fn_like_width_override_exceeds_max_width() {
922+
fn test_override_attr_fn_like_width_exceeds_max_width() {
932923
let mut config = Config::default();
933924
config.override_value("attr_fn_like_width", "101");
925+
assert_eq!(config.attr_fn_like_width(), 100);
934926
}
935927

936928
#[test]
937-
#[should_panic(expected = "`struct_lit_width` cannot have a value that exceeds `max_width")]
938-
fn test_panics_when_struct_lit_override_exceeds_max_width() {
929+
fn test_override_struct_lit_exceeds_max_width() {
939930
let mut config = Config::default();
940931
config.override_value("struct_lit_width", "101");
932+
assert_eq!(config.struct_lit_width(), 100);
941933
}
942934

943935
#[test]
944-
#[should_panic(
945-
expected = "`struct_variant_width` cannot have a value that exceeds `max_width"
946-
)]
947-
fn test_panics_when_struct_variant_width_override_exceeds_max_width() {
936+
fn test_override_struct_variant_width_exceeds_max_width() {
948937
let mut config = Config::default();
949938
config.override_value("struct_variant_width", "101");
939+
assert_eq!(config.struct_variant_width(), 100);
950940
}
951941

952942
#[test]
953-
#[should_panic(expected = "`array_width` cannot have a value that exceeds `max_width")]
954-
fn test_panics_when_array_width_override_exceeds_max_width() {
943+
fn test_override_array_width_exceeds_max_width() {
955944
let mut config = Config::default();
956945
config.override_value("array_width", "101");
946+
assert_eq!(config.array_width(), 100);
957947
}
958948

959949
#[test]
960-
#[should_panic(expected = "`chain_width` cannot have a value that exceeds `max_width")]
961-
fn test_panics_when_chain_width_override_exceeds_max_width() {
950+
fn test_override_chain_width_exceeds_max_width() {
962951
let mut config = Config::default();
963952
config.override_value("chain_width", "101");
953+
assert_eq!(config.chain_width(), 100);
964954
}
965955

966956
#[test]
967-
#[should_panic(
968-
expected = "`single_line_if_else_max_width` cannot have a value that exceeds `max_width"
969-
)]
970-
fn test_panics_when_single_line_if_else_max_width_override_exceeds_max_width() {
957+
fn test_override_single_line_if_else_max_width_exceeds_max_width() {
971958
let mut config = Config::default();
972959
config.override_value("single_line_if_else_max_width", "101");
960+
assert_eq!(config.single_line_if_else_max_width(), 100);
973961
}
974962
}
975963
}

0 commit comments

Comments
 (0)