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

View File

@ -22,7 +22,7 @@ pub struct PersistentState {
impl PersistentState {
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) {
@ -65,8 +65,8 @@ impl PersistentState {
pub fn read_from_file(path: &Path) -> anyhow::Result<Self> {
File::open(path)
.context("failed to open persistent state file")
.map(|r| BufReader::new(r))
.and_then(|r| Self::read_from(r))
.map(BufReader::new)
.and_then(Self::read_from)
}
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<()> {
File::create(path)
.context("failed to create persistent state file")
.map(|w| BufWriter::new(w))
.map(BufWriter::new)
.and_then(|w| self.save_to(w))
}

View File

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