Skip to content

Commit

Permalink
Merge branch 'refactor-cfg-and-model-cow-based' into refactor-and-cle…
Browse files Browse the repository at this point in the history
…an-code
  • Loading branch information
TheRustifyer committed Jun 23, 2024
2 parents 32fcb75 + 79e9e37 commit 87305b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
8 changes: 4 additions & 4 deletions zork++/src/lib/project_model/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ impl<'a> From<ModulePartition<'a>> for ModulePartitionModel<'a> {
}
}

/* impl<'a> From<&ModulePartition<'a>> for ModulePartitionModel<'a> {
fn from(value: &ModulePartition<'a>) -> Self {
impl<'a> From<&ModulePartition<'a>> for ModulePartitionModel<'a> {
fn from(value: &ModulePartition<'a>) -> &'a Self {
Self {
module: value.module,
module: value.module.,
partition_name: value.partition_name.unwrap_or_default(),
is_internal_partition: value.is_internal_partition.unwrap_or_default(),
}
}
} */
}

#[derive(Debug, PartialEq, Eq)]
pub struct ModuleImplementationModel<'a> {
Expand Down
29 changes: 15 additions & 14 deletions zork++/src/lib/utils/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ pub fn build_model<'a>(
cli_args: &'a CliArgs,
absolute_project_root: &Path,
) -> Result<ZorkModel<'a>> {
let proj_name = config.project.name.clone();
let project = assemble_project_model(config.project);

let compiler = assemble_compiler_model(config.compiler, cli_args);
let build = assemble_build_model(config.build, absolute_project_root);
let executable = assemble_executable_model(&project.name, config.executable, absolute_project_root);
let executable = assemble_executable_model(proj_name, config.executable, absolute_project_root);
let modules = assemble_modules_model(config.modules, absolute_project_root);
let tests = assemble_tests_model(project.name.as_ref(), config.tests, absolute_project_root);

Expand All @@ -120,7 +121,7 @@ fn assemble_project_model<'a>(config: ProjectAttribute<'a>) -> ProjectModel<'a>
.authors
// .as_ref()
// .map_or_else(|| &[] as &[Cow<'a, str>], |auths| auths.as_slice()),
.map_or_else(|| &[] as &[Cow<'a, str>], |auths| auths.as_slice()),
.map_or_else(|| &[] as [Cow<'a, str>], |auths| &*auths),
compilation_db: config.compilation_db.unwrap_or_default(),
project_root: config.project_root,
}
Expand Down Expand Up @@ -162,20 +163,19 @@ fn assemble_build_model(config: Option<BuildAttribute>, project_root: &Path) ->

//noinspection ALL
fn assemble_executable_model<'a>(
project_name: &'a Cow<'a, str>,
project_name: &Cow<'a, str>,
config: Option<ExecutableAttribute<'a>>,
project_root: &Path,
) -> ExecutableModel<'a> {
let config = config.as_ref();

let executable_name = config
.as_ref()
.and_then(|exe| -> Option<Cow<'_, str>> {exe.executable_name.clone()})
.unwrap_or(Cow::Borrowed(project_name));
.unwrap_or_else(|| project_name.clone());

let sources = config
.and_then(|exe| exe.sources.clone())
.unwrap_or_default();
.unwrap_or_else(|| Vec::with_capacity(0));

let sourceset = get_sourceset_for(sources, project_root);

Expand All @@ -196,6 +196,7 @@ fn assemble_modules_model<'a>(
project_root: &Path,
) -> ModulesModel<'a> {
let base_ifcs_dir = config
.as_ref()
.and_then(|modules| modules.base_ifcs_dir)
.unwrap_or(Cow::Borrowed("."));

Expand Down Expand Up @@ -248,20 +249,20 @@ fn assemble_module_interface_model<'a>(
base_path: &str,
project_root: &Path,
) -> ModuleInterfaceModel<'a> {
let file_path = Path::new(project_root).join(base_path).join(config.file);
let module_name = config.module_name.unwrap_or_else(|| {
std::borrow::Cow::Borrowed(Path::new(&config.file)
let file_path = Path::new(project_root).join(base_path).join(config.file.as_ref());
let module_name = config.module_name.clone().unwrap_or_else(|| {
Cow::Borrowed(Path::new(config.file.as_ref())
.file_stem()
.unwrap_or_else(|| panic!("Found ill-formed path on: {}", config.file))
.to_str()
.unwrap())
});
}).to_owned();

let dependencies = config.dependencies.clone().unwrap_or_default();
let dependencies = config.dependencies.clone().unwrap_or_else(|| Vec::with_capacity(0)); // TODO
let partition = if config.partition.is_none() {
None
} else {
Some(ModulePartitionModel::from( config.partition.unwrap(),))
Some(ModulePartitionModel::from(config.partition.as_ref().unwrap(),))
};

let file_details = utils::fs::get_file_details(&file_path).unwrap_or_else(|_| {
Expand Down Expand Up @@ -289,9 +290,9 @@ fn assemble_module_implementation_model<'a>(
let last_dot_index = config.file.as_ref().rfind('.');
if let Some(idx) = last_dot_index {
let implicit_dependency = config.file.split_at(idx);
dependencies.push(Cow::Borrowed(implicit_dependency.0))
dependencies.push(Cow::Owned(implicit_dependency.0.to_owned()))
} else {
dependencies.push(Cow::Borrowed(&config.file));
dependencies.push(config.file);
}
}

Expand Down

0 comments on commit 87305b7

Please sign in to comment.