Skip to content

Commit

Permalink
Have default for in-source
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandpeelen committed Nov 18, 2024
1 parent 9ebc209 commit b8d458d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 42 deletions.
87 changes: 46 additions & 41 deletions src/bsconfig.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::build::packages;
use crate::helpers::deserialize::*;
use convert_case::{Case, Casing};
use serde::Deserialize;
use std::fs;
Expand Down Expand Up @@ -27,45 +28,6 @@ pub struct PackageSource {
pub type_: Option<String>,
}

/// `to_qualified_without_children` takes a tree like structure of dependencies, coming in from
/// `bsconfig`, and turns it into a flat list. The main thing we extract here are the source
/// folders, and optional subdirs, where potentially, the subdirs recurse or not.
pub fn to_qualified_without_children(s: &Source, sub_path: Option<PathBuf>) -> PackageSource {
match s {
Source::Shorthand(dir) => PackageSource {
dir: sub_path
.map(|p| p.join(Path::new(dir)))
.unwrap_or(Path::new(dir).to_path_buf())
.to_string_lossy()
.to_string(),
subdirs: None,
type_: s.get_type(),
},
Source::Qualified(PackageSource {
dir,
type_,
subdirs: Some(Subdirs::Recurse(should_recurse)),
}) => PackageSource {
dir: sub_path
.map(|p| p.join(Path::new(dir)))
.unwrap_or(Path::new(dir).to_path_buf())
.to_string_lossy()
.to_string(),
subdirs: Some(Subdirs::Recurse(*should_recurse)),
type_: type_.to_owned(),
},
Source::Qualified(PackageSource { dir, type_, .. }) => PackageSource {
dir: sub_path
.map(|p| p.join(Path::new(dir)))
.unwrap_or(Path::new(dir).to_path_buf())
.to_string_lossy()
.to_string(),
subdirs: None,
type_: type_.to_owned(),
},
}
}

impl Eq for PackageSource {}

#[derive(Deserialize, Debug, Clone, PartialEq, Hash)]
Expand Down Expand Up @@ -97,14 +59,53 @@ impl Source {
(source, _) => source.clone(),
}
}

/// `to_qualified_without_children` takes a tree like structure of dependencies, coming in from
/// `bsconfig`, and turns it into a flat list. The main thing we extract here are the source
/// folders, and optional subdirs, where potentially, the subdirs recurse or not.
pub fn to_qualified_without_children(&self, sub_path: Option<PathBuf>) -> PackageSource {
match self {
Source::Shorthand(dir) => PackageSource {
dir: sub_path
.map(|p| p.join(Path::new(dir)))
.unwrap_or(Path::new(dir).to_path_buf())
.to_string_lossy()
.to_string(),
subdirs: None,
type_: self.get_type(),
},
Source::Qualified(PackageSource {
dir,
type_,
subdirs: Some(Subdirs::Recurse(should_recurse)),
}) => PackageSource {
dir: sub_path
.map(|p| p.join(Path::new(dir)))
.unwrap_or(Path::new(dir).to_path_buf())
.to_string_lossy()
.to_string(),
subdirs: Some(Subdirs::Recurse(*should_recurse)),
type_: type_.to_owned(),
},
Source::Qualified(PackageSource { dir, type_, .. }) => PackageSource {
dir: sub_path
.map(|p| p.join(Path::new(dir)))
.unwrap_or(Path::new(dir).to_path_buf())
.to_string_lossy()
.to_string(),
subdirs: None,
type_: type_.to_owned(),
},
}
}
}

impl Eq for Source {}

#[derive(Deserialize, Debug, Clone)]
pub struct PackageSpec {
pub module: String,
#[serde(rename = "in-source")]
#[serde(rename = "in-source", default = "default_true")]
pub in_source: bool,
pub suffix: Option<String>,
}
Expand Down Expand Up @@ -262,6 +263,10 @@ pub fn flatten_ppx_flags(
pub fn read(path: String) -> Config {
fs::read_to_string(path.clone())
.map_err(|e| format!("Could not read bsconfig. {path} - {e}"))
// .and_then(|x| {
// dbg!(&x);
// repair(x).map_err(|e| format!("Json was invalid and could not be repaired. {path} - {e}"))
// })
.and_then(|x| {
serde_json::from_str::<Config>(&x).map_err(|e| format!("Could not parse bsconfig. {path} - {e}"))
})
Expand Down Expand Up @@ -462,7 +467,7 @@ mod tests {

let config = serde_json::from_str::<Config>(json).unwrap();
if let OneOrMore::Single(source) = config.sources {
let source = to_qualified_without_children(&source, None);
let source = source.to_qualified_without_children(None);
assert_eq!(source.type_, Some(String::from("dev")));
} else {
dbg!(config.sources);
Expand Down
2 changes: 1 addition & 1 deletion src/build/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ pub fn read_folders(
fn get_source_dirs(source: bsconfig::Source, sub_path: Option<PathBuf>) -> AHashSet<bsconfig::PackageSource> {
let mut source_folders: AHashSet<bsconfig::PackageSource> = AHashSet::new();

let source_folder = bsconfig::to_qualified_without_children(&source, sub_path.to_owned());
let source_folder = source.to_qualified_without_children(sub_path.to_owned());
source_folders.insert(source_folder.to_owned());

let (subdirs, full_recursive) = match source.to_owned() {
Expand Down
10 changes: 10 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ use std::time::{SystemTime, UNIX_EPOCH};

pub type StdErr = String;

pub mod deserialize {
pub fn default_false() -> bool {
false
}

pub fn default_true() -> bool {
true
}
}

pub mod emojis {
use console::Emoji;
pub static COMMAND: Emoji<'_, '_> = Emoji("🏃 ", "");
Expand Down

0 comments on commit b8d458d

Please sign in to comment.