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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
//! Utility to traverse the file-system and inline modules that are declared as references to
//! other Rust files.
use proc_macro2::Span;
use std::{
error, fmt, io,
path::{Path, PathBuf},
};
use syn::spanned::Spanned;
use syn::ItemMod;
mod mod_path;
mod resolver;
mod visitor;
pub(crate) use mod_path::*;
pub(crate) use resolver::*;
pub(crate) use visitor::Visitor;
/// Parse the source code in `src_file` and return a `syn::File` that has all modules
/// recursively inlined.
///
/// This is equivalent to using an `InlinerBuilder` with the default settings.
///
/// # Panics
///
/// This function will panic if `src_file` cannot be opened or does not contain valid Rust
/// source code.
///
/// # Error Handling
///
/// This function ignores most error cases to return a best-effort result. To be informed of
/// failures that occur while inlining referenced modules, create an `InlinerBuilder` instead.
pub fn parse_and_inline_modules(src_file: &Path) -> syn::File {
InlinerBuilder::default()
.parse_and_inline_modules(src_file)
.unwrap()
.output
}
/// A builder that can configure how to inline modules.
///
/// After creating a builder, set configuration options using the methods
/// taking `&mut self`, then parse and inline one or more files using
/// `parse_and_inline_modules`.
#[derive(Debug)]
pub struct InlinerBuilder {
root: bool,
}
impl Default for InlinerBuilder {
fn default() -> Self {
InlinerBuilder { root: true }
}
}
impl InlinerBuilder {
/// Create a new `InlinerBuilder` with the default options.
pub fn new() -> Self {
Self::default()
}
/// Configures whether the module being parsed is a root module or not.
///
/// A root module is one that is passed directly to `rustc`. A non-root
/// module is one that is included from another module using a `mod` item.
///
/// Default: `true`.
pub fn root(&mut self, root: bool) -> &mut Self {
self.root = root;
self
}
/// Parse the source code in `src_file` and return an `InliningResult` that has all modules
/// recursively inlined.
pub fn parse_and_inline_modules(&self, src_file: &Path) -> Result<InliningResult, Error> {
self.parse_internal(src_file, &mut FsResolver::new(|_: &Path, _| {}))
}
/// Parse the source code in `src_file` and return an `InliningResult` that has all modules
/// recursively inlined. Call the given callback whenever a file is loaded from disk (regardless
/// of if it parsed successfully).
pub fn inline_with_callback(
&self,
src_file: &Path,
on_load: impl FnMut(&Path, String),
) -> Result<InliningResult, Error> {
self.parse_internal(src_file, &mut FsResolver::new(on_load))
}
fn parse_internal<R: FileResolver>(
&self,
src_file: &Path,
resolver: &mut R,
) -> Result<InliningResult, Error> {
// XXX There is no way for library callers to disable error tracking,
// but until we're sure that there's no performance impact of enabling it
// we'll let downstream code think that error tracking is optional.
let mut errors = Some(vec![]);
let result = Visitor::<R>::new(src_file, self.root, errors.as_mut(), resolver).visit()?;
Ok(InliningResult::new(result, errors.unwrap_or_default()))
}
}
/// An error that was encountered while reading, parsing or inlining a module.
///
/// Errors block further progress on inlining, but do not invalidate other progress.
/// Therefore, only an error on the initially-passed-in-file is fatal to inlining.
#[derive(Debug)]
pub enum Error {
/// An error happened while opening or reading the file.
Io(io::Error),
/// Errors happened while using `syn` to parse the file.
Parse(syn::Error),
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::Io(err) => Some(err),
Error::Parse(err) => Some(err),
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err)
}
}
impl From<syn::Error> for Error {
fn from(err: syn::Error) -> Self {
Error::Parse(err)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Io(_) => write!(f, "IO error"),
Error::Parse(_) => write!(f, "parse error"),
}
}
}
/// The result of a best-effort attempt at inlining.
///
/// This struct guarantees that the origin file was readable and valid Rust source code, but
/// `errors` must be inspected to check if everything was inlined successfully.
pub struct InliningResult {
output: syn::File,
errors: Vec<InlineError>,
}
impl InliningResult {
/// Create a new `InliningResult` with the best-effort output and any errors encountered
/// during the inlining process.
pub(crate) fn new(output: syn::File, errors: Vec<InlineError>) -> Self {
InliningResult { output, errors }
}
/// The best-effort result of inlining.
pub fn output(&self) -> &syn::File {
&self.output
}
/// The errors that kept the inlining from completing. May be empty if there were no errors.
pub fn errors(&self) -> &[InlineError] {
&self.errors
}
/// Whether the result has any errors. `false` implies that all inlining operations completed
/// successfully.
pub fn has_errors(&self) -> bool {
!self.errors.is_empty()
}
/// Break an incomplete inlining into the best-effort parsed result and the errors encountered.
///
/// # Usage
///
/// ```rust,ignore
/// # #![allow(unused_variables)]
/// # use std::path::Path;
/// # use syn_inline_mod::InlinerBuilder;
/// let result = InlinerBuilder::default().parse_and_inline_modules(Path::new("foo.rs"));
/// match result {
/// Err(e) => unimplemented!(),
/// Ok(r) if r.has_errors() => {
/// let (best_effort, errors) = r.into_output_and_errors();
/// // do things with the partial output and the errors
/// },
/// Ok(r) => {
/// let (complete, _) = r.into_output_and_errors();
/// // do things with the completed output
/// }
/// }
/// ```
pub fn into_output_and_errors(self) -> (syn::File, Vec<InlineError>) {
(self.output, self.errors)
}
}
impl fmt::Debug for InliningResult {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.errors.fmt(f)
}
}
impl fmt::Display for InliningResult {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "Inlining partially completed before errors:")?;
for error in &self.errors {
writeln!(f, " * {}", error)?;
}
Ok(())
}
}
/// An error that happened while attempting to inline a module.
#[derive(Debug)]
pub struct InlineError {
src_path: PathBuf,
module_name: String,
src_span: Span,
path: PathBuf,
kind: Error,
}
impl InlineError {
pub(crate) fn new(
src_path: impl Into<PathBuf>,
item_mod: &ItemMod,
path: impl Into<PathBuf>,
kind: Error,
) -> Self {
Self {
src_path: src_path.into(),
module_name: item_mod.ident.to_string(),
src_span: item_mod.span(),
path: path.into(),
kind,
}
}
/// Returns the source path where the error originated.
///
/// The file at this path parsed correctly, but it caused the file at `self.path()` to be read.
pub fn src_path(&self) -> &Path {
&self.src_path
}
/// Returns the name of the module that was attempted to be inlined.
pub fn module_name(&self) -> &str {
&self.module_name
}
/// Returns the `Span` (including line and column information) in the source path that caused
/// `self.path()` to be included.
pub fn src_span(&self) -> proc_macro2::Span {
self.src_span
}
/// Returns the path where the error happened.
///
/// Reading and parsing this file failed for the reason listed in `self.kind()`.
pub fn path(&self) -> &Path {
&self.path
}
/// Returns the reason for this error happening.
pub fn kind(&self) -> &Error {
&self.kind
}
}
impl fmt::Display for InlineError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let start = self.src_span.start();
write!(
f,
"{}:{}:{}: error while including {}: {}",
self.src_path.display(),
start.line,
start.column,
self.path.display(),
self.kind
)
}
}
#[cfg(test)]
mod tests {
use quote::{quote, ToTokens};
use super::*;
fn make_test_env() -> TestResolver {
let mut env = TestResolver::default();
env.register("src/lib.rs", "mod first;");
env.register("src/first/mod.rs", "mod second;");
env.register(
"src/first/second.rs",
r#"
#[doc = " Documentation"]
mod third {
mod fourth;
}
pub fn sample() -> usize { 4 }
"#,
);
env.register(
"src/first/second/third/fourth.rs",
"pub fn another_fn() -> bool { true }",
);
env
}
/// Run a full test, exercising the entirety of the functionality in this crate.
#[test]
fn happy_path() {
let result = InlinerBuilder::default()
.parse_internal(Path::new("src/lib.rs"), &mut make_test_env())
.unwrap()
.output;
assert_eq!(
result.into_token_stream().to_string(),
quote! {
mod first {
mod second {
#[doc = " Documentation"]
mod third {
mod fourth {
pub fn another_fn() -> bool {
true
}
}
}
pub fn sample() -> usize {
4
}
}
}
}
.to_string()
);
}
/// Test case involving missing and invalid modules
#[test]
fn missing_module() {
let mut env = TestResolver::default();
env.register("src/lib.rs", "mod missing;\nmod invalid;");
env.register("src/invalid.rs", "this-is-not-valid-rust!");
let result = InlinerBuilder::default().parse_internal(Path::new("src/lib.rs"), &mut env);
if let Ok(r) = result {
let errors = &r.errors;
assert_eq!(errors.len(), 2, "expected 2 errors");
let error = &errors[0];
assert_eq!(
error.src_path(),
Path::new("src/lib.rs"),
"correct source path"
);
assert_eq!(error.module_name(), "missing");
assert_eq!(error.src_span().start().line, 1);
assert_eq!(error.src_span().start().column, 0);
assert_eq!(error.src_span().end().line, 1);
assert_eq!(error.src_span().end().column, 12);
assert_eq!(error.path(), Path::new("src/missing/mod.rs"));
let io_err = match error.kind() {
Error::Io(err) => err,
_ => panic!("expected ErrorKind::Io, found {}", error.kind()),
};
assert_eq!(io_err.kind(), io::ErrorKind::NotFound);
let error = &errors[1];
assert_eq!(
error.src_path(),
Path::new("src/lib.rs"),
"correct source path"
);
assert_eq!(error.module_name(), "invalid");
assert_eq!(error.src_span().start().line, 2);
assert_eq!(error.src_span().start().column, 0);
assert_eq!(error.src_span().end().line, 2);
assert_eq!(error.src_span().end().column, 12);
assert_eq!(error.path(), Path::new("src/invalid.rs"));
match error.kind() {
Error::Parse(_) => {}
Error::Io(_) => panic!("expected ErrorKind::Parse, found {}", error.kind()),
}
} else {
unreachable!();
}
}
/// Test case involving `cfg_attr` from the original request for implementation.
///
/// Right now, this test fails for two reasons:
///
/// 1. We don't look for `cfg_attr` elements
/// 2. We don't have a way to insert new items
///
/// The first fix is simpler, but the second one would be difficult.
#[test]
#[should_panic]
fn cfg_attrs() {
let mut env = TestResolver::default();
env.register(
"src/lib.rs",
r#"
#[cfg(feature = "m1")]
mod m1;
#[cfg_attr(feature = "m2", path = "m2.rs")]
#[cfg_attr(not(feature = "m2"), path = "empty.rs")]
mod placeholder;
"#,
);
env.register("src/m1.rs", "struct M1;");
env.register(
"src/m2.rs",
"
//! module level doc comment
struct M2;
",
);
env.register("src/empty.rs", "");
let result = InlinerBuilder::default()
.parse_internal(Path::new("src/lib.rs"), &mut env)
.unwrap()
.output;
assert_eq!(
result.into_token_stream().to_string(),
quote! {
#[cfg(feature = "m1")]
mod m1 {
struct M1;
}
#[cfg(feature = "m2")]
mod placeholder {
//! module level doc comment
struct M2;
}
#[cfg(not(feature = "m2"))]
mod placeholder {
}
}
.to_string()
)
}
#[test]
fn cfg_attrs_revised() {
let mut env = TestResolver::default();
env.register(
"src/lib.rs",
r#"
#[cfg(feature = "m1")]
mod m1;
#[cfg(feature = "m2")]
#[path = "m2.rs"]
mod placeholder;
#[cfg(not(feature = "m2"))]
#[path = "empty.rs"]
mod placeholder;
"#,
);
env.register("src/m1.rs", "struct M1;");
env.register(
"src/m2.rs",
r#"
#![doc = " module level doc comment"]
struct M2;
"#,
);
env.register("src/empty.rs", "");
let result = InlinerBuilder::default()
.parse_internal(Path::new("src/lib.rs"), &mut env)
.unwrap()
.output;
assert_eq!(
result.into_token_stream().to_string(),
quote! {
#[cfg(feature = "m1")]
mod m1 {
struct M1;
}
#[cfg(feature = "m2")]
#[path = "m2.rs"]
mod placeholder {
#![doc = " module level doc comment"]
struct M2;
}
#[cfg(not(feature = "m2"))]
#[path = "empty.rs"]
mod placeholder {
}
}
.to_string()
)
}
}