Skip to content

Commit

Permalink
refactor: Make JanetArgs::get_matches and `JanetArgs::get_tagged_ma…
Browse files Browse the repository at this point in the history
…tches` not return Option
  • Loading branch information
GrayJack committed Apr 23, 2024
1 parent b64ac2e commit 4120e99
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,7 @@ pub trait JanetArgs {
/// # Janet Panics
/// This function may panic if the item at index does not match any of the `JanetType`
/// in `matches`.
fn get_matches(&self, index: usize, matches: &[JanetType]) -> Option<Janet> {
fn get_matches(&self, index: usize, matches: &[JanetType]) -> Janet {
/// Helper struct to format possible type matches in [`get_matches`] and
/// [`get_tagged_matches`].
///
Expand All @@ -1839,7 +1839,9 @@ pub trait JanetArgs {
}
}

let val = self.get_value(index)?;
let val = self.get_value(index).unwrap_or_else(|| {
crate::jpanic!("bad slot #{}, there is no value in this slot", index)
});
let kind = val.kind();

if !matches.contains(&kind) {
Expand All @@ -1848,7 +1850,7 @@ pub trait JanetArgs {
MatchesFormater(matches)
)
}
Some(val)
val
}

/// Get the argument at the `index` position if they match one if the [`JanetType`] in
Expand All @@ -1857,8 +1859,8 @@ pub trait JanetArgs {
/// # Janet Panics
/// This function may panic if the item at index does not match any of the `JanetType`
/// in `matches`.
fn get_tagged_matches(&self, index: usize, matches: &[JanetType]) -> Option<TaggedJanet> {
self.get_matches(index, matches).map(|val| val.unwrap())
fn get_tagged_matches(&self, index: usize, matches: &[JanetType]) -> TaggedJanet {
self.get_matches(index, matches).unwrap()
}

/// Get the argument at the `index` position and tries to convert to `T`.
Expand Down

0 comments on commit 4120e99

Please sign in to comment.