Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move all built-in functions onto the new macro #116

Open
rscarson opened this issue Oct 17, 2023 · 0 comments
Open

Move all built-in functions onto the new macro #116

rscarson opened this issue Oct 17, 2023 · 0 comments

Comments

@rscarson
Copy link
Owner

The builtins (src/functions/builtins/*) are all still using the older syntax, eg:

const LIST: FunctionDefinition = FunctionDefinition {
    name: "api_list",
    category: Some("network"),
    description: "List all registered APIs",
    arguments: Vec::new,
    handler: |_function, _token, state, _args| {
        let mut keys = state.apis.keys().collect::<Vec<&String>>();
        keys.sort();

        let definitions = keys
            .iter()
            .map(|k| format!("{}: {}", k, state.apis.get(*k).unwrap()));
        let t = definitions.collect::<Vec<String>>().join("\n");

        Ok(Value::String(t))
    },
};

Instead of the macro:

define_function!(
    name = sha256,
    category = "cryptography",
    description = "Returns the SHA256 hash of a given string",
    arguments = [function_arg!("plural", "input":Any)],
    handler = |function, token, state, args| {
        use sha2::{Digest, Sha256};
        let input = args.get("input").required().as_string();

        let mut hasher = Sha256::new();
        hasher.update(input);

        let s = format!("{:X}", hasher.finalize());
        Ok(Value::String(s))
    }
);

Examples have been moved over but these still need to be redone in the new syntax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant