Skip to content

Commit

Permalink
Produce an error if the user specifies an empty conditional inclusion…
Browse files Browse the repository at this point in the history
…/exclusion for any entry

Closes #75
  • Loading branch information
AngheloAlf committed Aug 12, 2024
1 parent f552917 commit fabde98
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 16 deletions.
8 changes: 4 additions & 4 deletions docs/file_format/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ segments:
### Valid values
A list of two-tuples of strings.
A non-empty list of two-tuples of strings.
## `include_if_all`

Expand Down Expand Up @@ -363,7 +363,7 @@ segments:
### Valid values
A list of two-tuples of strings.
A non-empty list of two-tuples of strings.
## `exclude_if_any`

Expand Down Expand Up @@ -395,7 +395,7 @@ segments:
### Valid values
A list of two-tuples of strings.
A non-empty list of two-tuples of strings.
## `exclude_if_all`

Expand Down Expand Up @@ -427,4 +427,4 @@ segments:
### Valid values
A list of two-tuples of strings.
A non-empty list of two-tuples of strings.
22 changes: 22 additions & 0 deletions slinky/src/absent_nullable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ impl<T> AbsentNullable<T> {
}
}

pub fn get_non_null_not_empty<F>(self, name: &str, default: F) -> Result<T, SlinkyError>
where
F: FnOnce() -> T,
T: Default + PartialEq,
{
match self {
AbsentNullable::Absent => Ok(default()),
AbsentNullable::Null => Err(SlinkyError::NullValueOnNonNull {
name: name.to_string(),
}),
AbsentNullable::Value(v) => {
if v == T::default() {
Err(SlinkyError::EmptyValue {
name: name.to_string(),
})
} else {
Ok(v)
}
}
}
}

pub fn get_non_null_no_default(self, name: &str) -> Result<Option<T>, SlinkyError> {
match self {
AbsentNullable::Absent => Ok(None),
Expand Down
8 changes: 4 additions & 4 deletions slinky/src/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,16 @@ impl FileInfoSerial {

let include_if_any = self
.include_if_any
.get_non_null("include_if_any", Vec::new)?;
.get_non_null_not_empty("include_if_any", Vec::new)?;
let include_if_all = self
.include_if_all
.get_non_null("include_if_all", Vec::new)?;
.get_non_null_not_empty("include_if_all", Vec::new)?;
let exclude_if_any = self
.exclude_if_any
.get_non_null("exclude_if_any", Vec::new)?;
.get_non_null_not_empty("exclude_if_any", Vec::new)?;
let exclude_if_all = self
.exclude_if_all
.get_non_null("exclude_if_all", Vec::new)?;
.get_non_null_not_empty("exclude_if_all", Vec::new)?;

Ok(FileInfo {
path,
Expand Down
8 changes: 4 additions & 4 deletions slinky/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,16 @@ impl SegmentSerial {

let include_if_any = self
.include_if_any
.get_non_null("include_if_any", Vec::new)?;
.get_non_null_not_empty("include_if_any", Vec::new)?;
let include_if_all = self
.include_if_all
.get_non_null("include_if_all", Vec::new)?;
.get_non_null_not_empty("include_if_all", Vec::new)?;
let exclude_if_any = self
.exclude_if_any
.get_non_null("exclude_if_any", Vec::new)?;
.get_non_null_not_empty("exclude_if_any", Vec::new)?;
let exclude_if_all = self
.exclude_if_all
.get_non_null("exclude_if_all", Vec::new)?;
.get_non_null_not_empty("exclude_if_all", Vec::new)?;

let alloc_sections = self
.alloc_sections
Expand Down
8 changes: 4 additions & 4 deletions slinky/src/symbol_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ impl SymbolAssignmentSerial {

let include_if_any = self
.include_if_any
.get_non_null("include_if_any", Vec::new)?;
.get_non_null_not_empty("include_if_any", Vec::new)?;
let include_if_all = self
.include_if_all
.get_non_null("include_if_all", Vec::new)?;
.get_non_null_not_empty("include_if_all", Vec::new)?;
let exclude_if_any = self
.exclude_if_any
.get_non_null("exclude_if_any", Vec::new)?;
.get_non_null_not_empty("exclude_if_any", Vec::new)?;
let exclude_if_all = self
.exclude_if_all
.get_non_null("exclude_if_all", Vec::new)?;
.get_non_null_not_empty("exclude_if_all", Vec::new)?;

Ok(SymbolAssignment {
name,
Expand Down
4 changes: 4 additions & 0 deletions tests/panics/file_exclude_if_all_empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
segments:
- name: main
files:
- { path: main.o, exclude_if_all: [] }
4 changes: 4 additions & 0 deletions tests/panics/file_exclude_if_any_empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
segments:
- name: main
files:
- { path: main.o, exclude_if_any: [] }
4 changes: 4 additions & 0 deletions tests/panics/file_include_if_all_empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
segments:
- name: main
files:
- { path: main.o, include_if_all: [] }
4 changes: 4 additions & 0 deletions tests/panics/file_include_if_any_empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
segments:
- name: main
files:
- { path: main.o, include_if_any: [] }
5 changes: 5 additions & 0 deletions tests/panics/segment_exclude_if_all_empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
segments:
- name: main
exclude_if_all: []
files:
- { path: main.o }
5 changes: 5 additions & 0 deletions tests/panics/segment_exclude_if_any_empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
segments:
- name: main
exclude_if_any: []
files:
- { path: main.o }
5 changes: 5 additions & 0 deletions tests/panics/segment_include_if_all_empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
segments:
- name: main
include_if_all: []
files:
- { path: main.o }
5 changes: 5 additions & 0 deletions tests/panics/segment_include_if_any_empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
segments:
- name: main
include_if_any: []
files:
- { path: main.o }
9 changes: 9 additions & 0 deletions tests/panics/symbol_assignment_exclude_if_all_empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
segments:
- name: main
files:
- { path: main.o }

symbol_assignments:
- name: dummy_test
value: 0x80801234
exclude_if_all: []
9 changes: 9 additions & 0 deletions tests/panics/symbol_assignment_exclude_if_any_empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
segments:
- name: main
files:
- { path: main.o }

symbol_assignments:
- name: dummy_test
value: 0x80801234
exclude_if_any: []
9 changes: 9 additions & 0 deletions tests/panics/symbol_assignment_include_if_all_empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
segments:
- name: main
files:
- { path: main.o }

symbol_assignments:
- name: dummy_test
value: 0x80801234
include_if_all: []
9 changes: 9 additions & 0 deletions tests/panics/symbol_assignment_include_if_any_empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
segments:
- name: main
files:
- { path: main.o }

symbol_assignments:
- name: dummy_test
value: 0x80801234
include_if_any: []

0 comments on commit fabde98

Please sign in to comment.