The _hello-world_ example demonstrates how to generate and compile a fully functional package from an sdf-package.yaml file. The example will utilize regex to look up and convert the word good to great.
- Install SDF if you haven't already.
- Look at the sdf-package.yaml to see the type and function definitions.
Use the generate scaffold to create the package sandbox:
sdf generateThe generator reads the sdf-package.yaml file and creates the following Rust project:
$ tree
.
├── README.MD
├── rust
│ └── hello-world
│ ├── Cargo.toml
│ ├── README.md
│ └── src
│ ├── hello_world.rs
│ └── lib.rs
└── sdf-package.yamlEach function generates a new file. In this case, we only have one function, which generated the hello_world.rs file.
Open the rust/hello-world/src/hello_world.rs file and add the following code:
use std::sync::LazyLock;
use regex::Regex;
static REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"good").unwrap());
#[sdf(fn_name = "hello-world")]
pub(crate) fn hello_world(input: Good) -> Result<Great> {
let output = REGEX.replace_all(input.as_str(), "great").to_string();
Ok(output)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_hello_world() {
let input = "This is good.".to_string();
let output = hello_world(input);
assert_eq!(output.unwrap(), "This is great.".to_string());
}
}The test we've included is not strictly necessary but a best practice.
We'll also need to update the Cargo.toml file to include the regex dependency.
[dependencies]
...
regex = "1"Add regex to the end of the file.
To build the code, run:
sdf buildThe build command creates a fully functional package that can be imported by other packages or dataflows.
Inside the sdf-package.yaml directory, run:
sdf test>> test function hello-world --value "This is good."
This is great.You also have the option to test your routines using rust test utilities. Inside the rust/hello-world directory, run the tests using Rust:
cargo test🎉 Congratulations! You have successfuly built your first package.