rpfm_extensions/startpos/world/calendar.rs
1//---------------------------------------------------------------------------//
2// Copyright (c) 2017-2026 Ismael Gutiérrez González. All rights reserved.
3//
4// This file is part of the Rusted PackFile Manager (RPFM) project,
5// which can be found here: https://github.com/Frodo45127/rpfm.
6//
7// This file is licensed under the MIT license, which can be found here:
8// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
9//---------------------------------------------------------------------------//
10
11//! The calendar of a campaign (`start_pos_calendars`).
12
13use getset::{CopyGetters, Getters};
14
15use rpfm_lib::error::{RLibError, Result};
16
17use crate::startpos::tables::StartposTables;
18
19#[cfg(test)] #[path = "calendar_test.rs"] mod calendar_test;
20
21//---------------------------------------------------------------------------//
22// Types
23//---------------------------------------------------------------------------//
24
25/// When a campaign starts and how many turns its years have.
26#[derive(Debug, Clone, PartialEq, Eq, Default, Getters, CopyGetters)]
27pub struct Calendar {
28
29 /// Year of the first turn, as the game counts them (Attila counts from the founding of Rome).
30 #[getset(get_copy = "pub")]
31 start_year: u32,
32
33 /// Turns per year.
34 #[getset(get_copy = "pub")]
35 turns_per_year: u32,
36
37 /// Season of the first turn (`seasons` key).
38 #[getset(get = "pub")]
39 start_season: String,
40}
41
42//---------------------------------------------------------------------------//
43// Implementations
44//---------------------------------------------------------------------------//
45
46impl Calendar {
47
48 /// Reads the calendar of a campaign.
49 ///
50 /// # Errors
51 ///
52 /// Returns an error if the campaign has no calendar, or the table lacks a column.
53 pub fn new(tables: &StartposTables, campaign: &str) -> Result<Self> {
54 for row in tables.table("start_pos_calendars")?.rows() {
55 if row.str("campaign")? == campaign {
56 return Ok(Self {
57 start_year: u32::try_from(row.int("start_year")?).unwrap_or_default(),
58 turns_per_year: u32::try_from(row.int("turns_per_year")?).unwrap_or_default(),
59 start_season: row.str("start_season")?.into_owned(),
60 });
61 }
62 }
63 Err(RLibError::StartposInvalidValue("start_pos_calendars".to_owned(), "campaign".to_owned(), campaign.to_owned()))
64 }
65}