Task 4: The System Inspector
"Reading the text is simple. Reading the system attributes is where engineering begins."
In standard C/C++, accessing file metadata (size, permissions, timestamps) involves using the stat family of structs and system calls, which can vary significantly between Windows and Linux/macOS.
Rust abstracts this via std::fs::metadata, but introduces a new challenge: Data Serialization.
Your Mission: Add a "File Info" panel that displays system-level metadata for the currently open file.
1. The Concept: Struct Serialization​
To send a complex C-style struct from Rust to a JavaScript frontend, we cannot simply pass the memory address. We must serialize the data into a format the frontend understands (JSON).
Rust uses a standard crate for this called Serde (SERializer/DEserializer).
- Open
Cargo.tomland addserdeto your dependencies:[dependencies]
serde = { version = "1.0", features = ["derive"] }
# ... existing dependencies
2. Backend Challenge (Rust)​
You need to query the filesystem for metadata and map it to a struct.
Requirements:
- Define a Rust
structrepresenting the file stats. - Derive
Serializeso Tauri can convert it to JSON automatically. - Use
std::fs::metadatato populate the data.
Starter Code:
use std::fs;
use serde::Serialize;
// This struct will be converted to a JSON object automatically
#[derive(Serialize)]
struct FileStats {
size_bytes: u64,
is_readonly: bool,
// Challenge: 'modified' returns SystemTime, which is not directly serializable to JSON.
// You must figure out how to convert SystemTime to a UNIX timestamp (u64) or String.
last_modified: u64,
}
#[tauri::command]
fn get_file_stats(path: String) -> Result<FileStats, String> {
// TODO:
// 1. Call fs::metadata(&path)
// 2. Handle the Result (match or ?)
// 3. Map the Metadata struct to your FileStats struct
}
3. Frontend Challenge (TypeScript)​
- Create a "Properties" view or a footer bar in your editor.
- When a file is active, invoke
get_file_stats. - Display the size.
- Math Challenge: Convert the raw bytes into human-readable units (KB, MB, GB).
4. Deep Dive: The Time Problem​
In C++, time_t is usually just an integer. In Rust, SystemTime is an opaque type designed to be platform-independent.
- The Hurdle: You cannot send
SystemTimedirectly to JS. - The Hint: Look for
duration_since(UNIX_EPOCH)in the Rust documentation to convert the time object into a simple number that JavaScript'snew Date()can parse.
Conclusion Task 4​
Completing this task demonstrates mastery of:
- System Abstraction: Using
std::fsto normalize OS-specific file attributes. - FFI (Foreign Function Interface): Marshalling complex data structures between Rust and JS via Serde.
"You now control not just the content, but the container." 🦀✨