Struct Codec

Source
pub struct Codec {
    pub resources: Box<Vec<Resource>>,
}
Expand description

Represents a collection of localized resources and provides methods to read, write, cache, and load these resources.

Fields§

§resources: Box<Vec<Resource>>

The collection of resources managed by this codec.

Implementations§

Source§

impl Codec

Source

pub fn new() -> Self

Creates a new, empty Codec.

§Returns

A new Codec instance with no resources.

Source

pub fn iter(&self) -> Iter<'_, Resource>

Returns an iterator over all resources.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, Resource>

Returns a mutable iterator over all resources.

Source

pub fn get_by_language(&self, lang: &str) -> Option<&Resource>

Finds a resource by its language code, if present.

Source

pub fn get_mut_by_language(&mut self, lang: &str) -> Option<&mut Resource>

Finds a mutable resource by its language code, if present.

Source

pub fn add_resource(&mut self, resource: Resource)

Adds a new resource to the collection.

Source

pub fn read_file_by_type<P: AsRef<Path>>( &mut self, path: P, format_type: FormatType, ) -> Result<(), Error>

Reads a resource file given its path and explicit format type.

§Parameters
  • path: Path to the resource file.
  • format_type: The format type of the resource file.
§Returns

Ok(()) if the file was successfully read and resources loaded, or an Error otherwise.

Source

pub fn read_file_by_extension<P: AsRef<Path>>( &mut self, path: P, lang: Option<String>, ) -> Result<(), Error>

Reads a resource file by inferring its format from the file extension. Optionally infers language from the path if not provided.

§Parameters
  • path: Path to the resource file.
  • lang: Optional language code to use.
§Returns

Ok(()) if the file was successfully read, or an Error if the format is unsupported or reading fails.

Source

pub fn write_to_file(&self) -> Result<(), Error>

Writes all managed resources back to their respective files, grouped by domain.

§Returns

Ok(()) if all writes succeed, or an Error otherwise.

Source

pub fn cache_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), Error>

Caches the current resources to a JSON file.

§Parameters
  • path: Destination file path for the cache.
§Returns

Ok(()) if caching succeeds, or an Error if file I/O or serialization fails.

Source

pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self, Error>

Loads resources from a JSON cache file.

§Parameters
  • path: Path to the JSON file containing cached resources.
§Returns

Ok(Codec) with loaded resources, or an Error if loading or deserialization fails.

Auto Trait Implementations§

§

impl Freeze for Codec

§

impl RefUnwindSafe for Codec

§

impl Send for Codec

§

impl Sync for Codec

§

impl Unpin for Codec

§

impl UnwindSafe for Codec

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.