Skip to content

Commit

Permalink
feat: Add JanetArgs::get_or_default trait method
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayJack committed Apr 22, 2024
1 parent 9e44246 commit a8c8904
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,33 @@ pub trait JanetArgs {
.unwrap_or(default)
}

/// Get the argument at the `index` position and convert to `T`, if that fails,
/// returns the [`Default::default`] value for `T`.
///
/// # Examples
///
/// ```
/// use janetrs::{janet_fn, Janet, JanetArgs};
///
/// // Lets say it's a function that if receives an argument, if is not the wanted type, it
/// // defaults to the given value.
/// #[janet_fn(arity(range(0, 1)))]
/// fn my_func(args: &mut [Janet]) -> Janet {
/// let my_flag = args.get_or_default(0);
///
/// // Rest of the function
/// todo!()
/// }
/// ```
fn get_or_default<T>(&self, index: usize) -> T
where
T: TryFrom<Janet> + Default,
{
self.get_value(index)
.and_then(|val| T::try_from(val).ok())
.unwrap_or_default()
}

/// Get the argument at the `index` position, if it's Janet nil, returns the `default`
/// value, but janet panics if the the value is different than nil and fail to convert
/// to `T`.
Expand Down

0 comments on commit a8c8904

Please sign in to comment.