|
1 |
| -use crate::{ast::AstToken, kinds::SyntaxKind::*}; |
| 1 | +use crate::kinds::SyntaxKind::{TOKEN_PATH_ABS, TOKEN_PATH_HOME, TOKEN_PATH_REL}; |
| 2 | + |
2 | 3 | use rowan::{ast::AstNode as OtherAstNode, NodeOrToken};
|
3 | 4 |
|
| 5 | +pub use super::nodes::Path; |
| 6 | +use super::{ |
| 7 | + nodes::{PathAbs, PathHome, PathRel, PathSearch}, |
| 8 | + AstToken, InterpolPart, PathContent, |
| 9 | +}; |
4 | 10 | use crate::ast;
|
5 | 11 |
|
6 |
| -use super::{InterpolPart, PathContent}; |
| 12 | +/// Base trait for all path node types |
| 13 | +pub trait PathNode: ast::AstNode {} |
| 14 | + |
| 15 | +impl PathNode for PathAbs {} |
| 16 | +impl PathNode for PathRel {} |
| 17 | +impl PathNode for PathHome {} |
| 18 | +impl PathNode for PathSearch {} |
| 19 | +impl PathNode for Path {} |
7 | 20 |
|
8 |
| -impl ast::nodes::Path { |
9 |
| - pub fn parts(&self) -> impl Iterator<Item = InterpolPart<PathContent>> { |
10 |
| - self.syntax().children_with_tokens().map(|child| match child { |
| 21 | +fn extract_path_parts<T: ast::AstNode>(node: &T) -> Vec<InterpolPart<PathContent>> { |
| 22 | + node.syntax() |
| 23 | + .children_with_tokens() |
| 24 | + .map(|child| match child { |
11 | 25 | NodeOrToken::Token(token) => {
|
12 |
| - assert_eq!(token.kind(), TOKEN_PATH); |
| 26 | + debug_assert!(matches!( |
| 27 | + token.kind(), |
| 28 | + TOKEN_PATH_ABS | TOKEN_PATH_REL | TOKEN_PATH_HOME |
| 29 | + )); |
13 | 30 | InterpolPart::Literal(PathContent::cast(token).unwrap())
|
14 | 31 | }
|
15 | 32 | NodeOrToken::Node(node) => {
|
16 | 33 | InterpolPart::Interpolation(ast::Interpol::cast(node.clone()).unwrap())
|
17 | 34 | }
|
18 | 35 | })
|
| 36 | + .collect() |
| 37 | +} |
| 38 | + |
| 39 | +// Direct methods for interpolatable path types |
| 40 | +impl PathAbs { |
| 41 | + pub fn parts(&self) -> Vec<InterpolPart<PathContent>> { |
| 42 | + extract_path_parts(self) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl PathRel { |
| 47 | + pub fn parts(&self) -> Vec<InterpolPart<PathContent>> { |
| 48 | + extract_path_parts(self) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl PathHome { |
| 53 | + pub fn parts(&self) -> Vec<InterpolPart<PathContent>> { |
| 54 | + extract_path_parts(self) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Direct methods for search path |
| 59 | +impl PathSearch { |
| 60 | + /// Get the content of a search path |
| 61 | + pub fn content(&self) -> Option<PathContent> { |
| 62 | + self.syntax() |
| 63 | + .children_with_tokens() |
| 64 | + .filter_map(|child| child.into_token().and_then(PathContent::cast)) |
| 65 | + .next() |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// Extension methods for the Path enum |
| 70 | +impl Path { |
| 71 | + /// Get parts from any path type in a unified way |
| 72 | + pub fn parts(&self) -> Vec<InterpolPart<PathContent>> { |
| 73 | + match self { |
| 74 | + // For interpolatable paths, get their parts |
| 75 | + Path::PathAbs(p) => p.parts(), |
| 76 | + Path::PathRel(p) => p.parts(), |
| 77 | + Path::PathHome(p) => p.parts(), |
| 78 | + // For search paths, return a single literal component if content exists |
| 79 | + Path::PathSearch(p) => { |
| 80 | + if let Some(content) = p.content() { |
| 81 | + vec![InterpolPart::Literal(content)] |
| 82 | + } else { |
| 83 | + vec![] |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + pub fn is_search(&self) -> bool { |
| 90 | + matches!(self, Path::PathSearch(_)) |
| 91 | + } |
| 92 | + |
| 93 | + pub fn is_interpolatable(&self) -> bool { |
| 94 | + !self.is_search() |
19 | 95 | }
|
20 | 96 | }
|
21 | 97 |
|
22 | 98 | #[cfg(test)]
|
23 | 99 | mod tests {
|
24 | 100 | use rowan::ast::AstNode;
|
25 | 101 |
|
| 102 | + use super::InterpolPart; |
26 | 103 | use crate::{
|
27 |
| - ast::{self, AstToken, InterpolPart, PathContent}, |
| 104 | + ast::{self, Path}, |
28 | 105 | Root,
|
29 | 106 | };
|
30 | 107 |
|
31 | 108 | #[test]
|
32 |
| - fn parts() { |
33 |
| - fn assert_eq_ast_ctn(it: &mut dyn Iterator<Item = InterpolPart<PathContent>>, x: &str) { |
34 |
| - let tmp = it.next().expect("unexpected EOF"); |
35 |
| - if let InterpolPart::Interpolation(astn) = tmp { |
36 |
| - assert_eq!(astn.expr().unwrap().syntax().to_string(), x); |
37 |
| - } else { |
38 |
| - unreachable!("unexpected literal {:?}", tmp); |
| 109 | + fn test_path_types() { |
| 110 | + // Absolute path |
| 111 | + let inp = "/foo/bar"; |
| 112 | + let expr = Root::parse(inp).ok().unwrap().expr().unwrap(); |
| 113 | + if let ast::Expr::PathAbs(p) = expr { |
| 114 | + let path = Path::cast(p.syntax().clone()).unwrap(); |
| 115 | + assert!(path.is_interpolatable()); |
| 116 | + assert!(!path.is_search()); |
| 117 | + } |
| 118 | + |
| 119 | + // Search path |
| 120 | + let inp = "<nixpkgs>"; |
| 121 | + let expr = Root::parse(inp).ok().unwrap().expr().unwrap(); |
| 122 | + if let ast::Expr::PathSearch(p) = expr { |
| 123 | + let path = Path::cast(p.syntax().clone()).unwrap(); |
| 124 | + assert!(!path.is_interpolatable()); |
| 125 | + assert!(path.is_search()); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn test_parts() { |
| 131 | + // Test parts with absolute path |
| 132 | + let inp = "/foo/bar"; |
| 133 | + let expr = Root::parse(inp).ok().unwrap().expr().unwrap(); |
| 134 | + if let ast::Expr::PathAbs(p) = expr { |
| 135 | + let path = Path::cast(p.syntax().clone()).unwrap(); |
| 136 | + |
| 137 | + let parts = path.parts(); |
| 138 | + assert_eq!(parts.len(), 1); |
| 139 | + |
| 140 | + match &parts[0] { |
| 141 | + InterpolPart::Literal(content) => { |
| 142 | + assert_eq!(content.text(), "/foo/bar"); |
| 143 | + } |
| 144 | + _ => panic!("Expected literal part"), |
39 | 145 | }
|
40 | 146 | }
|
41 | 147 |
|
42 |
| - fn assert_eq_lit(it: &mut dyn Iterator<Item = InterpolPart<PathContent>>, x: &str) { |
43 |
| - let tmp = it.next().expect("unexpected EOF"); |
44 |
| - if let InterpolPart::Literal(astn) = tmp { |
45 |
| - assert_eq!(astn.syntax().text(), x); |
46 |
| - } else { |
47 |
| - unreachable!("unexpected interpol {:?}", tmp); |
| 148 | + // Test parts with interpolated path |
| 149 | + let inp = r#"./a/${"hello"}"#; |
| 150 | + let expr = Root::parse(inp).ok().unwrap().expr().unwrap(); |
| 151 | + if let ast::Expr::PathRel(p) = expr { |
| 152 | + let path = Path::cast(p.syntax().clone()).unwrap(); |
| 153 | + |
| 154 | + let parts = path.parts(); |
| 155 | + assert_eq!(parts.len(), 2); |
| 156 | + |
| 157 | + match &parts[0] { |
| 158 | + InterpolPart::Literal(content) => { |
| 159 | + assert_eq!(content.text(), "./a/"); |
| 160 | + } |
| 161 | + _ => panic!("Expected literal part"), |
| 162 | + } |
| 163 | + |
| 164 | + match &parts[1] { |
| 165 | + InterpolPart::Interpolation(_) => {} // Success |
| 166 | + _ => panic!("Expected interpolation part"), |
48 | 167 | }
|
49 | 168 | }
|
50 | 169 |
|
51 |
| - let inp = r#"./a/b/${"c"}/${d}/e/f"#; |
| 170 | + // Test parts with search path |
| 171 | + let inp = "<nixpkgs>"; |
52 | 172 | let expr = Root::parse(inp).ok().unwrap().expr().unwrap();
|
53 |
| - match expr { |
54 |
| - ast::Expr::Path(p) => { |
55 |
| - let mut it = p.parts(); |
56 |
| - assert_eq_lit(&mut it, "./a/b/"); |
57 |
| - assert_eq_ast_ctn(&mut it, "\"c\""); |
58 |
| - assert_eq_lit(&mut it, "/"); |
59 |
| - assert_eq_ast_ctn(&mut it, "d"); |
60 |
| - assert_eq_lit(&mut it, "/e/f"); |
| 173 | + if let ast::Expr::PathSearch(p) = expr { |
| 174 | + let path = Path::cast(p.syntax().clone()).unwrap(); |
| 175 | + |
| 176 | + let parts = path.parts(); |
| 177 | + assert_eq!(parts.len(), 1); |
| 178 | + |
| 179 | + match &parts[0] { |
| 180 | + InterpolPart::Literal(content) => { |
| 181 | + assert_eq!(content.text(), "<nixpkgs>"); |
| 182 | + } |
| 183 | + _ => panic!("Expected literal part"), |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + #[test] |
| 189 | + fn direct_method_usage() { |
| 190 | + // Test direct parts() method on PathAbs |
| 191 | + let inp = "/foo/bar"; |
| 192 | + let expr = Root::parse(inp).ok().unwrap().expr().unwrap(); |
| 193 | + if let ast::Expr::PathAbs(p) = expr { |
| 194 | + let parts = p.parts(); |
| 195 | + assert_eq!(parts.len(), 1); |
| 196 | + |
| 197 | + match &parts[0] { |
| 198 | + InterpolPart::Literal(content) => { |
| 199 | + assert_eq!(content.text(), "/foo/bar"); |
| 200 | + } |
| 201 | + _ => panic!("Expected literal part"), |
61 | 202 | }
|
62 |
| - _ => unreachable!(), |
| 203 | + } |
| 204 | + |
| 205 | + // Test direct content() method on PathSearch |
| 206 | + let inp = "<nixpkgs>"; |
| 207 | + let expr = Root::parse(inp).ok().unwrap().expr().unwrap(); |
| 208 | + if let ast::Expr::PathSearch(p) = expr { |
| 209 | + let content = p.content().expect("Expected content"); |
| 210 | + assert_eq!(content.text(), "<nixpkgs>"); |
63 | 211 | }
|
64 | 212 | }
|
65 | 213 | }
|
0 commit comments