1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! # ryo-source
//!
//! High-speed Rust AST manipulation engine for the RYO (流) project's "Fluid Code" paradigm.
//!
//! This crate provides two complementary AST representations optimized for different use cases:
//! [`RustAST`] for full-fidelity manipulation and [`PureFile`] for thread-safe parallel processing.
//!
//! # Core Philosophy: Fluid Code
//!
//! Code is not static text—it's a living, flowing structure that can be manipulated,
//! analyzed, and transformed at high speed. The AST is the "shadow" of source code,
//! a pure representation that enables:
//!
//! - **Instant Analysis**: Query definitions, references, and dependencies in nanoseconds
//! - **Safe Mutation**: Transform code with type-safe operations
//! - **Parallel Processing**: Share and analyze code across threads via [`PureFile`]
//!
//! # Dual AST Architecture
//!
//! ```text
//! Source Code
//! "fn main() { ... }"
//! │
//! ▼
//! syn::parse
//! │
//! ┌───────────────┴───────────────┐
//! ▼ ▼
//! ┌─────────────────┐ ┌─────────────────┐
//! │ RustAST │ │ PureFile │
//! │ (Full-fidelity)│ │ (Thread-safe) │
//! ├─────────────────┤ ├─────────────────┤
//! │ • syn::File │ │ • Span-free │
//! │ • Complete info │ ToPure │ • Send + Sync │
//! │ • Span preserve │ ───────▶ │ • Arc-shareable │
//! │ • Code gen │ (~10%) │ • Serializable │
//! │ • NOT Send/Sync │ │ • Lightweight │
//! └─────────────────┘ └─────────────────┘
//! ```
//!
//! # RustAST: Full-Fidelity AST
//!
//! [`RustAST`] wraps `syn::File` to provide high-level operations while preserving
//! complete syntactic information including spans (source locations).
//!
//! ```
//! use ryo_source::RustAST;
//!
//! // Parse source code
//! let mut ast = RustAST::parse("use std::io;\nfn main() {}").unwrap();
//!
//! // Remove unused imports
//! let removed = ast.remove_unused_imports();
//! assert_eq!(removed.len(), 1);
//!
//! // Generate cleaned code
//! let code = ast.to_string_pretty();
//! assert!(!code.contains("use std::io"));
//! ```
//!
//! # PureFile: Thread-Safe AST
//!
//! [`PureFile`] strips all `Span` information, creating a pure data structure
//! that can be safely shared across threads with `Arc`.
//!
//! ```
//! use std::sync::Arc;
//! use std::thread;
//! use ryo_source::PureFile;
//!
//! // Parse directly to PureFile
//! let pure = PureFile::from_source("fn foo() {} fn bar() {}").unwrap();
//!
//! // Share across threads
//! let shared = Arc::new(pure);
//!
//! let s1 = Arc::clone(&shared);
//! let h1 = thread::spawn(move || s1.functions().len());
//!
//! let s2 = Arc::clone(&shared);
//! let h2 = thread::spawn(move || s2.find_fn("foo").is_some());
//!
//! assert_eq!(h1.join().unwrap(), 2);
//! assert!(h2.join().unwrap());
//! ```
//!
//! # Operations
//!
//! ## Query Operations (Read-only)
//!
//! | Operation | Description |
//! |-----------|-------------|
//! | [`DefRefs::analyze`] | Find all symbol definitions and references |
//! | [`DefRefs::find_definition`] | Locate where a symbol is defined |
//! | [`DefRefs::find_references`] | Find all uses of a symbol |
//! | [`PureFile::functions`] | Get all functions (thread-safe) |
//! | [`PureFile::find_fn`] | Find function by name (thread-safe) |
//!
//! ## Mutation Operations
//!
//! | Operation | Description | Pattern |
//! |-----------|-------------|---------|
//! | [`Rename::apply`] | Rename symbols globally | In-place |
//! | [`Rename::rename_local_in_fn`] | Rename within specific function | In-place |
//! | [`RemoveUnusedImports::apply`] | Remove unused imports | In-place |
//! | [`CowMut::rename`] | Copy-on-write rename | Returns new AST |
//! | [`CowMut::chain_renames`] | Sequential renames | Returns new AST |
//!
//! # Copy-on-Write Mutation
//!
//! For scenarios requiring multiple independent mutations:
//!
//! ```
//! use ryo_source::{RustAST, CowMut};
//!
//! let ast = RustAST::parse("fn foo() { fn bar() {} }").unwrap();
//!
//! // Each rename produces an independent copy
//! let results = CowMut::multi_rename(&ast, &[
//! ("foo", "alpha"),
//! ("bar", "beta"),
//! ]);
//!
//! // results[0].ast has foo→alpha, bar unchanged
//! // results[1].ast has bar→beta, foo unchanged
//! // Original ast is unchanged
//! assert!(ast.to_string().contains("foo"));
//! ```
//!
//! # Why syn over tree-sitter?
//!
//! We chose `syn` for Rust because:
//!
//! 1. **Semantic Awareness**: syn understands Rust's semantics, not just syntax
//! 2. **Type Safety**: Operations are checked at compile time
//! 3. **Ecosystem Integration**: Works with `quote`, `proc-macro2`, and Rust tooling
//! 4. **Rust-First Philosophy**: Optimized for Rust's unique features
//!
//! [`RustAST`]: crate::RustAST
//! [`PureFile`]: crate::PureFile
//! [`DefRefs::analyze`]: crate::DefRefs::analyze
//! [`DefRefs::find_definition`]: crate::DefRefs::find_definition
//! [`DefRefs::find_references`]: crate::DefRefs::find_references
//! [`PureFile::functions`]: crate::pure::PureFile::functions
//! [`PureFile::find_fn`]: crate::pure::PureFile::find_fn
//! [`Rename::apply`]: crate::Rename::apply
//! [`Rename::rename_local_in_fn`]: crate::Rename::rename_local_in_fn
//! [`RemoveUnusedImports::apply`]: crate::RemoveUnusedImports::apply
//! [`CowMut::rename`]: crate::CowMut::rename
//! [`CowMut::chain_renames`]: crate::CowMut::chain_renames
// Test utilities (available with `test-utils` feature or in test builds)
pub use RustAST;
pub use ;
pub use ;
pub use ;
pub use ;
pub use pure::;