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
//! A library and application for flashing Espressif devices over Serial
//!
//! ## As an application
//!
//! [espflash] can be installed using `cargo install`, and additionally supports installation via [cargo-binstall]:
//!
//! ```bash
//! $ cargo install espflash
//! $ cargo binstall espflash
//! ```
//!
//! Flashing via a Raspberry Pi's internal UART is also possible, however this
//! functionality is gated behind the `raspberry` feature; if you would like to
//! enable this simply enable the feature when installing:
//!
//! ```bash
//! $ cargo install espflash --feature=raspberry
//! ```
//!
//! Note that this feature can only be enabled on a Raspberry Pi, as it depends
//! on the [rppal] package which will not build on most systems.
//!
//! ## As a library
//!
//! [espflash] can also be used as a library:
//!
//! ```toml
//! espflash = { version = "2.0", default-features = false }
//! ```
//!
//! We add `default-features` here to disable the `cli` feature, which is
//! enabled by default; you likely will not need any of these types or functions
//! in your application so there's no use pulling in the extra dependencies.
//!
//! Just like when using [espflash] as an application, you can enable the
//! `raspberry` feature to allow your dependent application to use the Raspberry
//! Pi's built-in UART:
//!
//! ```toml
//! espflash = { version = "2.0", default-features = false, features = ["raspberry"] }
//! ```
//!
//! [espflash]: https://crates.io/crates/espflash
//! [cargo-binstall]: https://github.com/cargo-bins/cargo-binstall
//! [rppal]: https://docs.rs/rppal/latest/rppal/

#![cfg_attr(docsrs, feature(doc_cfg))]

#[cfg(feature = "cli")]
#[cfg_attr(docsrs, doc(cfg(feature = "cli")))]
pub mod cli;
pub mod command;
pub mod connection;
pub mod elf;
pub mod error;
pub mod flasher;
pub mod image_format;
pub mod interface;
pub mod targets;

/// Logging utilities
#[cfg(feature = "cli")]
#[cfg_attr(docsrs, doc(cfg(feature = "cli")))]
pub mod logging {
    use env_logger::Env;
    use log::LevelFilter;

    /// Initialize the logger with the given [LevelFilter]
    pub fn initialize_logger(filter: LevelFilter) {
        env_logger::Builder::from_env(Env::default().default_filter_or(filter.as_str()))
            .format_target(false)
            .init();
    }
}

/// Check for updates
#[cfg(feature = "cli")]
#[cfg_attr(docsrs, doc(cfg(feature = "cli")))]
pub mod update {
    use std::time::Duration;

    use log::info;
    use update_informer::{registry, Check};

    /// Check crates.io for a new version of the application
    pub fn check_for_update(name: &str, version: &str) {
        // By setting the interval to 0 seconds we invalidate the cache with each
        // invocation and ensure we're getting up-to-date results
        let informer =
            update_informer::new(registry::Crates, name, version).interval(Duration::from_secs(0));

        if let Some(version) = informer.check_version().ok().flatten() {
            info!("🚀 A new version of {name} is available: {version}");
        }
    }
}