0
0
mirror of https://github.com/TeFiLeDo/listload.git synced 2024-11-23 20:56:17 +01:00

apply clippy suggestions

This commit is contained in:
Adrian Wannenmacher 2023-06-28 00:45:22 +02:00
parent 59b3885fe5
commit 2f7824e08e
Signed by: tfld
GPG Key ID: 19D986ECB1E492D5
3 changed files with 42 additions and 38 deletions

View File

@ -18,7 +18,7 @@ static USER_DIRS: OnceLock<UserDirs> = OnceLock::new();
static PROJ_DIRS: OnceLock<ProjectDirs> = OnceLock::new(); static PROJ_DIRS: OnceLock<ProjectDirs> = OnceLock::new();
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let cli = CLI::parse(); let cli = Cli::parse();
// initialize dirs // initialize dirs
let user_dirs = UserDirs::new().context("failed to discover user directiories")?; let user_dirs = UserDirs::new().context("failed to discover user directiories")?;
@ -31,7 +31,7 @@ fn main() -> anyhow::Result<()> {
.context("failed to initialize user directories")?; .context("failed to initialize user directories")?;
let proj_dirs = PROJ_DIRS.get_or_init(|| proj_dirs); let proj_dirs = PROJ_DIRS.get_or_init(|| proj_dirs);
if let CMD::License = cli.command { if let Cmd::License = cli.cmd {
println!("{}", include_str!("../LICENSE")); println!("{}", include_str!("../LICENSE"));
return Ok(()); return Ok(());
} }
@ -42,35 +42,38 @@ fn main() -> anyhow::Result<()> {
let mut persistent = let mut persistent =
PersistentState::read_from_default_file().context("failed to load persistent state")?; PersistentState::read_from_default_file().context("failed to load persistent state")?;
match cli.command { match cli.cmd {
CMD::Config => { Cmd::Config => {
println!("{cfg}"); println!("{cfg}");
} }
CMD::PersistentState => { Cmd::PersistentState => {
println!("{persistent}"); println!("{persistent}");
} }
CMD::Download { name } => { Cmd::Download { name } => {
let mut cache = proj_dirs.cache_dir().to_path_buf(); let mut cache = proj_dirs.cache_dir().to_path_buf();
cache.push(&format!("{:0>16x}", rand::random::<u64>())); cache.push(&format!("{:0>16x}", rand::random::<u64>()));
fs::create_dir_all(&cache).context("failed to create cache dir")?; fs::create_dir_all(&cache).context("failed to create cache dir")?;
let name = name let name = name
.as_ref() .as_deref()
.map(|n| n.as_str())
.or(persistent.list()) .or(persistent.list())
.context("no list specified or selected")?; .context("no list specified or selected")?;
let list = TargetList::load(&name).context("failed to load list")?; let list = TargetList::load(name).context("failed to load list")?;
let mut downloads = list.downloads(); let mut downloads = list.downloads();
let mut mapping = HashMap::with_capacity(downloads.len()); let mut mapping: HashMap<_, _> = downloads
let mut counter = 0; .iter_mut()
for d in &mut downloads { .enumerate()
let mut cache_path = cache.clone(); .map(|(counter, value)| {
cache_path.push(format!("{counter:0>16x}")); let mut cache_path = cache.clone();
let prev = std::mem::replace(&mut d.file_name, cache_path.clone()); cache_path.push(format!("{counter:0>16x}"));
mapping.insert(cache_path, prev); (cache_path, value)
counter += 1; })
} .map(|(cache, down)| {
let target = std::mem::replace(&mut down.file_name, cache.clone());
(cache, target)
})
.collect();
let results = downloader let results = downloader
.download(&downloads) .download(&downloads)
@ -124,7 +127,7 @@ fn main() -> anyhow::Result<()> {
fs::remove_dir(cache).context("failed to delete cache directory")?; fs::remove_dir(cache).context("failed to delete cache directory")?;
} }
CMD::List { cmd } => match cmd { Cmd::List { cmd } => match cmd {
ListCommand::Create { ListCommand::Create {
name, name,
keep_current_selected: keep_current_active, keep_current_selected: keep_current_active,
@ -133,7 +136,7 @@ fn main() -> anyhow::Result<()> {
if TargetList::exists(&name) { if TargetList::exists(&name) {
eprintln!("list already exists"); eprintln!("list already exists");
} else { } else {
TargetList::new(&name, comment.as_ref().map(|c| c.as_str())) TargetList::new(&name, comment.as_deref())
.context("failed to create target list")?; .context("failed to create target list")?;
} }
@ -151,9 +154,9 @@ fn main() -> anyhow::Result<()> {
} }
} }
}, },
CMD::Target { cmd } => { Cmd::Target { cmd } => {
let list = persistent.list().context("no list selected")?; let list = persistent.list().context("no list selected")?;
let mut list = TargetList::load(&list).context("failed to load list")?; let mut list = TargetList::load(list).context("failed to load list")?;
match cmd { match cmd {
TargetCommand::Create { TargetCommand::Create {
@ -162,8 +165,8 @@ fn main() -> anyhow::Result<()> {
comment, comment,
keep_current_selected, keep_current_selected,
} => { } => {
let target = Target::new(url, &file, comment.as_ref().map(|c| c.as_str())) let target =
.context("invalid target")?; Target::new(url, &file, comment.as_deref()).context("invalid target")?;
list.add_target(target); list.add_target(target);
if !keep_current_selected { if !keep_current_selected {
@ -181,7 +184,7 @@ fn main() -> anyhow::Result<()> {
list.save().context("failed to save list")?; list.save().context("failed to save list")?;
} }
CMD::License => { Cmd::License => {
panic!("late command"); panic!("late command");
} }
} }
@ -191,13 +194,13 @@ fn main() -> anyhow::Result<()> {
#[derive(Parser)] #[derive(Parser)]
#[clap(about, author, version)] #[clap(about, author, version)]
struct CLI { struct Cli {
#[clap(subcommand)] #[clap(subcommand)]
command: CMD, cmd: Cmd,
} }
#[derive(Subcommand)] #[derive(Subcommand)]
enum CMD { enum Cmd {
/// Print the current configuration. /// Print the current configuration.
Config, Config,
/// Print the EUPL 1.2, under which this program is licensed. /// Print the EUPL 1.2, under which this program is licensed.

View File

@ -22,7 +22,7 @@ pub struct PersistentState {
impl PersistentState { impl PersistentState {
pub fn list(&self) -> Option<&str> { pub fn list(&self) -> Option<&str> {
self.list.as_ref().map(|l| l.as_str()) self.list.as_deref()
} }
pub fn set_list(&mut self, list: &str) { pub fn set_list(&mut self, list: &str) {
@ -65,8 +65,8 @@ impl PersistentState {
pub fn read_from_file(path: &Path) -> anyhow::Result<Self> { pub fn read_from_file(path: &Path) -> anyhow::Result<Self> {
File::open(path) File::open(path)
.context("failed to open persistent state file") .context("failed to open persistent state file")
.map(|r| BufReader::new(r)) .map(BufReader::new)
.and_then(|r| Self::read_from(r)) .and_then(Self::read_from)
} }
pub fn read_from(reader: impl Read) -> anyhow::Result<Self> { pub fn read_from(reader: impl Read) -> anyhow::Result<Self> {
@ -98,7 +98,7 @@ impl PersistentState {
pub fn save_to_file(&self, path: &Path) -> anyhow::Result<()> { pub fn save_to_file(&self, path: &Path) -> anyhow::Result<()> {
File::create(path) File::create(path)
.context("failed to create persistent state file") .context("failed to create persistent state file")
.map(|w| BufWriter::new(w)) .map(BufWriter::new)
.and_then(|w| self.save_to(w)) .and_then(|w| self.save_to(w))
} }

View File

@ -50,19 +50,20 @@ impl Display for Target {
} }
} }
impl Into<Download> for &Target { impl From<&Target> for Download {
fn into(self) -> Download { fn from(value: &Target) -> Self {
match self.urls.len() { match value.urls.len() {
0 => panic!("target without url"), 0 => panic!("target without url"),
1 => Download::new(self.urls[0].as_str()), 1 => Download::new(value.urls[0].as_str()),
_ => Download::new_mirrored( _ => Download::new_mirrored(
self.urls value
.urls
.iter() .iter()
.map(|u| u.as_str()) .map(|u| u.as_str())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.as_ref(), .as_ref(),
), ),
} }
.file_name(&self.file) .file_name(&value.file)
} }
} }