tiger-lib 1.17.0

Library used by the tools ck3-tiger, vic3-tiger, and imperator-tiger. This library holds the bulk of the code for them. It can be built either for ck3-tiger with the feature ck3, or for vic3-tiger with the feature vic3, or for imperator-tiger with the feature imperator, but not both at the same time.
Documentation
use crate::block::{BV, Block, BlockItem, Field};
use crate::fileset::FileEntry;
use crate::token::{Loc, Token};
use crate::trigger::Part;
use crate::validator::ValueValidator;

/// This trait lets the error reporting functions accept a variety of things as the error locator.
pub trait ErrorLoc {
    fn loc_length(&self) -> usize {
        1
    }
    fn into_loc(self) -> Loc;
}

impl ErrorLoc for ValueValidator<'_> {
    fn loc_length(&self) -> usize {
        self.value().loc_length()
    }

    fn into_loc(self) -> Loc {
        self.value().into_loc()
    }
}

impl ErrorLoc for &ValueValidator<'_> {
    fn loc_length(&self) -> usize {
        self.value().loc_length()
    }

    fn into_loc(self) -> Loc {
        self.value().into_loc()
    }
}

impl ErrorLoc for &mut ValueValidator<'_> {
    fn loc_length(&self) -> usize {
        self.value().loc_length()
    }

    fn into_loc(self) -> Loc {
        self.value().into_loc()
    }
}

impl ErrorLoc for BlockItem {
    fn loc_length(&self) -> usize {
        match self {
            BlockItem::Value(token) => token.loc_length(),
            BlockItem::Block(block) => block.loc_length(),
            BlockItem::Field(field) => field.loc_length(),
        }
    }

    fn into_loc(self) -> Loc {
        match self {
            BlockItem::Value(token) => token.into_loc(),
            BlockItem::Block(block) => block.into_loc(),
            BlockItem::Field(field) => field.into_loc(),
        }
    }
}

impl ErrorLoc for &BlockItem {
    fn loc_length(&self) -> usize {
        match self {
            BlockItem::Value(token) => token.loc_length(),
            BlockItem::Block(block) => block.loc_length(),
            BlockItem::Field(field) => field.loc_length(),
        }
    }

    fn into_loc(self) -> Loc {
        match self {
            BlockItem::Value(token) => token.into_loc(),
            BlockItem::Block(block) => block.into_loc(),
            BlockItem::Field(field) => field.into_loc(),
        }
    }
}

impl ErrorLoc for Field {
    fn into_loc(self) -> Loc {
        self.into_key().into_loc()
    }
}

impl ErrorLoc for &Field {
    fn into_loc(self) -> Loc {
        self.key().into_loc()
    }
}

impl ErrorLoc for BV {
    fn loc_length(&self) -> usize {
        match self {
            BV::Value(token) => token.loc_length(),
            BV::Block(block) => block.loc_length(),
        }
    }

    fn into_loc(self) -> Loc {
        match self {
            BV::Value(token) => token.into_loc(),
            BV::Block(block) => block.into_loc(),
        }
    }
}

impl ErrorLoc for &BV {
    fn loc_length(&self) -> usize {
        match self {
            BV::Value(token) => token.loc_length(),
            BV::Block(block) => block.loc_length(),
        }
    }

    fn into_loc(self) -> Loc {
        match self {
            BV::Value(t) => t.into_loc(),
            BV::Block(s) => s.into_loc(),
        }
    }
}

impl ErrorLoc for FileEntry {
    fn into_loc(self) -> Loc {
        Loc::from(&self)
    }
}

impl ErrorLoc for &FileEntry {
    fn into_loc(self) -> Loc {
        Loc::from(self)
    }
}

impl ErrorLoc for Loc {
    fn into_loc(self) -> Loc {
        self
    }
}

impl ErrorLoc for Token {
    fn loc_length(&self) -> usize {
        self.as_str().chars().count().max(1)
    }

    fn into_loc(self) -> Loc {
        self.loc
    }
}

impl ErrorLoc for &Token {
    fn loc_length(&self) -> usize {
        self.as_str().chars().count().max(1)
    }

    fn into_loc(self) -> Loc {
        self.loc
    }
}

impl ErrorLoc for Block {
    fn into_loc(self) -> Loc {
        self.loc
    }
}

impl ErrorLoc for &Block {
    fn into_loc(self) -> Loc {
        self.loc
    }
}

impl ErrorLoc for Part {
    fn into_loc(self) -> Loc {
        match self {
            Part::Token(t) | Part::TokenArgument(t, _, _) => t.loc,
        }
    }

    fn loc_length(&self) -> usize {
        match self {
            Part::Token(t) => t.loc_length(),
            Part::TokenArgument(part, _, _) => part.loc_length(),
        }
    }
}

impl ErrorLoc for &Part {
    fn into_loc(self) -> Loc {
        match self {
            Part::Token(t) | Part::TokenArgument(t, _, _) => t.loc,
        }
    }

    fn loc_length(&self) -> usize {
        match self {
            Part::Token(t) => t.loc_length(),
            Part::TokenArgument(part, _, _) => part.loc_length(),
        }
    }
}