Skip to main content

FileHandler

Struct FileHandler 

Source
#[non_exhaustive]
pub struct FileHandler;
Expand description

Handler for file I/O operations (JSON and CSV)

Implementations§

Source§

impl FileHandler

Source

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::Json for JSON or FileFormat::Csv for 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 created
  • serde_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)
Source

pub fn load_ndjson<T: DeserializeOwned>(file_path: &str) -> Result<Vec<T>>

Load existing NDJSON data from a file - one item per line.

§Arguments
  • file_path - Path to the NDJSON file to read
§Errors
  • std::io::Error - If the file cannot be opened
  • serde_json::Error - If a line cannot be deserialized into T
Source

pub fn append_or_create_ndjson<T: Serialize>( new_data: &[T], file_path: &str, ) -> Result<()>

Append new items to an existing NDJSON file, or create it if it does not exist.

§Arguments
  • new_data - New items to append
  • file_path - Path to the target NDJSON file
§Errors
  • std::io::Error - If the file cannot be opened or written
Source

pub fn save_single<T: Serialize>(data: &T, filename: &str) -> Result<()>

Save a single item to a JSON file

§Errors
  • std::io::Error - If there was an error reading or writing the file
  • serde_json::Error - If there was an error serializing the data
§Arguments
  • data - Data to save
  • filename - Filename to save as
Source

pub fn load<T: DeserializeOwned>(file_path: &str) -> Result<Vec<T>>

Load existing JSON data from a file.

§Arguments
  • file_path - Path to the JSON file to read
§Errors
  • std::io::Error - If the file cannot be opened
  • serde_json::Error - If the JSON cannot be deserialized into Vec<T>
Source

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.

Source

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.

Source

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
Source

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 append
  • file_path - Path to the target CSV file
§Errors
  • std::io::Error - If the file cannot be opened or written
  • csv::Error - If serialization fails
Source

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 prepend
  • file_path - Path to the target JSON file
§Errors
  • std::io::Error - If the file cannot be read or written
  • serde_json::Error - If serialization or deserialization fails

Trait Implementations§

Source§

impl Debug for FileHandler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more