Reputation: 404
I'm working on a project using Tauri (Rust Backend/Svelte Frontend). On application start I'm reading every file and its contents in a directory (the users library).
for file in WalkDir::new("path_to_library")
.into_iter()
.filter_map(|file| file.ok()) {
if file.metadata().unwrap().is_file() {
let contents = fs::read_to_string(file.path())
.expect("Should have been able to read the file");
// extract links from contents (is an object)
// append to previously extracted links
}
// send to frontend
}
I'm not at all familiar with Rust and somewhat uncertain on how to proceed now. Ultimately, I need a cleaned up version of the data in my frontend. This involves some Regex and restructuring. However, I ran into problems with Rust being unhappy that my String
(read from the file) wasn't a str
when I tried to Regex the files. Should I better do it in the frontend? I was thinking about just creating a JSON with {"fileName": content}[]
, but it feels like Rust would be much more suitable manipulation tool than JS.
EDIT: The function I wanted to use to extract the [[...]]
links:
fn extract_links(text: &str) -> HashSet<&str> {
lazy_static! {
static ref LINKS_REGEX : Regex = Regex::new(
r"/(\[(?:\[??[^\[]*?\]\]))/gm"
).unwrap();
}
LINKS_REGEX.find_iter(text).map(|mat| mat.as_str()).collect()
}
Upvotes: 0
Views: 147
Reputation: 404
Thanks to @BurntSushi5 and @AlexVergara I was able to figure out a series of small mistakes.
First, I had extract_hashtags
inside my_czstom_command
. Once I moved it on a higher level, that problem was solved. Second, I did miss the &
in let _text = extract_hashtags(&contents);
which let me actually parse my String
as str
. And finally, I made a copy error with my regex. Replaced it with r"\[\[\w*\]\]"
and now it works just fine.
So the whole think looks like this now:
fn extract_hashtags(text: &str) -> HashSet<&str> {
lazy_static! {
static ref HASHTAG_REGEX : Regex = Regex::new(
r"\[\[\w*\]\]"
).unwrap();
}
HASHTAG_REGEX.find_iter(text).map(|mat| mat.as_str()).collect()
}
#[tauri::command] // this comes from Tauri
fn my_custom_command() -> String {
let mut data = ""; // this I still have to figure out, should be a JSON sometime
for file in WalkDir::new("C:/Users/Micha/Desktop/Mapmind/library/")
.into_iter()
.filter_map(|file| file.ok()) {
if file.metadata().unwrap().is_file() {
let contents = fs::read_to_string(file.path())
.expect("Should have been able to read the file");
let _text = extract_hashtags(&contents);
// add _text to data here
println!("{}", file.path().display());
println!("{contents}");
println!("{:?}", _text)
}
}
data.into()
}
Upvotes: 1