1use std::{io, path::PathBuf};
50
51use thiserror::Error;
52
53pub(crate) mod customization;
54mod flashing;
55mod helpers;
56pub(crate) mod pal;
57
58pub use customization::Customization;
59pub use flashing::flash;
60
61pub(crate) type Result<T, E = Error> = std::result::Result<T, E>;
62
63#[derive(Error, Debug)]
64pub enum Error {
66 #[error("Failed to customize flashed image {0}")]
67 Customization(String),
68 #[error("IO Error: {0}")]
69 IoError(#[from] io::Error),
70 #[error("Aborted before completing")]
72 Aborted,
73 #[error("Failed to format SD Card: {0}")]
74 FailedToFormat(String),
75 #[error("Failed to open {0}")]
76 FailedToOpenDestination(String),
77
78 #[error("Udisks2 Error: {0}")]
79 #[cfg(all(feature = "udev", target_os = "linux"))]
80 Udisks(#[from] udisks2::Error),
81
82 #[cfg(windows)]
83 #[error("Drive path is not valid")]
84 InvalidDrive,
85 #[cfg(windows)]
86 #[error("Failed to find the drive {0}")]
87 DriveNotFound(String),
88 #[cfg(windows)]
89 #[error("Windows Error: {0}")]
90 WindowsError(#[from] windows::core::Error),
91}
92
93pub fn devices() -> std::collections::HashSet<Device> {
95 bb_drivelist::drive_list()
96 .expect("Unsupported OS for Sd Card")
97 .into_iter()
98 .filter(|x| x.is_removable)
99 .filter(|x| !x.is_virtual)
100 .map(|x| Device::new(x.description, x.raw.into(), x.size))
101 .collect()
102}
103
104#[derive(Hash, Debug, PartialEq, Eq, Clone)]
105pub struct Device {
107 pub name: String,
108 pub path: PathBuf,
109 pub size: u64,
110}
111
112impl Device {
113 const fn new(name: String, path: PathBuf, size: u64) -> Self {
114 Self { name, path, size }
115 }
116}
117
118pub fn format(dst: &std::path::Path) -> Result<()> {
120 crate::pal::format(dst)
121}