Skip to content

Commit

Permalink
reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Apr 28, 2024
1 parent 407dcdd commit bee77bb
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 92 deletions.
11 changes: 8 additions & 3 deletions slinky-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() {
.output
.expect("output path is required for partial linking");
writer
.save_linker_scripts(&output_path)
.export_linker_script_to_files(&output_path)
.expect("Error writing the linker scripts");
writer
.save_other_files()
Expand All @@ -52,10 +52,15 @@ fn main() {

if let Some(output_path) = cli.output {
writer
.save_linker_script(&output_path)
.export_linker_script_to_file(&output_path)
.expect("Error writing the linker script");
} else {
println!("{}", writer.export_as_string());
println!(
"{}",
writer
.export_linker_script_to_string()
.expect("Error exporting script to string")
);
}

writer
Expand Down
8 changes: 5 additions & 3 deletions slinky/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ pub enum SlinkyError {
#[error("Unable to open file '{path}', because '{description}'")]
FailedFileOpen { path: PathBuf, description: String },

#[error("Fail while writing to file '{path}', because '{description}'.\n Contents were: '{contents}'")]
FailedFileWrite {
path: PathBuf,
#[error("Failed to write, because '{description}'.\n Contents were: '{contents}'")]
FailedWrite {
description: String,
contents: String,
},

#[error("Failed to convert string, because '{description}'.")]
FailedStringConversion { description: String },

#[error("Unable to create dir '{path}', because '{description}")]
FailedDirCreate { path: PathBuf, description: String },

Expand Down
154 changes: 80 additions & 74 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ impl<'a> LinkerWriter<'a> {
need_ln = true;
}

if self.d.settings.discard_wildcard_section || !self.d.settings.sections_denylist.is_empty() {
if self.d.settings.discard_wildcard_section || !self.d.settings.sections_denylist.is_empty()
{
if need_ln {
self.writeln("");
}
Expand Down Expand Up @@ -205,14 +206,13 @@ impl<'a> LinkerWriter<'a> {

self.end_sections();
}
}

pub fn save_linker_script(&self, path: &Path) -> Result<(), SlinkyError> {
let mut f = utils::create_file_and_parents(path)?;

impl LinkerWriter<'_> {
pub fn export_linker_script(&self, dst: &mut impl Write) -> Result<(), SlinkyError> {
for line in &self.buffer {
if let Err(e) = writeln!(f, "{}", line) {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
if let Err(e) = writeln!(dst, "{}", line) {
return Err(SlinkyError::FailedWrite {
description: e.to_string(),
contents: line.into(),
});
Expand All @@ -222,54 +222,58 @@ impl<'a> LinkerWriter<'a> {
Ok(())
}

#[must_use]
pub fn export_as_string(&self) -> String {
let mut ret = String::new();
pub fn export_linker_script_to_file(&self, path: &Path) -> Result<(), SlinkyError> {
let mut f = utils::create_file_and_parents(path)?;

for line in &self.buffer {
ret += &format!("{}\n", line);
}
self.export_linker_script(&mut f)
}

pub fn export_linker_script_to_string(&self) -> Result<String, SlinkyError> {
let mut s = Vec::new();

ret
self.export_linker_script(&mut s)?;

match String::from_utf8(s) {
Err(e) => Err(SlinkyError::FailedStringConversion {
description: e.to_string(),
}),
Ok(ret) => Ok(ret),
}
}
}

pub fn save_dependencies_file(
impl LinkerWriter<'_> {
pub fn export_dependencies_file(
&self,
path: &Path,
dst: &mut impl Write,
target_path: &Path,
) -> Result<(), SlinkyError> {
let mut f = utils::create_file_and_parents(path)?;

if let Err(e) = write!(f, "{}:", target_path.display()) {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
if let Err(e) = write!(dst, "{}:", target_path.display()) {
return Err(SlinkyError::FailedWrite {
description: e.to_string(),
contents: target_path.display().to_string(),
});
}

for p in &self.files_paths {
if let Err(e) = write!(f, " \\\n {}", p.display()) {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
if let Err(e) = write!(dst, " \\\n {}", p.display()) {
return Err(SlinkyError::FailedWrite {
description: e.to_string(),
contents: p.display().to_string(),
});
}
}

if let Err(e) = write!(f, "\n\n") {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
if let Err(e) = write!(dst, "\n\n") {
return Err(SlinkyError::FailedWrite {
description: e.to_string(),
contents: "".to_string(),
});
}

for p in &self.files_paths {
if let Err(e) = writeln!(f, "{}:", p.display()) {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
if let Err(e) = writeln!(dst, "{}:", p.display()) {
return Err(SlinkyError::FailedWrite {
description: e.to_string(),
contents: p.display().to_string(),
});
Expand All @@ -279,31 +283,40 @@ impl<'a> LinkerWriter<'a> {
Ok(())
}

#[must_use]
pub fn export_dependencies_as_string(&self, target_path: &Path) -> String {
let mut ret = String::new();
pub fn export_dependencies_file_to_file(
&self,
path: &Path,
target_path: &Path,
) -> Result<(), SlinkyError> {
let mut f = utils::create_file_and_parents(path)?;

ret += &format!("{}:", target_path.display());
self.export_dependencies_file(&mut f, target_path)
}

for p in &self.files_paths {
ret += &format!(" \\\n {}", p.display());
}
pub fn export_dependencies_file_to_string(
&self,
target_path: &Path,
) -> Result<String, SlinkyError> {
let mut s = Vec::new();

ret += "\n\n";
self.export_dependencies_file(&mut s, target_path)?;

for p in &self.files_paths {
ret += &format!("{}:\n", p.display());
match String::from_utf8(s) {
Err(e) => Err(SlinkyError::FailedStringConversion {
description: e.to_string(),
}),
Ok(ret) => Ok(ret),
}

ret
}
}

pub fn save_symbol_header(&self, path: &Path) -> Result<(), SlinkyError> {
let mut f = utils::create_file_and_parents(path)?;

if let Err(e) = write!(f, "#ifndef HEADER_SYMBOLS_H\n#define HEADER_SYMBOLS_H\n\n") {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
impl LinkerWriter<'_> {
pub fn export_symbol_header(&self, dst: &mut impl Write) -> Result<(), SlinkyError> {
if let Err(e) = write!(
dst,
"#ifndef HEADER_SYMBOLS_H\n#define HEADER_SYMBOLS_H\n\n"
) {
return Err(SlinkyError::FailedWrite {
description: e.to_string(),
contents: "".into(),
});
Expand All @@ -317,21 +330,19 @@ impl<'a> LinkerWriter<'a> {

for sym in &self.linker_symbols {
if let Err(e) = writeln!(
f,
dst,
"extern {} {}{};",
self.d.settings.symbols_header_type, sym, arr_suffix
) {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
return Err(SlinkyError::FailedWrite {
description: e.to_string(),
contents: sym.into(),
});
}
}

if let Err(e) = write!(f, "\n#endif\n") {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
if let Err(e) = write!(dst, "\n#endif\n") {
return Err(SlinkyError::FailedWrite {
description: e.to_string(),
contents: "".into(),
});
Expand All @@ -340,41 +351,36 @@ impl<'a> LinkerWriter<'a> {
Ok(())
}

#[must_use]
pub fn export_symbol_header_as_string(&self) -> String {
let mut ret = String::new();

ret += "#ifndef HEADER_SYMBOLS_H\n#define HEADER_SYMBOLS_H\n\n";
pub fn export_symbol_header_to_file(&self, path: &Path) -> Result<(), SlinkyError> {
let mut f = utils::create_file_and_parents(path)?;

let arr_suffix = if self.d.settings.symbols_header_as_array {
"[]"
} else {
""
};
self.export_symbol_header(&mut f)
}

for sym in &self.linker_symbols {
ret += &format!(
"extern {} {}{};\n",
self.d.settings.symbols_header_type, sym, arr_suffix
);
}
pub fn export_symbol_header_to_string(&self) -> Result<String, SlinkyError> {
let mut s = Vec::new();

ret += "\n#endif\n";
self.export_symbol_header(&mut s)?;

ret
match String::from_utf8(s) {
Err(e) => Err(SlinkyError::FailedStringConversion {
description: e.to_string(),
}),
Ok(ret) => Ok(ret),
}
}
}

impl LinkerWriter<'_> {
pub fn save_other_files(&self) -> Result<(), SlinkyError> {
if let Some(d_path) = &self.d.settings.d_path {
if let Some(target_path) = &self.d.settings.target_path {
self.save_dependencies_file(d_path, target_path)?;
self.export_dependencies_file_to_file(d_path, target_path)?;
}
}

if let Some(symbols_header_path) = &self.d.settings.symbols_header_path {
self.save_symbol_header(symbols_header_path)?;
self.export_symbol_header_to_file(symbols_header_path)?;
}

Ok(())
Expand Down
8 changes: 4 additions & 4 deletions slinky/src/partial_linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ impl<'a> PartialLinkerWriter<'a> {
self.main_writer.end_sections();
}

pub fn save_linker_scripts(&self, path: &Path) -> Result<(), SlinkyError> {
self.main_writer.save_linker_script(path)?;
pub fn export_linker_script_to_files(&self, path: &Path) -> Result<(), SlinkyError> {
self.main_writer.export_linker_script_to_file(path)?;

for (partial, name) in &self.partial_writers {
let mut p = PathBuf::new();

p.push(&self.d.settings.partial_scripts_folder);
p.push(&format!("{}.ld", name));

partial.save_linker_script(&p)?;
partial.export_linker_script_to_file(&p)?;
}

Ok(())
Expand All @@ -95,7 +95,7 @@ impl<'a> PartialLinkerWriter<'a> {
d_path.push(&self.d.settings.partial_scripts_folder);
d_path.push(&format!("{}.d", name));

partial.save_dependencies_file(&d_path, &target_path)?;
partial.export_dependencies_file_to_file(&d_path, &target_path)?;
}
}

Expand Down
Loading

0 comments on commit bee77bb

Please sign in to comment.