69 lines
2.3 KiB
Rust
69 lines
2.3 KiB
Rust
// file: kb-app-demo-desktop/src/main_window.rs
|
|
// version: 2
|
|
|
|
//! Main-window helpers and workspace README loading.
|
|
|
|
pub(crate) fn workspace_root_dir() -> std::path::PathBuf {
|
|
let manifest_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
return match manifest_dir.parent() {
|
|
std::option::Option::Some(parent) => parent.to_path_buf(),
|
|
std::option::Option::None => manifest_dir,
|
|
};
|
|
}
|
|
|
|
pub(crate) fn load_project_readme() -> ks_core::Result<std::string::String> {
|
|
let path = crate::workspace_root_dir().join("README.md");
|
|
let content = match std::fs::read_to_string(&path) {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => {
|
|
return std::result::Result::Err(ks_core::Error::io(format!(
|
|
"cannot read project README '{}': {error}",
|
|
path.display()
|
|
)));
|
|
},
|
|
};
|
|
return std::result::Result::Ok(strip_readme_metadata_comments(content.as_str()));
|
|
}
|
|
|
|
fn strip_readme_metadata_comments(content: &str) -> std::string::String {
|
|
let mut output = std::string::String::new();
|
|
let mut leading_metadata = true;
|
|
for line in content.lines() {
|
|
let trimmed = line.trim();
|
|
if leading_metadata && is_readme_metadata_comment(trimmed) {
|
|
continue;
|
|
}
|
|
leading_metadata = false;
|
|
output.push_str(line);
|
|
output.push('\n');
|
|
}
|
|
return output;
|
|
}
|
|
|
|
fn is_readme_metadata_comment(line: &str) -> bool {
|
|
if line.starts_with("<!-- file:") || line.starts_with("<!--- file:") {
|
|
return true;
|
|
}
|
|
if line.starts_with("<!-- version:") || line.starts_with("<!--- version:") {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
#[test]
|
|
fn strip_readme_metadata_comments_removes_workspace_header_lines() {
|
|
let content = "<!-- file: README.md -->\n<!-- version: 7 -->\n# Title\nBody\n";
|
|
let cleaned = super::strip_readme_metadata_comments(content);
|
|
assert_eq!(cleaned, "# Title\nBody\n");
|
|
}
|
|
|
|
#[test]
|
|
fn strip_readme_metadata_comments_keeps_later_html_comments() {
|
|
let content = "<!-- file: README.md -->\n# Title\n<!-- normal comment -->\nBody\n";
|
|
let cleaned = super::strip_readme_metadata_comments(content);
|
|
assert_eq!(cleaned, "# Title\n<!-- normal comment -->\nBody\n");
|
|
}
|
|
}
|