#[non_exhaustive]pub struct FileHandler;Expand description
Handler for file I/O operations (JSON and CSV)
Implementations§
Source§impl FileHandler
impl FileHandler
Sourcepub fn save<T: Serialize>(
data: &[T],
format: &FileFormat,
filename_prefix: &str,
) -> Result<String>
pub fn save<T: Serialize>( data: &[T], format: &FileFormat, filename_prefix: &str, ) -> Result<String>
Save data to a file in the data directory.
Files are saved to the data/ directory (created if it doesn’t exist) with a timestamp in the filename.
§Arguments
data- Data to save (must implement Serialize)format- File format to save as (FileFormat::Jsonfor JSON orFileFormat::Csvfor CSV)filename_prefix- Prefix for the filename. The final filename will be{prefix}_{timestamp}.{extension}
§Errors
std::io::Error- If the file cannot be opened or written to, or if the data directory cannot be createdserde_json::Error- If the JSON cannot be serialized
§Returns
Result<String>- Full path to the saved file (e.g.,data/recent_tracks_20240101_120000.json)
Sourcepub fn load_ndjson<T: DeserializeOwned>(file_path: &str) -> Result<Vec<T>>
pub fn load_ndjson<T: DeserializeOwned>(file_path: &str) -> Result<Vec<T>>
Sourcepub fn append_or_create_ndjson<T: Serialize>(
new_data: &[T],
file_path: &str,
) -> Result<()>
pub fn append_or_create_ndjson<T: Serialize>( new_data: &[T], file_path: &str, ) -> Result<()>
Sourcepub fn sidecar_path(file_path: &str) -> String
pub fn sidecar_path(file_path: &str) -> String
Return the path of the sidecar metadata file for file_path.
The sidecar stores the latest known Unix timestamp so subsequent update calls do not need to deserialize the full data file.
Sourcepub fn read_sidecar_timestamp(file_path: &str) -> Option<u32>
pub fn read_sidecar_timestamp(file_path: &str) -> Option<u32>
Read the latest timestamp from a sidecar metadata file.
Returns None if the sidecar does not exist or cannot be parsed.
Sourcepub fn write_sidecar_timestamp(file_path: &str, timestamp: u32) -> Result<()>
pub fn write_sidecar_timestamp(file_path: &str, timestamp: u32) -> Result<()>
Write a timestamp to the sidecar metadata file associated with file_path.
§Errors
std::io::Error- If the sidecar file cannot be written
Sourcepub fn append_or_create_csv<T: Serialize>(
new_data: &[T],
file_path: &str,
) -> Result<()>
pub fn append_or_create_csv<T: Serialize>( new_data: &[T], file_path: &str, ) -> Result<()>
Append new items to an existing CSV file, or create it with headers if it does not exist.
When appending to an existing file the header row is omitted so it is not duplicated.
§Arguments
new_data- New items to appendfile_path- Path to the target CSV file
§Errors
std::io::Error- If the file cannot be opened or writtencsv::Error- If serialization fails
Sourcepub fn prepend_json<T: Serialize + DeserializeOwned + Clone>(
new_data: &[T],
file_path: &str,
) -> Result<()>
pub fn prepend_json<T: Serialize + DeserializeOwned + Clone>( new_data: &[T], file_path: &str, ) -> Result<()>
Prepend new items to an existing JSON file, or create the file if it does not exist.
New items are placed before existing items so the result remains sorted newest-first, which matches the order returned by the Last.fm API.
§Arguments
new_data- New items to prependfile_path- Path to the target JSON file
§Errors
std::io::Error- If the file cannot be read or writtenserde_json::Error- If serialization or deserialization fails