]> git.lizzy.rs Git - rust.git/blob - crates/ide_ssr/src/fragments.rs
remove Item::parse
[rust.git] / crates / ide_ssr / src / fragments.rs
1 //! When specifying SSR rule, you generally want to map one *kind* of thing to
2 //! the same kind of thing: path to path, expression to expression, type to
3 //! type.
4 //!
5 //! The problem is, while this *kind* is generally obvious to the human, the ide
6 //! needs to determine it somehow. We do this in a stupid way -- by pasting SSR
7 //! rule into different contexts and checking what works.
8
9 use parser::SyntaxKind;
10 use syntax::{ast, AstNode, SyntaxNode};
11
12 pub(crate) fn ty(s: &str) -> Result<SyntaxNode, ()> {
13     let template = "type T = {};";
14     let input = template.replace("{}", s);
15     let parse = syntax::SourceFile::parse(&input);
16     if !parse.errors().is_empty() {
17         return Err(());
18     }
19     let node = parse.tree().syntax().descendants().find_map(ast::Type::cast).ok_or(())?;
20     Ok(node.syntax().clone())
21 }
22
23 pub(crate) fn item(s: &str) -> Result<SyntaxNode, ()> {
24     let template = "{}";
25     let input = template.replace("{}", s);
26     let parse = syntax::SourceFile::parse(&input);
27     if !parse.errors().is_empty() {
28         return Err(());
29     }
30     let node = parse.tree().syntax().descendants().find_map(ast::Item::cast).ok_or(())?;
31     Ok(node.syntax().clone())
32 }