Comments (2)
Adding support for multiple validators within the library adds more complexity and allocations than is really necessary. Using a wrapper function like you have shown not only works but allows the compiler to perform better optimizations. Also, the wrapper can be improved by being a macro:
macro_rules! join_validators {
// Just uses the question mark operator to call the validators in order of which they are
// specified, returning early if one fails
($($validators:expr),+ $(,)?) => {
|val, ans| { $($validators(val, ans)?;)+ Ok(()) }
};
}
macro_rules! join_on_key_validators {
// Joins all the validator calls with an && as the result is boolean and all must return true
($($validators:expr),+ $(,)?) => {
|val, ans| $($validators(val, ans))&&+
};
}
macro_rules! join_filters {
// This is slightly more complicated as the function calls need to be nested.
//
// The top level macro call sets up the wrapper function and calls the implementation
($($filters:expr),+ $(,)?) => {
|val, ans| join_filters!(@__impl val, ans; $($filters),+ ,)
};
// In the general case, $val represents the filtered value so far, $ans is the answers supplied
// to the wrapper function, $filter and $remaining are the filter functions which are yet to be called.
//
// join_filters! is called recursively with the new $val being $filter($val), after removing
// $filter from the list of filters to be called.
(@__impl $val:expr, $ans:expr; $filter:expr, $($remaining:expr,)*) => {
join_filters!(@__impl $filter($val, $ans), $ans; $($remaining,)*)
};
// This is the base case where there are no more functions to call, so just return the value as it is fully filtered
(@__impl $val:expr, $ans:expr;) => {
$val
};
}
These allow there to be any number of comma separated functions to be supplied, and will create a wrapper that calls all the functions.
In my testing, when declaring the functions as closures, it is not able to infer the type and requires a type annotation. Otherwise, closures work just fine.
If there are a large enough amount of people requesting for multiple validators/filters to be part of the library, then I will re-open this issue and add it to the library. However, I personally don't see it to be worth it.
from requestty.
I'm currently using this functions, but I think if supported it would be better
pub fn join_str_validators<'a>(
mut left: impl FnMut(&str, &Answers) -> Result<(), String> + 'a,
mut right: impl FnMut(&str, &Answers) -> Result<(), String> + 'a,
) -> impl FnMut(&str, &Answers) -> Result<(), String> + 'a {
move |str_value: &str, answers: &Answers| {
if let Err(err) = left(str_value, &answers) {
Err(err)
} else if let Err(err) = right(str_value, &answers) {
Err(err)
} else {
Ok(())
}
}
}
pub fn join_on_key_validator<'a>(
mut left: impl FnMut(&str, &Answers) -> bool + 'a,
mut right: impl FnMut(&str, &Answers) -> bool + 'a,
) -> impl FnMut(&str, &Answers) -> bool + 'a {
move |str_value, answers| left(str_value, &answers) && right(str_value, &answers)
}
from requestty.
Related Issues (18)
- Builder error: unsupported pair HOT 2
- Remove screen size limit error HOT 1
- divisor of zero error requestty-ui-0.3.0 in MacOS 12.3.1 Docker 4.5.0 (74594) HOT 6
- dbg code in 0.4.0 HOT 2
- [Feature] Choose order of all options HOT 3
- [Bug] Requestty view doens't addapt when terminal size is changed + Panics if smaller than current view height HOT 1
- [Bug] Invalid cursor position
- Bugs with wide characters (like CJK) HOT 2
- Incorrect cursor location when input prompt spans multiple lines HOT 3
- Allow filtering a Select prompt HOT 2
- `adjust_page` called before `height` or `render` HOT 2
- Greyed-out text for the first autocompletion HOT 3
- Color input red as long as it doesn't pass validation HOT 1
- Support abort key HOT 1
- default for input returns empty value HOT 3
- EditorBuilder: Change Editor Programatically HOT 1
- Allow setting of custom symbols
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from requestty.