Skip to content

Commit

Permalink
refactor: remove unnecessary repeated Ok()s
Browse files Browse the repository at this point in the history
  • Loading branch information
null8626 committed Sep 24, 2024
1 parent cf347dc commit 6c426dd
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 96 deletions.
38 changes: 19 additions & 19 deletions src/_archive/app/from_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ impl App {
pub async fn dl_from_url(&self, urlstr: &str) -> Result<Downloadable> {
let url = reqwest::Url::parse(urlstr)?;

match (
Ok(match (
url.domain(),
url.path_segments()
.map_or_else(Default::default, Iterator::collect::<Vec<_>>)
.as_slice(),
) {
// 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 @ ..]) => {
Expand Down Expand Up @@ -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 @ ..]) => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 @ ..]) => {
Expand Down Expand Up @@ -266,11 +266,11 @@ impl App {
}
};

Ok(Downloadable::GithubRelease {
Downloadable::GithubRelease {
repo,
tag: tag.to_string(),
asset,
})
}
},

(domain, path) => {
Expand Down Expand Up @@ -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: ...
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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!(),
}
},
}
})
}
}
32 changes: 14 additions & 18 deletions src/api/app/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: DeserializeOwned>(&self, path: &str) -> Result<Option<T>> {
match &self.0 {
Ok(match &self.0 {
Some(base) => {
let fullpath = base.join(path);

Expand All @@ -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<T: serde::Serialize>(&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(())
}
}
36 changes: 18 additions & 18 deletions src/api/models/metadata/addon_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,68 +84,68 @@ pub struct AddonMetadata {

impl Addon {
pub async fn resolve_metadata(&self, app: &App) -> Result<AddonMetadata> {
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,
Expand All @@ -155,27 +155,27 @@ 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,
group,
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()),
}),
}
},
})
}
}
14 changes: 7 additions & 7 deletions src/api/models/packwiz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<Self>> {

Check warning on line 41 in src/api/models/packwiz/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

associated function `from_addon_type` is never used

warning: associated function `from_addon_type` is never used --> src/api/models/packwiz/mod.rs:41:12 | 40 | impl PackwizModUpdate { | --------------------- associated function in this implementation 41 | pub fn from_addon_type(addon_type: &AddonType) -> Result<Option<Self>> { | ^^^^^^^^^^^^^^^
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,
})
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/api/models/server/server_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ impl ServerJar {
}

pub async fn update(&mut self, app: &App) -> Result<bool> {
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()
Expand All @@ -240,7 +240,7 @@ impl ServerJar {
build: new_build.clone(),
};

Ok(new_build != build)
new_build != build
},
ServerType::Purpur { build } => todo!(),

Check warning on line 245 in src/api/models/server/server_type.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `build`

warning: unused variable: `build` --> src/api/models/server/server_type.rs:245:34 | 245 | ServerType::Purpur { build } => todo!(), | ^^^^^ help: try ignoring the field: `build: _`
ServerType::Fabric { loader, installer } => {
Expand All @@ -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!(),

Check warning on line 271 in src/api/models/server/server_type.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `installer`

warning: unused variable: `installer` --> src/api/models/server/server_type.rs:271:41 | 271 | ServerType::Quilt { loader, installer } => todo!(), | ^^^^^^^^^ help: try ignoring the field: `installer: _`

Check warning on line 271 in src/api/models/server/server_type.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `loader`

warning: unused variable: `loader` --> src/api/models/server/server_type.rs:271:33 | 271 | ServerType::Quilt { loader, installer } => todo!(), | ^^^^^^ help: try ignoring the field: `loader: _`
ServerType::NeoForge { loader } => todo!(),

Check warning on line 272 in src/api/models/server/server_type.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `loader`

warning: unused variable: `loader` --> src/api/models/server/server_type.rs:272:36 | 272 | ServerType::NeoForge { loader } => todo!(), | ^^^^^^ help: try ignoring the field: `loader: _`
ServerType::Forge { loader } => todo!(),

Check warning on line 273 in src/api/models/server/server_type.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `loader`

warning: unused variable: `loader` --> src/api/models/server/server_type.rs:273:33 | 273 | ServerType::Forge { loader } => todo!(), | ^^^^^^ help: try ignoring the field: `loader: _`
ServerType::Custom { .. } => todo!(),
}
})
}
}
20 changes: 11 additions & 9 deletions src/api/sources/buildtools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ pub fn get_java_version_for(mc_version: &str) -> Result<JavaVersion> {
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::<i32>()?
{
..=16 => Ok(8),
17 => Ok(16),
_ => Ok(17),
}
Ok(
match split
.next()
.ok_or(anyhow!("Error parsing mc_version"))?
.parse::<i32>()?
{
..=16 => 8,
17 => 16,
_ => 17,
},
)
}
4 changes: 1 addition & 3 deletions src/api/sources/vanilla/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ impl<'a> VanillaAPI<'a> {

let version: VersionInfo = self.0.http_get_json(indexed.url).await?;

let steps: Vec<Step> = version
.as_step(env)
.ok_or(anyhow!("Cant find download"))?;
let steps: Vec<Step> = version.as_step(env).ok_or(anyhow!("Cant find download"))?;

Ok(steps)
}
Expand Down
Loading

0 comments on commit 6c426dd

Please sign in to comment.