Struct modinfo::Modinfo

source ·
pub struct Modinfo { /* private fields */ }
Expand description

The main struct for the library

Fields

  • name - the name of the modlet
  • display_name - the display name of the modlet (v2 only)
  • version - the version of the modlet
  • description - the description of the modlet
  • author - the author of the modlet
  • website - the website of the modlet (v2 only)

Additionally, version supports an optional compat field which can be used to indicate the game’s version for the compatibility string

Example

use modinfo::Modinfo;
use std::borrow::Cow;

let mut modinfo = Modinfo::new();

modinfo.set_version("0.1.0".to_owned());
modinfo.set_value_for("name", "SomeMod");
modinfo.set_value_for("display_name", "Some Mod");
modinfo.set_value_for("author", "Some Author");
modinfo.set_value_for("description", "Some Description");
modinfo.set_value_for("website", "https://example.org");

assert_eq!(modinfo.get_value_for("name"), Some(&Cow::from("SomeMod")));
assert_eq!(modinfo.get_value_for("display_name"), Some(&Cow::from("Some Mod")));
assert_eq!(modinfo.get_value_for("author"), Some(&Cow::from("Some Author")));
assert_eq!(modinfo.get_value_for("description"), Some(&Cow::from("Some Description")));
assert_eq!(modinfo.get_value_for("website"), Some(&Cow::from("https://example.org")));
assert_eq!(modinfo.get_version(), &semver::Version::new(0, 1, 0));

Implementations§

source§

impl Modinfo

source

pub fn new() -> Self

Create a new Modinfo instance with default values

source

pub fn write(&self, file: Option<&Path>) -> Result<(), ModinfoError>

Write the Modinfo to a file uses modinfo_version to determine which format to use

source

pub fn get_value_for<F>(&self, field: F) -> Option<&Cow<'_, str>>where F: AsRef<str>,

Retrieve the value for a given field.

Note: This is not case-sensitive so you can use get_value_for("Author") or get_value_for("author")

Please note that version is excluded from this function, use get_version instead

use modinfo::Modinfo;

let mut modinfo = Modinfo::default();
modinfo.set_value_for("author", "Joe");

assert_eq!(modinfo.get_value_for("author"), Some(&std::borrow::Cow::from("Joe")));
source

pub fn set_value_for(&mut self, field: &str, value: &str)

Set the value for a given field to value

Note: field is not case-sensitive, so you can use set_value_for("Author", "Joe") or get_value_for("author", "Joe")

use modinfo::Modinfo;

let mut modinfo = Modinfo::default();
modinfo.set_value_for("name", "MyMod");

assert_eq!(modinfo.get_value_for("name"), Some(&std::borrow::Cow::from("MyMod")));
source

pub fn get_version(&self) -> &Version

Retrieve the value for the version field included the ModInfo

Note: This is the version of the modlet, not the version of the ModInfo file format Please see get_modinfo_version for that.

use modinfo::Modinfo;

let modinfo = Modinfo::default();

assert_eq!(modinfo.get_version(), &semver::Version::new(0, 1, 0));
source

pub fn set_version(&mut self, version: String)

Sets the version field inside the modinfo.xml file (modlet version)

use modinfo::Modinfo;

let mut modinfo = Modinfo::default();
modinfo.set_version("1.2.3".to_owned());

assert_eq!(modinfo.get_version(), &semver::Version::new(1, 2, 3));
source

pub fn get_modinfo_version(&self) -> ModinfoVersion

Retrieves the current version of the ModInfo.xml file (V1 or V2)

returns a ModinfoVersion enum: ModinfoVersion::V1 ModinfoVersion::V2

use modinfo::{Modinfo, ModinfoVersion};

let mut modinfo = Modinfo::default();

assert_eq!(modinfo.get_modinfo_version(), ModinfoVersion::V2);
source

pub fn set_modinfo_version(&mut self, version: ModinfoVersion)

Sets the version of the ModInfo.xml file itesle (V1 or V2)

Accepts a ModinfoVersion enum: ModinfoVersion::V1 ModinfoVersion::V2

use modinfo::{Modinfo, ModinfoVersion};

let mut modinfo = Modinfo::default();
modinfo.set_modinfo_version(ModinfoVersion::V1);

assert_eq!(modinfo.get_modinfo_version(), ModinfoVersion::V1);
source

pub fn get_file_path(&self) -> &PathBuf

Retrieves the current modinfo.xml file path

use modinfo::Modinfo;
use std::path::PathBuf;

let mut modinfo = Modinfo::default();
modinfo.set_file_path(PathBuf::from("modinfo.xml"));

assert_eq!(modinfo.get_file_path(), &PathBuf::from("modinfo.xml"));
source

pub fn set_file_path(&mut self, path: PathBuf)

Sets the modinfo.xml file path

This is normally set automatically when a file is parsed, but can be set manually such as when creating a new modinfo.xml file.

use modinfo::Modinfo;
use std::path::PathBuf;

let mut modinfo = Modinfo::default();
modinfo.set_file_path(PathBuf::from("modinfo.xml"));

assert_eq!(modinfo.get_file_path(), &PathBuf::from("modinfo.xml"));
source

pub fn bump_version_major(&mut self)

Increases the Major version number by 1, sets Minor and Patch to 0, and removes any pre or build data.

use modinfo::Modinfo;

let mut modinfo = Modinfo::default();
modinfo.set_version("1.2.3-foo+bar".to_owned());
modinfo.bump_version_major();

assert_eq!(modinfo.get_version(), &semver::Version::new(2, 0, 0));
source

pub fn bump_version_minor(&mut self)

Increases the Minor version number by 1, sets Patch to 0, and removes any pre or build data.

use modinfo::Modinfo;

let mut modinfo = Modinfo::default();
modinfo.set_version("1.2.3-foo+bar".to_owned());
modinfo.bump_version_minor();

assert_eq!(modinfo.get_version(), &semver::Version::new(1, 3, 0));
source

pub fn bump_version_patch(&mut self)

Increases the Patch version number by 1, and removes any pre or build data.

use modinfo::Modinfo;

let mut modinfo = Modinfo::default();
modinfo.set_version("1.2.3-foo+bar".to_owned());
modinfo.bump_version_patch();
assert_eq!(modinfo.get_version(), &semver::Version::new(1, 2, 4));
source

pub fn add_version_pre(&mut self, pre: &str)

Adds a pre-release version to the version field

use modinfo::Modinfo;

let mut modinfo = Modinfo::default();
modinfo.set_version("1.2.3".to_owned());
modinfo.add_version_pre("foo");
assert_eq!(modinfo.get_version(), &semver::Version::parse("1.2.3-foo").unwrap());
source

pub fn add_version_build(&mut self, build: &str)

Adds build data to the version field

use modinfo::Modinfo;

let mut modinfo = Modinfo::default();
modinfo.set_version("1.2.3".to_owned());
modinfo.add_version_build("bar");
assert_eq!(modinfo.get_version(), &semver::Version::parse("1.2.3+bar").unwrap());

Trait Implementations§

source§

impl Debug for Modinfo

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Modinfo

source§

fn default() -> Modinfo

Returns the “default value” for a type. Read more
source§

impl FromStr for Modinfo

§

type Err = ModinfoError

The associated error which can be returned from parsing.
source§

fn from_str(xml: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl ToString for Modinfo

source§

fn to_string(&self) -> String

Converts the given value to a String. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.