Skip to content

Commit

Permalink
refactor: use fs::write instead of File::create + f.write_all
Browse files Browse the repository at this point in the history
  • Loading branch information
null8626 committed Sep 23, 2024
1 parent bf6eddb commit 3066204
Show file tree
Hide file tree
Showing 33 changed files with 176 additions and 187 deletions.
4 changes: 4 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
edition = "2021"
match_block_trailing_comma = true
newline_style = "Unix"
use_field_init_shorthand = true
14 changes: 7 additions & 7 deletions src/app/downloading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ impl App {
)? {
0 => {
tokio::fs::remove_dir_all(&file_path).await?;
}
},
1 => {
self.notify(Prefix::SkippedWarning, progress_bar.message());
return Ok(resolved);
}
},
2 => bail!(message),
_ => unreachable!(),
}
Expand Down Expand Up @@ -208,10 +208,10 @@ impl App {
cached.to_string_lossy()
));
None
}
},
_ => Some((cached, cached_size)),
}
}
},
_ => None,
} {
progress_bar.disable_steady_tick();
Expand Down Expand Up @@ -276,9 +276,9 @@ impl App {
}

progress_bar.set_length(len);
}
},
(Some(size), None) | (None, Some(size)) => progress_bar.set_length(size),
_ => {}
_ => {},
}

progress_bar.disable_steady_tick();
Expand Down Expand Up @@ -318,7 +318,7 @@ impl App {
if let Some(cached_file_path) = match &resolved.cache {
CacheStrategy::File { namespace, path } => {
self.get_cache(namespace).map(|c| c.path(path))
}
},
CacheStrategy::Indexed { .. } => todo!(),
CacheStrategy::None => None,
} {
Expand Down
30 changes: 15 additions & 15 deletions src/app/from_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ impl App {
id: id.to_owned(),
version: version.to_owned(),
})
}
},
("cr" | "cf" | "curseforge" | "curserinth", id) => {
let (id, version) = id.split_once(',').unwrap_or((id, "latest"));
Ok(Downloadable::CurseRinth {
id: id.to_owned(),
version: version.to_owned(),
})
}
},
("hangar" | "h", id) => {
let (id, version) = id.split_once(',').unwrap_or((id, "latest"));
Ok(Downloadable::Hangar {
id: id.to_owned(),
version: version.to_owned(),
})
}
},
("spigot" | "spiget", id) => {
let (id, version) = id.split_once(',').unwrap_or((id, "latest"));
Ok(Downloadable::Spigot {
id: id.to_owned(),
version: version.to_owned(),
})
}
},
("ghrel" | "gh" | "github", id) => {
let (repo, tag) = id.split_once(',').unwrap_or((id, "latest"));

Expand All @@ -51,7 +51,7 @@ impl App {
tag: tag.to_owned(),
asset: "first".to_owned(),
})
}
},
(ty, _) => bail!("Unknown identifier '{ty}'"),
}
} else {
Expand All @@ -76,7 +76,7 @@ impl App {
id: id.to_owned().to_owned(),
version: version.to_owned().to_owned(),
})
}
},

(Some("modrinth.com"), ["mod" | "plugin" | "datapack", id, rest @ ..]) => {
let version = if let ["version", v] = rest {
Expand Down Expand Up @@ -112,7 +112,7 @@ impl App {
id: id.to_owned().to_owned(),
version: version.clone(),
})
}
},

(Some("curserinth.kuylar.dev"), ["mod", id, rest @ ..]) => {
let version = if let ["version", v] = rest {
Expand Down Expand Up @@ -148,7 +148,7 @@ impl App {
id: (*id).to_string(),
version,
})
}
},

// https://www.curseforge.com/minecraft/mc-mods/betterwithpatches
(Some("www.curseforge.com"), ["minecraft", "mc-mods", id, rest @ ..]) => {
Expand Down Expand Up @@ -184,7 +184,7 @@ impl App {
};

Ok(Downloadable::CurseRinth { id, version })
}
},

// https://www.spigotmc.org/resources/http-requests.101253/
(Some("www.spigotmc.org"), ["resources", id]) => Ok(Downloadable::Spigot {
Expand Down Expand Up @@ -266,7 +266,7 @@ impl App {
tag: tag.to_string(),
asset,
})
}
},

(domain, path) => {
let def = match domain {
Expand All @@ -278,7 +278,7 @@ impl App {
} else {
0
}
}
},
};

let selection = self.select_with_default(
Expand Down Expand Up @@ -310,7 +310,7 @@ impl App {
filename: Some(input),
desc: if desc.is_empty() { None } else { Some(desc) },
})
}
},
1 => {
// TODO: ...
let j_url = if self.confirm(&format!(
Expand Down Expand Up @@ -359,7 +359,7 @@ impl App {
build,
artifact,
})
}
},
2 => {
let mut repo = None;
let mut group_id = None;
Expand Down Expand Up @@ -464,10 +464,10 @@ impl App {
version,
filename,
})
}
},
_ => unreachable!(),
}
}
},
}
}
}
4 changes: 2 additions & 2 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl App {
}
}
list
}
},
AddonType::Mod => {
let mut list: Vec<Downloadable> = self.server.mods.clone();
if let Some(nw) = &self.network {
Expand All @@ -173,7 +173,7 @@ impl App {
}
}
list
}
},
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/commands/add/modrinth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub async fn run(mut app: App, args: Args) -> Result<()> {
} {
"modpack" => {
todo!("Modpack importing currently unsupported")
}
},
"mod" => {
app.add_addon_inferred(Downloadable::Modrinth {
id: project.slug.clone(),
Expand All @@ -104,7 +104,7 @@ pub async fn run(mut app: App, args: Args) -> Result<()> {
app.save_changes()?;
app.notify(Prefix::Imported, format!("{} from modrinth", project.title));
app.refresh_markdown().await?;
}
},
"datapack" => {
app.add_datapack(Downloadable::Modrinth {
id: project.slug.clone(),
Expand All @@ -113,7 +113,7 @@ pub async fn run(mut app: App, args: Args) -> Result<()> {

app.save_changes()?;
app.refresh_markdown().await?;
}
},
ty => bail!("Unsupported modrinth project type: '{ty}'"),
}

Expand Down
10 changes: 5 additions & 5 deletions src/commands/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn run(commands: Commands) -> Result<()> {
match commands {
Commands::Path => {
println!("{}", cache_folder.to_string_lossy());
}
},

Commands::List { detailed } => {
println!(
Expand Down Expand Up @@ -65,11 +65,11 @@ pub fn run(commands: Commands) -> Result<()> {
}

println!(" {all} entries in {namespaces} namespaces in total");
}
},

Commands::Open => {
opener::open(cache_folder).context("Opening cache folder")?;
}
},

Commands::Clear => {
let pb = ProgressBar::new_spinner()
Expand All @@ -89,11 +89,11 @@ pub fn run(commands: Commands) -> Result<()> {

pb.finish_and_clear();
println!(" Cache has been cleared");
}
},
}

Ok(())
}
},

None => bail!("Cache directory was missing, maybe it's disabled?"),
}
Expand Down
10 changes: 6 additions & 4 deletions src/commands/env/workflow_packwiz.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{io::Write, path::Path};
use std::{fs, path::Path};

use anyhow::Result;
use std::fs::File;

use crate::{app::App, util::env::get_git_root};

Expand All @@ -10,8 +9,11 @@ pub fn run(app: &App) -> Result<()> {
.join(".github")
.join("workflows");

let mut f = File::create(path.join("packwiz.yml"))?;
f.write_all(include_bytes!("../../../res/workflows/packwiz.yml"))?;
fs::write(
path.join("packwiz.yml"),
include_bytes!("../../../res/workflows/packwiz.yml"),
)?;

app.success("packwiz.yml workflow created");

Ok(())
Expand Down
10 changes: 6 additions & 4 deletions src/commands/env/workflow_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{io::Write, path::Path};
use std::{fs, path::Path};

use anyhow::Result;
use std::fs::File;

use crate::{app::App, util::env::get_git_root};

Expand All @@ -10,8 +9,11 @@ pub fn run(app: &App) -> Result<()> {
.join(".github")
.join("workflows");

let mut f = File::create(path.join("test.yml"))?;
f.write_all(include_bytes!("../../../res/workflows/test.yml"))?;
fs::write(
path.join("test.yml"),
include_bytes!("../../../res/workflows/test.yml"),
)?;

app.success("test.yml workflow created");

Ok(())
Expand Down
Loading

0 comments on commit 3066204

Please sign in to comment.