From 6c426ddc81bfaf5cf7fb6bf52e2c4a9e18d74830 Mon Sep 17 00:00:00 2001 From: null8626 Date: Tue, 24 Sep 2024 09:29:57 +0700 Subject: [PATCH] refactor: remove unnecessary repeated Ok()s --- src/_archive/app/from_string.rs | 38 +++++++++++------------ src/api/app/cache/mod.rs | 32 +++++++++---------- src/api/models/metadata/addon_metadata.rs | 36 ++++++++++----------- src/api/models/packwiz/mod.rs | 14 ++++----- src/api/models/server/server_type.rs | 10 +++--- src/api/sources/buildtools/mod.rs | 20 ++++++------ src/api/sources/vanilla/mod.rs | 4 +-- src/api/utils/accessor.rs | 20 ++++++------ src/api/utils/update_writer.rs | 14 ++++----- 9 files changed, 92 insertions(+), 96 deletions(-) diff --git a/src/_archive/app/from_string.rs b/src/_archive/app/from_string.rs index 7858617..457b219 100644 --- a/src/_archive/app/from_string.rs +++ b/src/_archive/app/from_string.rs @@ -63,7 +63,7 @@ impl App { pub async fn dl_from_url(&self, urlstr: &str) -> Result { let url = reqwest::Url::parse(urlstr)?; - match ( + Ok(match ( url.domain(), url.path_segments() .map_or_else(Default::default, Iterator::collect::>) @@ -71,10 +71,10 @@ impl App { ) { // https://cdn.modrinth.com/data/{ID}/versions/{VERSION}/{FILENAME} (Some("cdn.modrinth.com"), ["data", id, "versions", version, _filename]) => { - Ok(Downloadable::Modrinth { + Downloadable::Modrinth { id: id.to_owned().to_owned(), version: version.to_owned().to_owned(), - }) + } }, (Some("modrinth.com"), ["mod" | "plugin" | "datapack", id, rest @ ..]) => { @@ -108,10 +108,10 @@ impl App { version.id.clone() }; - Ok(Downloadable::Modrinth { + Downloadable::Modrinth { id: id.to_owned().to_owned(), version: version.clone(), - }) + } }, (Some("curserinth.kuylar.dev"), ["mod", id, rest @ ..]) => { @@ -145,10 +145,10 @@ impl App { version.id.clone() }; - Ok(Downloadable::CurseRinth { + Downloadable::CurseRinth { id: (*id).to_string(), version, - }) + } }, // https://www.curseforge.com/minecraft/mc-mods/betterwithpatches @@ -185,14 +185,14 @@ impl App { version.id.clone() }; - Ok(Downloadable::CurseRinth { id, version }) + Downloadable::CurseRinth { id, version } }, // https://www.spigotmc.org/resources/http-requests.101253/ - (Some("www.spigotmc.org"), ["resources", id]) => Ok(Downloadable::Spigot { + (Some("www.spigotmc.org"), ["resources", id]) => Downloadable::Spigot { id: (*id).to_string(), version: "latest".to_owned(), - }), + }, // https://github.com/{owner}/{repo}/releases/{'tag'|'download'}/{tag}/{filename} (Some("github.com"), [owner, repo_name, rest @ ..]) => { @@ -266,11 +266,11 @@ impl App { } }; - Ok(Downloadable::GithubRelease { + Downloadable::GithubRelease { repo, tag: tag.to_string(), asset, - }) + } }, (domain, path) => { @@ -310,11 +310,11 @@ impl App { let desc = self.prompt_string_default("Optional description/comment?", "")?; - Ok(Downloadable::Url { + Downloadable::Url { url: urlstr.to_owned(), filename: Some(input), desc: if desc.is_empty() { None } else { Some(desc) }, - }) + } }, 1 => { // TODO: ... @@ -354,12 +354,12 @@ impl App { let build = self.prompt_string_filled("Build", "latest")?; let artifact = self.prompt_string_filled("Artifact", "first")?; - Ok(Downloadable::Jenkins { + Downloadable::Jenkins { url: j_url, job, build, artifact, - }) + } }, 2 => { let mut repo = None; @@ -456,17 +456,17 @@ impl App { self.prompt_string_default("Filename?", "${artifact}-${version}.jar")? }; - Ok(Downloadable::Maven { + Downloadable::Maven { url: repo, group, artifact, version, filename, - }) + } }, _ => unreachable!(), } }, - } + }) } } diff --git a/src/api/app/cache/mod.rs b/src/api/app/cache/mod.rs index 301c4b3..0e3a06b 100644 --- a/src/api/app/cache/mod.rs +++ b/src/api/app/cache/mod.rs @@ -27,7 +27,7 @@ impl Cache { /// Tries to read a json file from cache. Returns `None` if it's not in cache or there is not a cache folder pub fn try_get_json(&self, path: &str) -> Result> { - match &self.0 { + Ok(match &self.0 { Some(base) => { let fullpath = base.join(path); @@ -38,32 +38,28 @@ impl Cache { let file = File::open(fullpath)?; let reader = BufReader::new(file); - Ok(serde_json::from_reader(reader)?) + serde_json::from_reader(reader)? }, - None => Ok(None), - } + None => None, + }) } /// Try to write a json file to cache. Does nothing if there is no cache folder pub fn try_write_json(&self, path: &str, data: &T) -> Result<()> { - match &self.0 { - Some(base) => { - let fullpath = base.join(path); - - create_parents_sync(&fullpath)?; + if let Some(base) = &self.0 { + let fullpath = base.join(path); - let writer = BufWriter::new( - File::create(&fullpath) - .context(format!("Creating cache file at: {}", fullpath.display()))?, - ); + create_parents_sync(&fullpath)?; - serde_json::to_writer(writer, data)?; + let writer = BufWriter::new( + File::create(&fullpath) + .context(format!("Creating cache file at: {}", fullpath.display()))?, + ); - Ok(()) - }, - - _ => Ok(()), + serde_json::to_writer(writer, data)?; } + + Ok(()) } } diff --git a/src/api/models/metadata/addon_metadata.rs b/src/api/models/metadata/addon_metadata.rs index a703163..c8c88cc 100644 --- a/src/api/models/metadata/addon_metadata.rs +++ b/src/api/models/metadata/addon_metadata.rs @@ -84,68 +84,68 @@ pub struct AddonMetadata { impl Addon { pub async fn resolve_metadata(&self, app: &App) -> Result { - match &self.addon_type { - AddonType::Url { url } => Ok(AddonMetadata { + Ok(match &self.addon_type { + AddonType::Url { url } => AddonMetadata { name: get_filename_from_url(url), description: None, link: Some(url.to_owned()), source: AddonMetadataSource::Other, version: None, - }), + }, AddonType::Modrinth { id, version } => { let proj = app.modrinth().fetch_project(id).await?; - Ok(AddonMetadata { + AddonMetadata { name: proj.title, description: Some(proj.description), version: Some(version.to_owned()), link: Some(format!("https://modrinth.com/{}", proj.slug)), source: AddonMetadataSource::Modrinth, - }) + } }, AddonType::Curseforge { id, version } => { let m = app.curseforge().fetch_mod(id).await?; - Ok(AddonMetadata { + AddonMetadata { name: m.name, description: Some(m.summary), version: Some(version.to_owned()), link: Some(m.links.website_url), source: AddonMetadataSource::Curseforge, - }) + } }, AddonType::Spigot { id, version } => { let r = app.spigot().fetch_resource(id).await?; - Ok(AddonMetadata { + AddonMetadata { name: r.name, description: Some(r.tag), version: Some(version.to_owned()), link: Some(format!("https://www.spigotmc.org/resources/{id}")), source: AddonMetadataSource::Curseforge, - }) + } }, AddonType::Hangar { id, version } => { let proj = app.hangar().fetch_project(id).await?; - Ok(AddonMetadata { + AddonMetadata { name: proj.name, link: Some(format!("https://hangar.papermc.io/{}", proj.namespace)), description: Some(proj.description), source: AddonMetadataSource::Hangar, version: Some(version.to_owned()), - }) + } }, AddonType::GithubRelease { repo, version, .. } => { let desc = app.github().fetch_repo_description(repo).await?; - Ok(AddonMetadata { + AddonMetadata { name: repo.to_owned(), description: Some(desc), link: Some(format!("https://github.com/{repo}")), source: AddonMetadataSource::Github, version: Some(version.to_owned()), - }) + } }, AddonType::Jenkins { url, @@ -155,13 +155,13 @@ impl Addon { } => { let description = app.jenkins().fetch_description(url, job).await?; - Ok(AddonMetadata { + AddonMetadata { name: artifact.to_owned(), link: Some(jenkins_job_url(url, job)), description, source: AddonMetadataSource::Jenkins, version: Some(build.to_owned()), - }) + } }, AddonType::MavenArtifact { url, @@ -169,13 +169,13 @@ impl Addon { artifact, version, .. - } => Ok(AddonMetadata { + } => AddonMetadata { name: artifact.to_owned(), link: Some(maven_artifact_url(url, group, artifact)), description: None, source: AddonMetadataSource::Maven, version: Some(version.to_owned()), - }), - } + }, + }) } } diff --git a/src/api/models/packwiz/mod.rs b/src/api/models/packwiz/mod.rs index c0b0b13..57d32c7 100644 --- a/src/api/models/packwiz/mod.rs +++ b/src/api/models/packwiz/mod.rs @@ -39,17 +39,17 @@ pub async fn resolve_packwiz_addons(app: &App, mut accessor: Accessor) -> Result impl PackwizModUpdate { pub fn from_addon_type(addon_type: &AddonType) -> Result> { - match addon_type.clone() { - AddonType::Modrinth { id, version } => Ok(Some(Self::Modrinth { + Ok(match addon_type.clone() { + AddonType::Modrinth { id, version } => Some(Self::Modrinth { mod_id: id, version, - })), - AddonType::Curseforge { id, version } => Ok(Some(Self::Curseforge { + }), + AddonType::Curseforge { id, version } => Some(Self::Curseforge { file_id: version.parse()?, project_id: id.parse()?, - })), - _ => Ok(None), - } + }), + _ => None, + }) } } diff --git a/src/api/models/server/server_type.rs b/src/api/models/server/server_type.rs index b646a05..bcd3656 100644 --- a/src/api/models/server/server_type.rs +++ b/src/api/models/server/server_type.rs @@ -221,8 +221,8 @@ impl ServerJar { } pub async fn update(&mut self, app: &App) -> Result { - match self.server_type.clone() { - ServerType::Vanilla {} | ServerType::BuildTools { .. } => Ok(false), + Ok(match self.server_type.clone() { + ServerType::Vanilla {} | ServerType::BuildTools { .. } => false, ServerType::PaperMC { project, build } => { let new_build = app .papermc() @@ -240,7 +240,7 @@ impl ServerJar { build: new_build.clone(), }; - Ok(new_build != build) + new_build != build }, ServerType::Purpur { build } => todo!(), ServerType::Fabric { loader, installer } => { @@ -266,12 +266,12 @@ impl ServerJar { installer: latest_installer.clone(), }; - Ok(loader != latest_loader || installer != latest_installer) + loader != latest_loader || installer != latest_installer }, ServerType::Quilt { loader, installer } => todo!(), ServerType::NeoForge { loader } => todo!(), ServerType::Forge { loader } => todo!(), ServerType::Custom { .. } => todo!(), - } + }) } } diff --git a/src/api/sources/buildtools/mod.rs b/src/api/sources/buildtools/mod.rs index 84c845e..f608a8f 100644 --- a/src/api/sources/buildtools/mod.rs +++ b/src/api/sources/buildtools/mod.rs @@ -110,13 +110,15 @@ pub fn get_java_version_for(mc_version: &str) -> Result { let mut split = mc_version.split('.'); split.next().ok_or(anyhow!("Error parsing mc_version"))?; - match split - .next() - .ok_or(anyhow!("Error parsing mc_version"))? - .parse::()? - { - ..=16 => Ok(8), - 17 => Ok(16), - _ => Ok(17), - } + Ok( + match split + .next() + .ok_or(anyhow!("Error parsing mc_version"))? + .parse::()? + { + ..=16 => 8, + 17 => 16, + _ => 17, + }, + ) } diff --git a/src/api/sources/vanilla/mod.rs b/src/api/sources/vanilla/mod.rs index 697256f..7b05278 100644 --- a/src/api/sources/vanilla/mod.rs +++ b/src/api/sources/vanilla/mod.rs @@ -67,9 +67,7 @@ impl<'a> VanillaAPI<'a> { let version: VersionInfo = self.0.http_get_json(indexed.url).await?; - let steps: Vec = version - .as_step(env) - .ok_or(anyhow!("Cant find download"))?; + let steps: Vec = version.as_step(env).ok_or(anyhow!("Cant find download"))?; Ok(steps) } diff --git a/src/api/utils/accessor.rs b/src/api/utils/accessor.rs index 4f1b71c..fe8e138 100644 --- a/src/api/utils/accessor.rs +++ b/src/api/utils/accessor.rs @@ -56,28 +56,28 @@ impl Accessor { /// Read a JSON file pub async fn json(&mut self, app: &App, path: &str) -> Result { - match self { - Self::Local(base) => Ok(serde_json::from_reader(File::open(base.join(path))?)?), - Self::ZipLocal((_, zip)) => Ok(serde_json::from_reader(zip.by_name(path)?)?), - Self::Remote(url) => Ok(app.http_get_json(url.join(path)?).await?), - } + Ok(match self { + Self::Local(base) => serde_json::from_reader(File::open(base.join(path))?)?, + Self::ZipLocal((_, zip)) => serde_json::from_reader(zip.by_name(path)?)?, + Self::Remote(url) => app.http_get_json(url.join(path)?).await?, + }) } /// Read a TOML file pub async fn toml(&mut self, app: &App, path: &str) -> Result { - match self { - Self::Local(base) => Ok(toml::from_str(&fs::read_to_string(base.join(path)).await?)?), + Ok(match self { + Self::Local(base) => toml::from_str(&fs::read_to_string(base.join(path)).await?)?, Self::ZipLocal((_, zip)) => { let file = zip.by_name(path)?; - Ok(toml::from_str(&std::io::read_to_string(file)?)?) + toml::from_str(&std::io::read_to_string(file)?)? }, Self::Remote(url) => { let res = app.http_get(url.join(path)?).await?; let content = res.text().await?; - Ok(toml::from_str(&content)?) + toml::from_str(&content)? }, - } + }) } } diff --git a/src/api/utils/update_writer.rs b/src/api/utils/update_writer.rs index 0c23b38..7f84b23 100644 --- a/src/api/utils/update_writer.rs +++ b/src/api/utils/update_writer.rs @@ -27,24 +27,24 @@ impl UpdateWriter { todo!(); /* match &source.source_type { - SourceType::File { path } => Ok(Self::File(path, read_toml(relative_to.join(path).to_path_buf())?)), + SourceType::File { path } => Self::File(path, read_toml(relative_to.join(path).to_path_buf())?)), SourceType::Folder { path } => unimplemented!(), SourceType::Modpack { modpack_source: ModpackSource::Local { path, can_update: true }, modpack_type - } => match modpack_type { - ModpackType::MRPack => Ok(Self::MRPack( + } => Ok(match modpack_type { + ModpackType::MRPack => Self::MRPack( relative_to.join(path).to_path_buf(), ZipArchive::new(std::fs::File::open(relative_to.join(path))?)?, todo!() - )), - ModpackType::Packwiz => Ok(Self::Packwiz( + ), + ModpackType::Packwiz => Self::Packwiz( relative_to.join(path), todo!(), todo!(), - )), + ), ModpackType::Unsup => unimplemented!(), - }, + }), _ => Err(anyhow!("Can't make an UpdateWriter")), } */ }