cargo_sync_rdme/
lib.rs

1//! Cargo subcommand to synchronize README with the cargo manifest and crate
2//! documentation.
3//!
4//! See [repository's README] for `cargo-sync-rdme` command usage.
5//!
6//! # Usage
7//!
8//! Add this to your `Cargo.toml`:
9//!
10//! ```toml
11//! [dependencies]
12//! cargo-sync-rdme = "0.4.3"
13//! ```
14//!
15//! [repository's README]: https://github.com/gifnksm/cargo-sync-rdme/blob/main/README.md
16#![doc(html_root_url = "https://docs.rs/cargo-sync-rdme/0.4.3")]
17#![warn(
18    elided_lifetimes_in_paths,
19    explicit_outlives_requirements,
20    keyword_idents,
21    missing_copy_implementations,
22    missing_debug_implementations,
23    missing_docs,
24    single_use_lifetimes,
25    unreachable_pub,
26    unused
27)]
28
29use std::{env, io};
30
31use clap::Parser;
32use tracing::Level;
33use tracing_subscriber::EnvFilter;
34
35#[macro_use]
36mod macros;
37
38mod cli;
39mod config;
40mod diff;
41mod sync;
42mod traits;
43mod vcs;
44mod with_source;
45
46pub use self::cli::App;
47
48/// Error type for `cargo-sync-rdme` command.
49pub type Error = miette::Error;
50
51/// Result type for `cargo-sync-rdme` command.
52pub type Result<T> = miette::Result<T>;
53
54/// Entry point of `cargo-sync-rdme` command.
55pub fn main() -> Result<()> {
56    // If this command is run by cargo, the first argument is the subcommand name
57    // `sync-rdme`. We need to remove it to avoid parsing error.
58    let args = env::args().enumerate().filter_map(|(idx, arg)| {
59        if idx == 1 && arg == "sync-rdme" {
60            None
61        } else {
62            Some(arg)
63        }
64    });
65    let app = App::parse_from(args);
66    install_logger(app.verbosity.into())?;
67
68    let workspace = app.workspace.metadata()?;
69    for package in app.package.packages(&workspace)? {
70        sync::sync_all(&app, &workspace, package)?;
71    }
72
73    Ok(())
74}
75
76fn install_logger(verbosity: Option<Level>) -> Result<()> {
77    if env::var_os("RUST_LOG").is_none() {
78        match verbosity {
79            Some(Level::ERROR) => env::set_var("RUST_LOG", "error"),
80            Some(Level::WARN) => env::set_var("RUST_LOG", "warn"),
81            Some(Level::INFO) => env::set_var("RUST_LOG", "info"),
82            Some(Level::DEBUG) => env::set_var("RUST_LOG", "debug"),
83            Some(Level::TRACE) => env::set_var("RUST_LOG", "trace"),
84            None => env::set_var("RUST_LOG", "off"),
85        }
86    }
87
88    tracing_subscriber::fmt()
89        .with_env_filter(EnvFilter::from_default_env())
90        .with_writer(io::stderr)
91        .with_target(false)
92        .try_init()
93        .map_err(|e| miette!(e))?;
94
95    Ok(())
96}