我正在使用Tauri (Rust后端/Svelte前端)进行一个项目。在应用程序启动时,我读取目录(用户库)中的每个文件及其内容。
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
}我对铁锈一点也不熟悉,对现在该怎么做有点不确定。最终,我需要一个清理版本的数据在我的前端。这涉及到一些Regex和重组。然而,当我试图对文件进行Regex时,我的String (从文件中读取)不是str,我遇到了一些问题。我应该在前面做吗?我正在考虑用{"fileName": content}[]创建一个JSON,但它似乎比JS更适合使用Rust。
编辑:我想用来提取[[...]]链接的函数:
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()
}发布于 2022-08-19 11:54:54
多亏了@BurntSushi5和@AlexVergara,我才能发现一系列的小错误。
首先,我让extract_hashtags在my_czstom_command里面。一旦我把它移到更高的层次,这个问题就解决了。其次,我确实错过了let _text = extract_hashtags(&contents);中的let _text = extract_hashtags(&contents);,它允许我将自己的String解析为str。最后,我用正则表达式做了一个复制错误。用r"\[\[\w*\]\]"代替它,现在它工作得很好。
所以现在整个想法都是这样的:
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()
}https://stackoverflow.com/questions/73414685
复制相似问题