Skip to content

Commit

Permalink
SlinkyError
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Feb 22, 2024
1 parent 85cd1ce commit 2253908
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion slinky-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use std::path::Path;
use slinky::{Document, LinkerWriter};

fn main() {
let document = Document::read(Path::new("test_case.yaml"));
// TODO: don't use expect?
let document = Document::read_file(Path::new("test_case.yaml")).expect("Error while reading input file");

let mut writer = LinkerWriter::new(&document.options);
writer.begin_sections();
Expand Down
1 change: 1 addition & 0 deletions slinky/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ edition = "2021"
[dependencies]
serde = {version="1.0.197", features = ["derive"]}
serde_yaml = "0.9.32"
thiserror = "1.0.57"
16 changes: 11 additions & 5 deletions slinky/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{fs, path::Path};

use serde::Deserialize;

use crate::{Options, Segment};
use crate::{Options, Segment, SlinkyError};

#[derive(Deserialize, PartialEq, Debug)]
pub struct Document {
Expand All @@ -16,9 +16,15 @@ pub struct Document {
}

impl Document {
pub fn read(path: &Path) -> Self {
let f = fs::File::open(path).expect("asdfasdf");
let mut document: Document = serde_yaml::from_reader(f).expect("");
pub fn read_file(path: &Path) -> Result<Self, SlinkyError> {
let f = match fs::File::open(path) {
Ok(f) => f,
Err(e) => return Err(SlinkyError::FailedFileOpen{description: e.to_string()}),
};
let mut document: Document = match serde_yaml::from_reader(f) {
Ok(d) => d,
Err(e) => return Err(SlinkyError::FailedYamlParsing{description: e.to_string()}),
};

for segment in &mut document.segments {
segment.use_subalign = Some(document.options.use_subalign);
Expand All @@ -27,6 +33,6 @@ impl Document {
segment.wildcard_sections = Some(document.options.wildcard_sections);
}

document
Ok(document)
}
}
11 changes: 11 additions & 0 deletions slinky/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */


#[derive(Clone, Debug, PartialEq, Eq, Hash, thiserror::Error)]
pub enum SlinkyError {
#[error("Unable to open file: {description}")]
FailedFileOpen {description: String},
#[error("Unable parse yaml: {description}")]
FailedYamlParsing { description: String },
}
4 changes: 4 additions & 0 deletions slinky/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */

mod error;

mod paths_configs;
mod segment_symbols_style;
mod options;
Expand All @@ -14,6 +16,8 @@ mod document;
mod linker_writer;


pub use error::SlinkyError;

pub use paths_configs::PathsConfigs;
pub use segment_symbols_style::SegmentSymbolsStyle;
pub use options::Options;
Expand Down

0 comments on commit 2253908

Please sign in to comment.