Skip to content

Commit f5b3979

Browse files
committed
Implement RdFile
1 parent 8a4bf40 commit f5b3979

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use std::fs;
2+
use std::path::Path;
3+
4+
pub struct RdFile {
5+
pub doc_type: Option<RdDocType>,
6+
}
7+
8+
#[derive(Debug, PartialEq)]
9+
pub enum RdDocType {
10+
Data,
11+
Package,
12+
}
13+
14+
impl RdFile {
15+
pub fn load_from_file(path: &Path) -> anyhow::Result<Self> {
16+
let content = fs::read_to_string(path).map_err(|err| {
17+
anyhow::anyhow!(
18+
"Failed to read Rd file '{path}': {err}",
19+
path = path.to_string_lossy()
20+
)
21+
})?;
22+
23+
let doc_type = parse_doc_type(&content);
24+
Ok(RdFile { doc_type })
25+
}
26+
}
27+
28+
fn parse_doc_type(content: &str) -> Option<RdDocType> {
29+
static RE: std::sync::LazyLock<regex::Regex> =
30+
std::sync::LazyLock::new(|| regex::Regex::new(r"\\docType\{(data|package)\}").unwrap());
31+
32+
let captures = RE.captures(content)?;
33+
34+
match captures.get(1)?.as_str() {
35+
"data" => Some(RdDocType::Data),
36+
"package" => Some(RdDocType::Package),
37+
_ => None,
38+
}
39+
}
40+
41+
#[cfg(test)]
42+
mod tests {
43+
use super::*;
44+
45+
#[test]
46+
fn test_parse_doc_type_data() {
47+
let input = r#"\docType{data}"#;
48+
assert_eq!(parse_doc_type(input), Some(RdDocType::Data));
49+
50+
let input = r#"
51+
% Generated by roxygen2: do not edit by hand
52+
\docType{data}
53+
"#;
54+
assert_eq!(parse_doc_type(input), Some(RdDocType::Data));
55+
56+
let input = r#"
57+
% Generated by roxygen2: do not edit by hand
58+
\docType{data}"
59+
\docType{package}
60+
"#;
61+
assert_eq!(parse_doc_type(input), Some(RdDocType::Data));
62+
}
63+
64+
#[test]
65+
fn test_parse_doc_type_package() {
66+
let input = r#"\docType{package}"#;
67+
assert_eq!(parse_doc_type(input), Some(RdDocType::Package));
68+
}
69+
70+
#[test]
71+
fn test_parse_doc_type_none() {
72+
let input = r#"\title{Something}"#;
73+
assert_eq!(parse_doc_type(input), None);
74+
}
75+
}

crates/ark/src/lsp/inputs/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//
66
//
77

8+
pub mod documentation_rd_file;
89
pub mod library;
910
pub mod package;
1011
pub mod package_description;

0 commit comments

Comments
 (0)