Skip to content

Commit 29d1e4e

Browse files
committed
🎨 - More lenient parsing of config
1 parent 9ebc209 commit 29d1e4e

File tree

3 files changed

+71
-48
lines changed

3 files changed

+71
-48
lines changed

src/bsconfig.rs

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::build::packages;
2+
use crate::helpers::deserialize::*;
23
use convert_case::{Case, Casing};
34
use serde::Deserialize;
45
use std::fs;
@@ -27,45 +28,6 @@ pub struct PackageSource {
2728
pub type_: Option<String>,
2829
}
2930

30-
/// `to_qualified_without_children` takes a tree like structure of dependencies, coming in from
31-
/// `bsconfig`, and turns it into a flat list. The main thing we extract here are the source
32-
/// folders, and optional subdirs, where potentially, the subdirs recurse or not.
33-
pub fn to_qualified_without_children(s: &Source, sub_path: Option<PathBuf>) -> PackageSource {
34-
match s {
35-
Source::Shorthand(dir) => PackageSource {
36-
dir: sub_path
37-
.map(|p| p.join(Path::new(dir)))
38-
.unwrap_or(Path::new(dir).to_path_buf())
39-
.to_string_lossy()
40-
.to_string(),
41-
subdirs: None,
42-
type_: s.get_type(),
43-
},
44-
Source::Qualified(PackageSource {
45-
dir,
46-
type_,
47-
subdirs: Some(Subdirs::Recurse(should_recurse)),
48-
}) => PackageSource {
49-
dir: sub_path
50-
.map(|p| p.join(Path::new(dir)))
51-
.unwrap_or(Path::new(dir).to_path_buf())
52-
.to_string_lossy()
53-
.to_string(),
54-
subdirs: Some(Subdirs::Recurse(*should_recurse)),
55-
type_: type_.to_owned(),
56-
},
57-
Source::Qualified(PackageSource { dir, type_, .. }) => PackageSource {
58-
dir: sub_path
59-
.map(|p| p.join(Path::new(dir)))
60-
.unwrap_or(Path::new(dir).to_path_buf())
61-
.to_string_lossy()
62-
.to_string(),
63-
subdirs: None,
64-
type_: type_.to_owned(),
65-
},
66-
}
67-
}
68-
6931
impl Eq for PackageSource {}
7032

7133
#[derive(Deserialize, Debug, Clone, PartialEq, Hash)]
@@ -97,14 +59,53 @@ impl Source {
9759
(source, _) => source.clone(),
9860
}
9961
}
62+
63+
/// `to_qualified_without_children` takes a tree like structure of dependencies, coming in from
64+
/// `bsconfig`, and turns it into a flat list. The main thing we extract here are the source
65+
/// folders, and optional subdirs, where potentially, the subdirs recurse or not.
66+
pub fn to_qualified_without_children(&self, sub_path: Option<PathBuf>) -> PackageSource {
67+
match self {
68+
Source::Shorthand(dir) => PackageSource {
69+
dir: sub_path
70+
.map(|p| p.join(Path::new(dir)))
71+
.unwrap_or(Path::new(dir).to_path_buf())
72+
.to_string_lossy()
73+
.to_string(),
74+
subdirs: None,
75+
type_: self.get_type(),
76+
},
77+
Source::Qualified(PackageSource {
78+
dir,
79+
type_,
80+
subdirs: Some(Subdirs::Recurse(should_recurse)),
81+
}) => PackageSource {
82+
dir: sub_path
83+
.map(|p| p.join(Path::new(dir)))
84+
.unwrap_or(Path::new(dir).to_path_buf())
85+
.to_string_lossy()
86+
.to_string(),
87+
subdirs: Some(Subdirs::Recurse(*should_recurse)),
88+
type_: type_.to_owned(),
89+
},
90+
Source::Qualified(PackageSource { dir, type_, .. }) => PackageSource {
91+
dir: sub_path
92+
.map(|p| p.join(Path::new(dir)))
93+
.unwrap_or(Path::new(dir).to_path_buf())
94+
.to_string_lossy()
95+
.to_string(),
96+
subdirs: None,
97+
type_: type_.to_owned(),
98+
},
99+
}
100+
}
100101
}
101102

102103
impl Eq for Source {}
103104

104105
#[derive(Deserialize, Debug, Clone)]
105106
pub struct PackageSpec {
106107
pub module: String,
107-
#[serde(rename = "in-source")]
108+
#[serde(rename = "in-source", default = "default_true")]
108109
pub in_source: bool,
109110
pub suffix: Option<String>,
110111
}
@@ -122,10 +123,14 @@ pub struct Warnings {
122123
pub error: Option<Error>,
123124
}
124125

125-
#[derive(Deserialize, Debug, Clone)]
126-
pub struct Reason {
127-
#[serde(rename = "react-jsx")]
128-
pub react_jsx: i32,
126+
#[derive(Deserialize, Debug, Clone, PartialEq, Hash)]
127+
#[serde(untagged)]
128+
pub enum Reason {
129+
Versioned {
130+
#[serde(rename = "react-jsx")]
131+
react_jsx: i32,
132+
},
133+
Unversioned(bool),
129134
}
130135

131136
#[derive(Deserialize, Debug, Clone)]
@@ -262,6 +267,10 @@ pub fn flatten_ppx_flags(
262267
pub fn read(path: String) -> Config {
263268
fs::read_to_string(path.clone())
264269
.map_err(|e| format!("Could not read bsconfig. {path} - {e}"))
270+
// .and_then(|x| {
271+
// dbg!(&x);
272+
// repair(x).map_err(|e| format!("Json was invalid and could not be repaired. {path} - {e}"))
273+
// })
265274
.and_then(|x| {
266275
serde_json::from_str::<Config>(&x).map_err(|e| format!("Could not parse bsconfig. {path} - {e}"))
267276
})
@@ -332,8 +341,12 @@ impl Config {
332341
Some(_version) => panic!("Unsupported JSX version"),
333342
None => vec![],
334343
},
335-
(Some(reason), None) => {
336-
vec!["-bs-jsx".to_string(), format!("{}", reason.react_jsx)]
344+
(Some(Reason::Versioned { react_jsx }), None) => {
345+
vec!["-bs-jsx".to_string(), format!("{}", react_jsx)]
346+
}
347+
(Some(Reason::Unversioned(true)), None) => {
348+
// If Reason is 'true' - we should default to the latest
349+
vec!["-bs-jsx".to_string()]
337350
}
338351
_ => vec![],
339352
}
@@ -462,7 +475,7 @@ mod tests {
462475

463476
let config = serde_json::from_str::<Config>(json).unwrap();
464477
if let OneOrMore::Single(source) = config.sources {
465-
let source = to_qualified_without_children(&source, None);
478+
let source = source.to_qualified_without_children(None);
466479
assert_eq!(source.type_, Some(String::from("dev")));
467480
} else {
468481
dbg!(config.sources);

src/build/packages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ pub fn read_folders(
184184
fn get_source_dirs(source: bsconfig::Source, sub_path: Option<PathBuf>) -> AHashSet<bsconfig::PackageSource> {
185185
let mut source_folders: AHashSet<bsconfig::PackageSource> = AHashSet::new();
186186

187-
let source_folder = bsconfig::to_qualified_without_children(&source, sub_path.to_owned());
187+
let source_folder = source.to_qualified_without_children(sub_path.to_owned());
188188
source_folders.insert(source_folder.to_owned());
189189

190190
let (subdirs, full_recursive) = match source.to_owned() {

src/helpers.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ use std::time::{SystemTime, UNIX_EPOCH};
1010

1111
pub type StdErr = String;
1212

13+
pub mod deserialize {
14+
pub fn default_false() -> bool {
15+
false
16+
}
17+
18+
pub fn default_true() -> bool {
19+
true
20+
}
21+
}
22+
1323
pub mod emojis {
1424
use console::Emoji;
1525
pub static COMMAND: Emoji<'_, '_> = Emoji("🏃 ", "");

0 commit comments

Comments
 (0)