Skip to main content

FileDestination

Struct FileDestination 

Source
pub struct FileDestination { /* private fields */ }
Expand description

Destination implementation for writing JSON data to a file File output destination writing to a buffered file on disk (32KB write buffer).

Implementations§

Source§

impl FileDestination

Source

pub fn new(path: impl AsRef<Path>) -> Result<FileDestination, Error>

Creates or opens a file for writing with 32KB buffer.

Examples found in repository?
examples/json_fibonacci.rs (line 70)
68fn write_sequence(file_path: &Path, sequence: &Node) -> Result<(), String> {
69    // Create a new file destination, falling back to empty string if the path is invalid
70    let file = FileDestination::new(file_path.to_str().unwrap_or(""));
71    match file {
72        Ok(mut f) => {
73            stringify(&sequence, &mut f).unwrap();
74            Ok(())
75        }
76        Err(e) => Err(e.to_string()),
77    }
78}
More examples
Hide additional examples
examples/json_strip_whitespace.rs (lines 11-16)
9fn process_json_file(file_path: &str) -> Result<(), String> {
10    let mut source = FileSource::new(file_path).map_err(|e| e.to_string())?;
11    let mut destination = FileDestination::new(
12        Path::new(file_path)
13            .with_extension("json_stripped")
14            .to_string_lossy()
15            .as_ref(),
16    )
17    .map_err(|e| e.to_string())?;
18
19    strip_whitespace(&mut source, &mut destination);
20    Ok(())
21}
examples/json_to_toml.rs (lines 13-18)
10fn process_json_file(file_path: &str) -> Result<(), String> {
11    let mut source = FileSource::new(file_path).map_err(|e| e.to_string())?;
12    let node = parse(&mut source).map_err(|e| e.to_string())?;
13    let mut destination = FileDestination::new(
14        Path::new(file_path)
15            .with_extension("toml")
16            .to_string_lossy()
17            .as_ref(),
18    )
19    .map_err(|e| e.to_string())?;
20
21    to_toml(&node, &mut destination)?;
22    Ok(())
23}
examples/json_pretty_print.rs (lines 13-18)
10fn process_json_file(file_path: &str) -> Result<(), String> {
11    let mut source = FileSource::new(file_path).map_err(|e| e.to_string())?;
12    let node = parse(&mut source).map_err(|e| e.to_string())?;
13    let mut destination = FileDestination::new(
14        Path::new(file_path)
15            .with_extension("json_pp")
16            .to_string_lossy()
17            .as_ref(),
18    )
19    .map_err(|e| e.to_string())?;
20
21    print(&node, &mut destination, 4);
22    Ok(())
23}
examples/json_to_bencode.rs (lines 12-17)
9fn process_json_file(file_path: &str) -> Result<(), String> {
10    let mut source = FileSource::new(file_path).map_err(|e| e.to_string())?;
11    let node = parse(&mut source).map_err(|e| e.to_string())?;
12    let mut destination = FileDestination::new(
13        Path::new(file_path)
14            .with_extension("bencode")
15            .to_string_lossy()
16            .as_ref(),
17    )
18    .map_err(|e| e.to_string())?;
19
20    to_bencode(&node, &mut destination)?;
21    Ok(())
22}
examples/json_to_xml.rs (lines 12-17)
9fn process_json_file(file_path: &str) -> Result<(), String> {
10    let mut source = FileSource::new(file_path).map_err(|e| e.to_string())?;
11    let node = parse(&mut source).map_err(|e| e.to_string())?;
12    let mut destination = FileDestination::new(
13        Path::new(file_path)
14            .with_extension("xml")
15            .to_string_lossy()
16            .as_ref(),
17    )
18    .map_err(|e| e.to_string())?;
19
20    to_xml(&node, &mut destination).map_err(|e| e.to_string())?;
21    Ok(())
22}
Source

pub fn create(path: impl AsRef<Path>) -> Result<FileDestination, Error>

Creates or opens a file for writing with 32KB buffer.

Source

pub fn add_byte(&mut self, byte: u8)

Writes a single byte to the buffered destination.

Source

pub fn add_bytes(&mut self, bytes: &str)

Writes multiple bytes from a string slice to the buffered destination.

Source

pub fn clear(&mut self)

Clears all content from the file.

Source

pub fn last(&self) -> Option<u8>

Returns the last written byte.

Source

pub fn file_length(&self) -> usize

Returns the number of bytes written.

Source

pub fn file_name(&self) -> &str

Returns the file path string.

Source

pub fn flush(&mut self) -> Result<(), Error>

Flushes the underlying buffered writer and syncs file data.

Source

pub fn close(&self) -> Result<(), Error>

Closes the file handle (auto-flushed on drop).

Source

pub fn write_all_bytes(&mut self, data: &[u8]) -> Result<(), Error>

Writes all bytes to the buffered file.

Source

pub fn write_str(&mut self, data: &str) -> Result<(), Error>

Writes a string slice to the buffered file.

Trait Implementations§

Source§

impl Drop for FileDestination

Available on crate feature file-io only.
Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl IByteWriter for FileDestination

Available on crate feature file-io only.
Source§

fn write_byte(&mut self, byte: u8) -> Result<(), ErrorCode>

Writes a single byte to the destination.
Source§

fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), ErrorCode>

Writes multiple bytes from a raw byte slice to the destination.
Source§

impl IClearable for FileDestination

Available on crate feature file-io only.
Source§

fn clear(&mut self)

Clears all accumulated content from the destination.
Source§

impl IDestination for FileDestination

Available on crate feature file-io only.
Source§

fn add_byte(&mut self, byte: u8)

Writes a single byte to the destination.
Source§

fn add_bytes(&mut self, bytes: &str)

Writes multiple bytes from a string slice.
Source§

fn clear(&mut self)

Clears all accumulated content from the destination (default: no-op for forward-only streaming destinations).
Source§

fn last(&self) -> Option<u8>

Returns the last written byte, if tracked (default: None).
Source§

fn add_raw_bytes(&mut self, bytes: &[u8])

Writes multiple raw bytes from a byte slice.
Source§

impl IFlushable for FileDestination

Available on crate feature file-io only.
Source§

fn flush(&mut self) -> Result<(), ErrorCode>

Flushes any buffered bytes to destination.
Source§

impl ITailInspectable for FileDestination

Available on crate feature file-io only.
Source§

fn last_byte(&self) -> Option<u8>

Returns the last written byte, if any.

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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.