Skip to content

Commit

Permalink
refactor: Rename JanetArgs::get_unwrapped to JanetArgs::try_get
Browse files Browse the repository at this point in the history
Breaking change
  • Loading branch information
GrayJack committed Apr 22, 2024
1 parent 31978b7 commit 8cf6b30
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1786,16 +1786,33 @@ 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 and tries to convert to `T`.
fn get_unwrapped<T: TryFrom<Janet>>(&self, index: usize) -> Result<T, T::Error>;
///
/// # 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 res = args.try_get::<i32>(0);
///
/// // Rest of the function
/// todo!()
/// }
/// ```
fn try_get<T: TryFrom<Janet>>(&self, index: usize) -> Result<T, T::Error>;

/// Get the argument at the `index` position and convert to `T`, if that fails,
/// returns the `default` value.
///
/// # Examples
///
/// ```
/// use janetrs::{bad_slot, janet_fn, Janet, JanetArgs};
/// 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.
Expand All @@ -1819,7 +1836,7 @@ pub trait JanetArgs {
/// # Examples
///
/// ```
/// use janetrs::{bad_slot, janet_fn, Janet, JanetArgs};
/// use janetrs::{janet_fn, Janet, JanetArgs};
///
/// // Lets say it's a function that receives a second argument that change de behavior of
/// // the function
Expand All @@ -1842,7 +1859,7 @@ pub trait JanetArgs {
/// # Examples
///
/// ```
/// use janetrs::{bad_slot, janet_fn, Janet, JanetArgs, JanetString};
/// use janetrs::{janet_fn, Janet, JanetArgs, JanetString};
///
/// #[janet_fn(arity(fix(1)))]
/// fn my_func(args: &mut [Janet]) -> Janet {
Expand All @@ -1856,7 +1873,7 @@ pub trait JanetArgs {
}

impl JanetArgs for [Janet] {
fn get_unwrapped<T: TryFrom<Janet>>(&self, index: usize) -> Result<T, T::Error> {
fn try_get<T: TryFrom<Janet>>(&self, index: usize) -> Result<T, T::Error> {
T::try_from(*self.get(index).unwrap_or(&Janet::nil()))
}

Expand Down

0 comments on commit 8cf6b30

Please sign in to comment.