espflash/lib.rs
1//! A library and application for flashing Espressif devices over Serial
2//!
3//! ## As an application
4//!
5//! [espflash] can be installed using `cargo install`, and additionally supports installation via [cargo-binstall]:
6//!
7//! ```bash
8//! $ cargo install espflash
9//! $ cargo binstall espflash
10//! ```
11//!
12//! ## As a library
13//!
14//! [espflash] can also be used as a library:
15//!
16//! ```toml
17//! espflash = { version = "4.1.0", default-features = false }
18//! ```
19//!
20//! We add `default-features` here to disable the `cli` feature, which is
21//! enabled by default. It's important to note that the `cli` module does not
22//! provide SemVer guarantees. You likely will not need any of these types or
23//! functions in your application so there's no use pulling in the extra
24//! dependencies.
25//!
26//! [espflash]: https://crates.io/crates/espflash
27//! [cargo-binstall]: https://github.com/cargo-bins/cargo-binstall
28
29#![cfg_attr(docsrs, feature(doc_cfg))]
30#![deny(missing_debug_implementations, missing_docs, rust_2018_idioms)]
31
32pub use self::error::Error;
33
34pub mod command;
35#[cfg(feature = "serialport")]
36#[cfg_attr(docsrs, doc(cfg(feature = "serialport")))]
37pub mod connection;
38pub mod flasher;
39pub mod image_format;
40pub mod target;
41
42mod error;
43
44// Command-line interface
45#[cfg(feature = "cli")]
46pub mod cli;
47
48/// Logging utilities
49#[cfg(feature = "cli")]
50pub mod logging {
51 use env_logger::{Builder, Env};
52 use log::LevelFilter;
53
54 /// Initialize the logger with the given [LevelFilter]
55 pub fn initialize_logger(filter: LevelFilter) {
56 Builder::from_env(Env::default().default_filter_or(filter.as_str()))
57 .format_target(false)
58 .init();
59 }
60}
61
62/// Check for updates
63#[cfg(feature = "cli")]
64pub mod update {
65 use std::time::Duration;
66
67 use log::info;
68 use update_informer::{Check, registry::Crates};
69
70 /// Check for updates to the espflash crate.
71 pub fn check_for_update(name: &str, version: &str) {
72 // By setting the interval to 0 seconds we invalidate the cache with each
73 // invocation and ensure we're getting up-to-date results
74 let informer = update_informer::new(Crates, name, version).interval(Duration::from_secs(0));
75
76 if let Some(version) = informer.check_version().ok().flatten() {
77 info!("🚀 A new version of {name} is available: {version}");
78 }
79 }
80}