Skip to content

Commit 9d569cf

Browse files
committed
docs/extend: extend guiding principles
Add a note on usage of `Error` vs `Result`. Rephrase some parts for clarity. Signed-off-by: Daniel Maslowski <[email protected]>
1 parent fb0dbeb commit 9d569cf

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

docs/extend.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
# Extend with new variants and features
22

3-
The following is a brief list of guiding principles to extend `intel_fw`.
4-
As a library, it should to adhere to those as requirements in order to provide
3+
These guiding principles are meant to help extending `intel_fw` over time.
4+
As a library, it should adhere to those as requirements in order to provide
55
guarantees to applications working with it. Generally, it should follow the
66
[Rust API guidelines](https://rust-lang.github.io/api-guidelines/about.html).
77

88
## Do not panic!
99

1010
Parsing firmware means looking for and acting on offsets and sizes frequently,
1111
and they always need to be checked to stay within the bounds of the given data.
12-
Never `unwrap()`, which means an _intentional panic_, in case something cannot
13-
be found, read, or recognized. Instead, return instances of `Self` for structs,
14-
wrapped in a `Result<Self, CustomError>` or possibly `Option<Self>`.
12+
Never `.unwrap()` or `.expect()`, which mean an _intentional panic_, in case
13+
something cannot be found, read, or recognized. Instead, return instances of
14+
`Self` for structs, wrapped in a `Result<Self, CustomError>` or possibly
15+
`Option<Self>`. Do not `assert!()` or explicitly `panic!()` either.
16+
17+
## `Option` or `Result`
18+
19+
In general, use `Option<Self>` for things that might not be found. For example,
20+
a full image from a production system would have an IFD at the beginning,
21+
whereas an ME-only image would not. On the other hand, either image is expected
22+
to contain an FPT, so in this case, provide a `Result<Self, CustomError>`,
23+
because not finding an FPT means that something is clearly wrong with the image.
1524

1625
## Continuous parsing
1726

@@ -23,8 +32,9 @@ when there is still remaining data that could be parsed, keep going.
2332

2433
## Let apps provide feedback
2534

26-
In other words, **let the consuming application take care** of taking the result
27-
apart. Nested structures mean that whenever a node in a graph turns into an
35+
Finally, **let the consuming application take care** of taking the result apart.
36+
Nested structures can be thought of as trees, similar to ASTs in programming
37+
languages. Whenever a node in the tree that is a parsing result turns into an
2838
`Error` or `None`, other nodes beside it may still provide useful information.
2939
That information helps the user of an app to understand the context and possibly
3040
report what they are facing or look into the issue themselves.

0 commit comments

Comments
 (0)