-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from YarnSpinnerTool/validate-optional-parame…
…ters
- Loading branch information
Showing
8 changed files
with
146 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#![allow(missing_debug_implementations)] | ||
|
||
use yarnspinner_macros::all_tuples; | ||
|
||
/// Marker trait for valid optionality hints. | ||
pub trait Optionality {} | ||
|
||
/// An optional parameter or a tuple where | ||
/// the last element is optional. | ||
pub struct Optional; | ||
|
||
impl Optionality for Optional {} | ||
|
||
/// A parameter that is required. | ||
pub struct Required; | ||
|
||
impl Optionality for Required {} | ||
|
||
/// A valid chain of optionality hints | ||
/// i.e. a chain where no optional element follows | ||
/// a required element. | ||
pub trait AllowedOptionalityChain { | ||
/// The optionality hint of the last element in the chain. | ||
type Last: Optionality; | ||
} | ||
|
||
impl AllowedOptionalityChain for () { | ||
type Last = Required; | ||
} | ||
|
||
impl<O: Optionality> AllowedOptionalityChain for (O,) { | ||
type Last = O; | ||
} | ||
|
||
impl<O: Optionality> AllowedOptionalityChain for (Required, O) { | ||
type Last = O; | ||
} | ||
|
||
impl AllowedOptionalityChain for (Optional, Optional) { | ||
type Last = Optional; | ||
} | ||
|
||
// `impl AllowedOptionalityChain for (Optional, Required) {}` | ||
// is intentionally missing (that's the whole point of this trait). | ||
|
||
macro_rules! impl_chain { | ||
// Implementations for zero, one and two-element tuples covered manually. | ||
() => {}; | ||
($p1:ident) => {}; | ||
($p1:ident, $p2:ident) => {}; | ||
($($param:ident),*) => { | ||
// A tuple of three or more elements is valid | ||
// if all two-pairs from left to right are valid. | ||
// example: (A, B, C) is valid if (A, B) and (B, C) are. | ||
impl_chain!(@pairwise [$($param),*] [] $($param,)*); | ||
}; | ||
(@pairwise [$($param:ident),*] [$($tt:tt)*] $a:ident, $b:ident,) => { | ||
impl_chain!(@emit [$($param),*] [$($tt)* ($a, $b): AllowedOptionalityChain,] $b,); | ||
}; | ||
(@pairwise [$($param:ident),*] [$($tt:tt)*] $a:ident, $b:ident, $($tail:ident,)*) => { | ||
impl_chain!(@pairwise [$($param),*] [$($tt)* ($a, $b): AllowedOptionalityChain,] $b, $($tail,)*); | ||
}; | ||
(@emit [$($param: ident),*] [$($tt:tt)*] $last:ident,) => { | ||
impl<$($param: Optionality),*> AllowedOptionalityChain for ($($param),*) where $($tt)* { | ||
type Last = $last; | ||
} | ||
}; | ||
} | ||
|
||
all_tuples!(impl_chain, 0, 16, O); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters