MForster / factorio-rust-tools

A Rust library to export prototype definitions from Factorio.
4 stars 1 forks source link

Refactor the factorio-mod-api #69

Open Builditluc opened 1 year ago

Builditluc commented 1 year ago

Currently, the factorio-mod-api returns its data in the same format the API returns it. I think it would be better to create some basic, more abstract data models. These are returned by the library and when some other library needs to access some of the features it'll implement the conversion to the correct format itself.

For example, the whole mod api revolves around mods. So we need to implement a basic struct Mod that has all of the information of a mod (including dependencies, etc.). This would mean that we remove the whole "short" and "full" mod thing but I personally think that's okay because it just adds a lot of unnecessary logic.

EDIT: Mod would contain sub structs that contain certain information for example Metadata containing things like the owner or the homepage and source. It would be used like this

// get a mod
let mod: Mod = get_mod("krastorio2"); // just a placeholder for getting a mod

let owner = mod
    .metadata
    .owner;

// if we want to get the latest release
let latest_release = mod
    .latest_release(); // the releases are stored in a list so we can get the last one by using this function

// get the download URL of the latest release
let latest_downlaod = mod
    .latest_release()
    .download;

// get a dependency of the mod
let dep = mod
    .latest_release()
    .dependencies[0];

// extract the flavor of the dependency
let flavor = dep
    .flavor;

// and the version
let version: Comparator = dep
    .version;
Builditluc commented 1 year ago

The ModPortalClient would then cache these Mod structs. With this refactor, I imagine the use would be like this

let client = ModPortalClient::new()?;

let mod = client.get_mod_spec("Krastorio 2").await?;

// this mod data can now be used in other libraries
MForster commented 1 year ago

Sounds good to me. Do you want to send a pull request?