pub struct Text { /* private fields */ }Expand description
In-memory representation of a decoded text file.
Stores the text contents along with encoding and format metadata. The encoding is preserved when re-encoding to maintain file compatibility.
§Fields
encoding- Character encoding detected or specified for the fileformat- File format detected from extension (for syntax highlighting)contents- Decoded text contents as a UTF-8 Rust string
§Getters/Setters
All fields have public getters, mutable getters, and setters via the getset crate:
encoding(),encoding_mut(),set_encoding()format(),format_mut(),set_format()contents(),contents_mut(),set_contents()
§Example
use rpfm_lib::files::{Decodeable, text::Text, DecodeableExtraData};
use std::io::Cursor;
let data = b"Hello, World!";
let mut reader = Cursor::new(data);
let text = Text::decode(&mut reader, &None).unwrap();
assert_eq!(text.contents(), "Hello, World!");Implementations§
Source§impl Text
impl Text
Sourcepub fn encoding_mut(&mut self) -> &mut Encoding
pub fn encoding_mut(&mut self) -> &mut Encoding
Character encoding of the file.
Sourcepub fn format_mut(&mut self) -> &mut TextFormat
pub fn format_mut(&mut self) -> &mut TextFormat
Detected file format based on extension.
Sourcepub fn contents_mut(&mut self) -> &mut String
pub fn contents_mut(&mut self) -> &mut String
Decoded text contents.
Source§impl Text
impl Text
Sourcepub fn set_encoding(&mut self, val: Encoding) -> &mut Self
pub fn set_encoding(&mut self, val: Encoding) -> &mut Self
Character encoding of the file.
Sourcepub fn set_format(&mut self, val: TextFormat) -> &mut Self
pub fn set_format(&mut self, val: TextFormat) -> &mut Self
Detected file format based on extension.
Sourcepub fn set_contents(&mut self, val: String) -> &mut Self
pub fn set_contents(&mut self, val: String) -> &mut Self
Decoded text contents.
Source§impl Text
impl Text
Sourcepub fn detect_encoding<R: ReadBytes>(data: &mut R) -> Result<Encoding>
pub fn detect_encoding<R: ReadBytes>(data: &mut R) -> Result<Encoding>
Detects the character encoding of text data.
Examines the data stream to determine its encoding by checking for Byte Order Marks (BOMs) and attempting to decode as different encodings.
§Detection Algorithm
- UTF-8 BOM: Checks for
0xEF 0xBB 0xBFat the start - UTF-16 LE BOM: Checks for
0xFF 0xFEat the start - UTF-8 without BOM: Attempts to decode entire file as UTF-8
- ISO-8859-1: Attempts to decode as ISO-8859-1
§Arguments
data- Reader positioned at the start of the text data
§Returns
The detected Encoding, or an error if no supported encoding matches.
§Errors
Returns RLibError::DecodingTextUnsupportedEncodingOrNotATextFile if:
- The data cannot be decoded as any supported encoding
- The file is not actually a text file
§Side Effects
After detection, the reader is repositioned:
- After the BOM if one was found
- At the start if no BOM was found
Trait Implementations§
Source§impl Decodeable for Text
impl Decodeable for Text
Source§impl<'de> Deserialize<'de> for Text
impl<'de> Deserialize<'de> for Text
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Encodeable for Text
impl Encodeable for Text
Source§fn encode<W: WriteBytes>(
&mut self,
buffer: &mut W,
_extra_data: &Option<EncodeableExtraData<'_>>,
) -> Result<()>
fn encode<W: WriteBytes>( &mut self, buffer: &mut W, _extra_data: &Option<EncodeableExtraData<'_>>, ) -> Result<()>
impl Eq for Text
impl StructuralPartialEq for Text
Auto Trait Implementations§
impl Freeze for Text
impl RefUnwindSafe for Text
impl Send for Text
impl Sync for Text
impl Unpin for Text
impl UnsafeUnpin for Text
impl UnwindSafe for Text
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.