Skip to content

Commit b8d458d

Browse files
committed
Have default for in-source
1 parent 9ebc209 commit b8d458d

File tree

3 files changed

+57
-42
lines changed

3 files changed

+57
-42
lines changed

src/bsconfig.rs

Lines changed: 46 additions & 41 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
}
@@ -262,6 +263,10 @@ pub fn flatten_ppx_flags(
262263
pub fn read(path: String) -> Config {
263264
fs::read_to_string(path.clone())
264265
.map_err(|e| format!("Could not read bsconfig. {path} - {e}"))
266+
// .and_then(|x| {
267+
// dbg!(&x);
268+
// repair(x).map_err(|e| format!("Json was invalid and could not be repaired. {path} - {e}"))
269+
// })
265270
.and_then(|x| {
266271
serde_json::from_str::<Config>(&x).map_err(|e| format!("Could not parse bsconfig. {path} - {e}"))
267272
})
@@ -462,7 +467,7 @@ mod tests {
462467

463468
let config = serde_json::from_str::<Config>(json).unwrap();
464469
if let OneOrMore::Single(source) = config.sources {
465-
let source = to_qualified_without_children(&source, None);
470+
let source = source.to_qualified_without_children(None);
466471
assert_eq!(source.type_, Some(String::from("dev")));
467472
} else {
468473
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)