Skip to content

Commit

Permalink
simplify cod by using more traits
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Aug 12, 2024
1 parent fabde98 commit 0b248df
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 59 deletions.
38 changes: 11 additions & 27 deletions slinky/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use serde::Deserialize;

use crate::{
absent_nullable::AbsentNullable, segment::SegmentSerial, settings::SettingsSerial,
symbol_assignment::SymbolAssignmentSerial, vram_class::VramClassSerial, Segment, Settings,
SlinkyError, SymbolAssignment, VramClass,
symbol_assignment::SymbolAssignmentSerial, traits::Serial, vram_class::VramClassSerial,
Segment, Settings, SlinkyError, SymbolAssignment, VramClass,
};

#[derive(PartialEq, Debug)]
Expand Down Expand Up @@ -74,39 +74,23 @@ impl DocumentSerial {
});
}

let mut vram_classes = Vec::new();
match self.vram_classes.get_non_null_no_default("vram_classes")? {
None => (),
Some(v) => {
for c in v {
vram_classes.push(c.unserialize(&settings)?);
}
}
}
let vram_classes = self
.vram_classes
.get_non_null("vram_classes", Vec::new)?
.unserialize(&settings)?;

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

let mut undefined_symbols = Vec::new();
match self
let symbol_assignments = self
.symbol_assignments
.get_non_null_no_default("undefined_symbols")?
{
None => (),
Some(v) => {
for c in v {
undefined_symbols.push(c.unserialize(&settings)?);
}
}
}
.get_non_null("symbol_assignments", Vec::new)?
.unserialize(&settings)?;

Ok(Document {
settings,
vram_classes,
segments,
symbol_assignments: undefined_symbols,
symbol_assignments,
})
}
}
23 changes: 8 additions & 15 deletions slinky/src/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::{
};

use crate::{
absent_nullable::AbsentNullable, file_kind::FileKind, EscapedPath, RuntimeSettings, Settings,
SlinkyError,
absent_nullable::AbsentNullable, file_kind::FileKind, traits::Serial, EscapedPath,
RuntimeSettings, Settings, SlinkyError,
};

#[derive(PartialEq, Debug, Clone)]
Expand Down Expand Up @@ -106,8 +106,10 @@ pub(crate) struct FileInfoSerial {
pub exclude_if_all: AbsentNullable<Vec<(String, String)>>,
}

impl FileInfoSerial {
pub(crate) fn unserialize(self, _settings: &Settings) -> Result<FileInfo, SlinkyError> {
impl Serial for FileInfoSerial {
type Output = FileInfo;

fn unserialize(self, settings: &Settings) -> Result<Self::Output, SlinkyError> {
// Since a `kind` can be deduced from a `path` (which requires a `path`) then we need to do both simultaneously
let (path, kind) = match self.kind.get_non_null_no_default("kind")? {
Some(k) => match k {
Expand Down Expand Up @@ -225,16 +227,7 @@ impl FileInfoSerial {
}
Vec::default()
}
FileKind::Group => {
let temp_vec = self.files.get("files")?;
let mut result_vec = Vec::with_capacity(temp_vec.len());

for temp in temp_vec {
result_vec.push(temp.unserialize(_settings)?);
}

result_vec
}
FileKind::Group => self.files.get("files")?.unserialize(settings)?,
};

let dir = match kind {
Expand Down Expand Up @@ -263,7 +256,7 @@ impl FileInfoSerial {
.exclude_if_all
.get_non_null_not_empty("exclude_if_all", Vec::new)?;

Ok(FileInfo {
Ok(Self::Output {
path,
kind,
subfile,
Expand Down
14 changes: 7 additions & 7 deletions slinky/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde::Deserialize;
use crate::{
absent_nullable::AbsentNullable,
file_info::{FileInfo, FileInfoSerial},
traits::Serial,
EscapedPath, RuntimeSettings, Settings, SlinkyError,
};

Expand Down Expand Up @@ -151,8 +152,10 @@ pub(crate) struct SegmentSerial {
pub fill_value: AbsentNullable<u32>,
}

impl SegmentSerial {
pub fn unserialize(self, settings: &Settings) -> Result<Segment, SlinkyError> {
impl Serial for SegmentSerial {
type Output = Segment;

fn unserialize(self, settings: &Settings) -> Result<Self::Output, SlinkyError> {
if self.name.is_empty() {
return Err(SlinkyError::EmptyValue {
name: "name".to_string(),
Expand All @@ -166,10 +169,7 @@ impl SegmentSerial {
});
}

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

let fixed_vram = self.fixed_vram.get_non_null_no_default("fixed_vram")?;

Expand Down Expand Up @@ -291,7 +291,7 @@ impl SegmentSerial {
.fill_value
.get_optional_nullable("fill_value", || settings.fill_value)?;

Ok(Segment {
Ok(Self::Output {
name,
files,
fixed_vram,
Expand Down
10 changes: 6 additions & 4 deletions slinky/src/symbol_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use serde::Deserialize;

use crate::{absent_nullable::AbsentNullable, Settings, SlinkyError};
use crate::{absent_nullable::AbsentNullable, traits::Serial, Settings, SlinkyError};

#[derive(PartialEq, Debug, Clone)]
pub struct SymbolAssignment {
Expand Down Expand Up @@ -47,8 +47,10 @@ pub(crate) struct SymbolAssignmentSerial {
pub exclude_if_all: AbsentNullable<Vec<(String, String)>>,
}

impl SymbolAssignmentSerial {
pub fn unserialize(self, _settings: &Settings) -> Result<SymbolAssignment, SlinkyError> {
impl Serial for SymbolAssignmentSerial {
type Output = SymbolAssignment;

fn unserialize(self, _settings: &Settings) -> Result<Self::Output, SlinkyError> {
if self.name.is_empty() {
return Err(SlinkyError::EmptyValue {
name: "name".to_string(),
Expand Down Expand Up @@ -79,7 +81,7 @@ impl SymbolAssignmentSerial {
.exclude_if_all
.get_non_null_not_empty("exclude_if_all", Vec::new)?;

Ok(SymbolAssignment {
Ok(Self::Output {
name,
value,
provide,
Expand Down
32 changes: 30 additions & 2 deletions slinky/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */

use crate::{Document, EscapedPath, Segment, SlinkyError, SymbolAssignment};
use crate::{Document, EscapedPath, Segment, Settings, SlinkyError, SymbolAssignment};

mod private {
use crate::{LinkerWriter, PartialLinkerWriter};
use crate::{
file_info::FileInfoSerial, segment::SegmentSerial,
symbol_assignment::SymbolAssignmentSerial, vram_class::VramClassSerial, LinkerWriter,
PartialLinkerWriter,
};

pub trait Sealed {}

impl Sealed for LinkerWriter<'_> {}
impl Sealed for PartialLinkerWriter<'_> {}

impl Sealed for SegmentSerial {}
impl Sealed for FileInfoSerial {}
impl Sealed for VramClassSerial {}
impl Sealed for SymbolAssignmentSerial {}

impl<T> Sealed for Vec<T> {}
}

pub trait ScriptImporter: private::Sealed {
Expand All @@ -35,3 +46,20 @@ pub trait ScriptExporter: private::Sealed {
}

pub trait ScriptGenerator: ScriptImporter + ScriptExporter {}

pub(crate) trait Serial: private::Sealed {
type Output;

fn unserialize(self, settings: &Settings) -> Result<Self::Output, SlinkyError>;
}

impl<T> Serial for Vec<T>
where
T: Serial,
{
type Output = Vec<T::Output>;

fn unserialize(self, settings: &Settings) -> Result<Self::Output, SlinkyError> {
self.into_iter().map(|x| x.unserialize(settings)).collect()
}
}
10 changes: 6 additions & 4 deletions slinky/src/vram_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use serde::Deserialize;

use crate::{absent_nullable::AbsentNullable, Settings, SlinkyError};
use crate::{absent_nullable::AbsentNullable, traits::Serial, Settings, SlinkyError};

#[derive(PartialEq, Debug, Clone)]
pub struct VramClass {
Expand Down Expand Up @@ -34,8 +34,10 @@ pub(crate) struct VramClassSerial {
pub follows_classes: AbsentNullable<Vec<String>>,
}

impl VramClassSerial {
pub fn unserialize(self, _settings: &Settings) -> Result<VramClass, SlinkyError> {
impl Serial for VramClassSerial {
type Output = VramClass;

fn unserialize(self, _settings: &Settings) -> Result<Self::Output, SlinkyError> {
if self.name.is_empty() {
return Err(SlinkyError::EmptyValue {
name: "name".to_string(),
Expand Down Expand Up @@ -80,7 +82,7 @@ impl VramClassSerial {
});
}

Ok(VramClass {
Ok(Self::Output {
name,
fixed_vram,
fixed_symbol,
Expand Down

0 comments on commit 0b248df

Please sign in to comment.