Skip to content

Commit

Permalink
feat: Add JanetArgs::get_matches and `JanetArgs::get_tagged_matches…
Browse files Browse the repository at this point in the history
…` trait methods
  • Loading branch information
GrayJack committed Apr 23, 2024
1 parent 9191b0c commit 9a3adfa
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,55 @@ pub trait JanetArgs {
/// Get the argument at the `index` position as the [`TaggedJanet`] type.
fn get_tagged(&self, index: usize) -> Option<TaggedJanet>;

/// Get the argument at the `index` position if they match one if the [`JanetType`] in
/// `matches`, janet-panicking otherwise.
///
/// # 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> {
/// Helper struct to format possible type matches in [`get_matches`] and
/// [`get_tagged_matches`].
///
/// [`get_matches`]: JanetArgs::get_matches
/// [`get_tagged_matches`]: JanetArgs::get_tagged_matches
struct MatchesFormater<'a>(&'a [JanetType]);

impl fmt::Display for MatchesFormater<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let len = self.0.len() - 1;
for (i, ty) in self.0.iter().enumerate() {
write!(f, "{ty}")?;
if i < len {
write!(f, "|")?;
}
}
Ok(())
}
}

let val = self.get_value(index)?;
let kind = val.kind();

if !matches.contains(&kind) {
crate::jpanic!(
"bad slot #{index}, expected {}, got {kind}",
MatchesFormater(matches)
)
}
Some(val)
}

/// Get the argument at the `index` position if they match one if the [`JanetType`] in
/// `matches` as the [`TaggedJanet`] type, janet-panicking otherwise.
///
/// # 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())
}

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

0 comments on commit 9a3adfa

Please sign in to comment.