Skip to content

Commit

Permalink
Change the unserializing logic a tiny bit
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Feb 25, 2024
1 parent 507a0af commit c121737
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 94 deletions.
14 changes: 6 additions & 8 deletions slinky/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
Settings, SlinkyError,
};

#[derive(PartialEq, Debug, Default)]
#[derive(PartialEq, Debug)]
pub struct Document {
pub settings: Settings,

Expand Down Expand Up @@ -52,10 +52,8 @@ pub(crate) struct DocumentSerial {

impl DocumentSerial {
pub fn unserialize(self) -> Result<Document, SlinkyError> {
let mut ret = Document::default();

ret.settings = match self.settings.get_non_null_no_default("settings")? {
None => ret.settings,
let settings = match self.settings.get_non_null_no_default("settings")? {
None => Settings::default(),
Some(v) => v.unserialize()?,
};

Expand All @@ -65,11 +63,11 @@ impl DocumentSerial {
});
}

ret.segments.reserve(self.segments.len());
let mut segments = Vec::with_capacity(self.segments.len());
for seg in self.segments {
ret.segments.push(seg.unserialize(&ret.settings)?);
segments.push(seg.unserialize(&settings)?);
}

Ok(ret)
Ok(Document { settings, segments })
}
}
33 changes: 13 additions & 20 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,13 @@ impl<'a> LinkerWriter<'a> {
}

pub fn save_linker_script(&self, path: &Path) -> Result<(), SlinkyError> {
match path.parent() {
None => (),
Some(parent) => match fs::create_dir_all(parent) {
Ok(_) => (),
Err(e) => {
return Err(SlinkyError::FailedDirCreate {
path: parent.to_path_buf(),
description: e.to_string(),
})
}
},
if let Some(parent) = path.parent() {
if let Err(e) = fs::create_dir_all(parent) {
return Err(SlinkyError::FailedDirCreate {
path: parent.to_path_buf(),
description: e.to_string(),
});
}
}

let mut f = match File::create(path) {
Expand All @@ -114,15 +110,12 @@ impl<'a> LinkerWriter<'a> {
};

for line in &self.buffer {
match writeln!(f, "{}", line) {
Ok(_) => (),
Err(e) => {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
description: e.to_string(),
contents: line.into(),
})
}
if let Err(e) = writeln!(f, "{}", line) {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
description: e.to_string(),
contents: line.into(),
});
}
}

Expand Down
48 changes: 18 additions & 30 deletions slinky/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,6 @@ pub struct Segment {
pub wildcard_sections: bool,
}

impl Default for Segment {
fn default() -> Self {
Self {
name: "".to_string(),
files: Vec::new(),

fixed_vram: None,

alloc_sections: Vec::new(),
noload_sections: Vec::new(),

subalign: None,

wildcard_sections: false,
}
}
}

#[derive(Deserialize, PartialEq, Debug)]
#[serde(deny_unknown_fields)]
pub(crate) struct SegmentSerial {
Expand All @@ -66,43 +48,49 @@ pub(crate) struct SegmentSerial {

impl SegmentSerial {
pub fn unserialize(self, settings: &Settings) -> Result<Segment, SlinkyError> {
let mut ret = Segment::default();

if self.name.is_empty() {
return Err(SlinkyError::EmptyValue {
name: "name".to_string(),
});
}
ret.name = self.name;
let name = self.name;

if self.files.is_empty() {
return Err(SlinkyError::EmptyValue {
name: "files".to_string(),
});
}

ret.files.reserve(self.files.len());
let mut files = Vec::with_capacity(self.files.len());
for file in self.files {
ret.files.push(file.unserialize(settings)?);
files.push(file.unserialize(settings)?);
}

ret.alloc_sections = self
let fixed_vram = self.fixed_vram;

let alloc_sections = self
.alloc_sections
.get_non_null("alloc_sections", || settings.alloc_sections.clone())?;
ret.noload_sections = self
let noload_sections = self
.noload_sections
.get_non_null("noload_sections", || settings.noload_sections.clone())?;

ret.fixed_vram = self.fixed_vram;

ret.subalign = self
let subalign = self
.subalign
.get_optional_nullable("subalign", || settings.subalign)?;

ret.wildcard_sections = self
let wildcard_sections = self
.wildcard_sections
.get_non_null("wildcard_sections", || settings.wildcard_sections)?;

Ok(ret)
Ok(Segment {
name,
files,
alloc_sections,
noload_sections,
fixed_vram,
subalign,
wildcard_sections,
})
}
}
105 changes: 69 additions & 36 deletions slinky/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,53 @@ pub struct Settings {
}

// TODO: consider changing the defaults before 1.0.0

fn settings_base_path_default() -> PathBuf {
PathBuf::new()
}

fn settings_linker_symbols_style_default() -> LinkerSymbolsStyle {
LinkerSymbolsStyle::Splat
}

fn settings_alloc_sections_default() -> Vec<String> {
vec![
".text".into(),
".data".into(),
".rodata".into(),
".sdata".into(),
]
}

fn settings_noload_sections_default() -> Vec<String> {
vec![
".sbss".into(),
".scommon".into(),
".bss".into(),
"COMMON".into(),
]
}

fn settings_subalign_default() -> Option<u32> {
Some(16)
}

fn settings_wildcard_sections_default() -> bool {
true
}

impl Default for Settings {
fn default() -> Self {
Self {
base_path: PathBuf::new(),
linker_symbols_style: LinkerSymbolsStyle::Splat,

alloc_sections: vec![
".text".into(),
".data".into(),
".rodata".into(),
".sdata".into(),
],
noload_sections: vec![
".sbss".into(),
".scommon".into(),
".bss".into(),
"COMMON".into(),
],

subalign: Some(16),

wildcard_sections: true,
base_path: settings_base_path_default(),
linker_symbols_style: settings_linker_symbols_style_default(),

alloc_sections: settings_alloc_sections_default(),
noload_sections: settings_noload_sections_default(),

subalign: settings_subalign_default(),

wildcard_sections: settings_wildcard_sections_default(),
// fill_value: Some(Some(0)),
}
}
Expand Down Expand Up @@ -76,28 +101,36 @@ pub(crate) struct SettingsSerial {

impl SettingsSerial {
pub fn unserialize(self) -> Result<Settings, SlinkyError> {
let mut ret = Settings::default();

ret.base_path = self.base_path.get_non_null("base_path", || ret.base_path)?;
ret.linker_symbols_style = self
.linker_symbols_style
.get_non_null("linker_symbols_style", || ret.linker_symbols_style)?;

ret.alloc_sections = self
let base_path = self
.base_path
.get_non_null("base_path", settings_base_path_default)?;
let linker_symbols_style = self.linker_symbols_style.get_non_null(
"linker_symbols_style",
settings_linker_symbols_style_default,
)?;

let alloc_sections = self
.alloc_sections
.get_non_null("alloc_sections", || ret.alloc_sections)?;
ret.noload_sections = self
.get_non_null("alloc_sections", settings_alloc_sections_default)?;
let noload_sections = self
.noload_sections
.get_non_null("noload_sections", || ret.noload_sections)?;
.get_non_null("noload_sections", settings_noload_sections_default)?;

ret.subalign = self
let subalign = self
.subalign
.get_optional_nullable("subalign", || ret.subalign)?;
.get_optional_nullable("subalign", settings_subalign_default)?;

ret.wildcard_sections = self
let wildcard_sections = self
.wildcard_sections
.get_non_null("wildcard_sections", || ret.wildcard_sections)?;

Ok(ret)
.get_non_null("wildcard_sections", settings_wildcard_sections_default)?;

Ok(Settings {
base_path,
linker_symbols_style,
alloc_sections,
noload_sections,
subalign,
wildcard_sections,
})
}
}

0 comments on commit c121737

Please sign in to comment.