Giter Club home page Giter Club logo

Comments (2)

Lutetium-Vanadium avatar Lutetium-Vanadium commented on May 28, 2024 1

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.

TheAwiteb avatar TheAwiteb commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.