Struct retdec::file::File
[−]
[src]
pub struct File { /* fields omitted */ }
In-memory representation of a file.
Only the name and content of a file are accessible. Path to the file is not stored.
Examples
use std::path::Path; use retdec::file::File; let file = File::from_path("tests/file.exe")?; assert_eq!(file.name(), "file.exe"); let saved_file_path = file.save_into("another_dir")?; assert_eq!(saved_file_path, Path::new("another_dir/file.exe"));
Methods
impl File
[src]
fn from_path<P>(path: P) -> Result<File> where
P: AsRef<Path>,
P: AsRef<Path>,
Creates a file from the given path.
The name is detected automatically from the path.
Examples
use retdec::file::File; let file = File::from_path("tests/file.exe")?; assert_eq!(file.name(), "file.exe");
fn from_path_with_custom_name<P, N>(path: P, name: N) -> Result<File> where
P: AsRef<Path>,
N: Into<String>,
P: AsRef<Path>,
N: Into<String>,
Creates a file from the given path but gives it a custom name.
Examples
use retdec::file::File; let file = File::from_path_with_custom_name("tests/file.exe", "other.exe")?; assert_eq!(file.name(), "other.exe");
fn from_content_with_name<N>(content: &[u8], name: N) -> File where
N: Into<String>,
N: Into<String>,
Creates a file with the given content and name.
Examples
use retdec::file::File; let file = File::from_content_with_name(b"content", "file.txt"); assert_eq!(file.content(), b"content"); assert_eq!(file.name(), "file.txt");
fn content(&self) -> &[u8]
Returns the raw content of the file.
Examples
use retdec::file::File; let file = File::from_content_with_name(b"content", "file.txt"); assert_eq!(file.content(), b"content");
fn content_as_text(&self) -> Result<&str>
Returns the content of the file as text.
The content is expected to be encoded as UTF-8, which is the encoding
that retdec.com
's API uses.
Examples
use retdec::file::File; let file = File::from_content_with_name(b"content", "file.txt"); assert_eq!(file.content_as_text()?, "content");
fn content_len(&self) -> usize
Returns the number of bytes in the content.
Examples
use retdec::file::File; let file = File::from_content_with_name(b"content", "file.txt"); assert_eq!(file.content_len(), 7);
fn name(&self) -> &str
Returns the name of the file.
Examples
use retdec::file::File; let file = File::from_content_with_name(b"content", "file.txt"); assert_eq!(file.name(), "file.txt");
fn safe_name(&self) -> String
Returns a modified version of the file's name that can be passed to the retdec.com's API.
Examples
use retdec::file::File; let file = File::from_content_with_name(b"content", "jalapeño.txt"); assert_eq!(file.safe_name(), "jalapeno.txt");
fn save_into<P>(&self, dir: P) -> Result<PathBuf> where
P: AsRef<Path>,
P: AsRef<Path>,
Stores a copy of the file into the given directory.
Returns a path to the saved file.
Examples
use std::path::Path; use retdec::file::File; let file = File::from_path("tests/file.exe")?; let saved_file_path = file.save_into("another_dir")?; assert_eq!(saved_file_path, Path::new("another_dir/file.exe"));
fn save_into_under_name<P>(&self, dir: P, name: &str) -> Result<PathBuf> where
P: AsRef<Path>,
P: AsRef<Path>,
Stores a copy of the file into the given directory under a custom name.
Returns a path to the saved file.
Examples
use std::path::Path; use retdec::file::File; let file = File::from_path("tests/file.exe")?; let saved_file_path = file.save_into_under_name("another_dir", "test.exe")?; assert_eq!(saved_file_path, Path::new("another_dir/test.exe"));
fn save_as<P>(&self, path: P) -> Result<()> where
P: AsRef<Path>,
P: AsRef<Path>,
Stores a copy of the file into the given path.
The path is expected to be a file path. If you want to store the file
into a directory, use either save_into()
or save_into_under_name()
.
Examples
use retdec::file::File; let file = File::from_path("tests/file.exe")?; file.save_as("another_dir/test.exe")?;
Trait Implementations
impl Clone for File
[src]
fn clone(&self) -> File
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more