WarcWriter

Struct WarcWriter 

Source
pub struct WarcWriter<W> { /* private fields */ }
Expand description

A writer which writes records to an output stream.

Implementations§

Source§

impl<W: Write> WarcWriter<W>

Source

pub fn new(w: W) -> Self

Create a new writer.

Source

pub fn write(&mut self, record: &Record<BufferedBody>) -> Result<usize>

Write a single record.

The number of bytes written is returned upon success.

Examples found in repository?
examples/write_file.rs (line 20)
5fn main() -> Result<(), std::io::Error> {
6    let date = Utc::now();
7    let body = format!("wrote to the file on {}", date);
8    let body = body.into_bytes();
9
10    let mut headers = Record::<BufferedBody>::new();
11    headers.set_warc_type(RecordType::WarcInfo);
12    headers.set_date(date);
13    headers
14        .set_header(WarcHeader::IPAddress, "127.0.0.1")
15        .expect("BUG: should be a valid IP address");
16    let record = headers.add_body(body);
17
18    let mut file = WarcWriter::from_path("warc_example.warc")?;
19
20    let bytes_written = file.write(&record)?;
21
22    println!("{} bytes written.", bytes_written);
23
24    Ok(())
25}
Source

pub fn write_raw<B>( &mut self, headers: RawRecordHeader, body: &B, ) -> Result<usize>
where B: AsRef<[u8]>,

Write a single raw record.

The number of bytes written is returned upon success.

Examples found in repository?
examples/write_raw.rs (line 35)
6fn main() -> Result<(), std::io::Error> {
7    let date = Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true);
8    let body = format!("wrote to the file on {}", date);
9    let body = body.into_bytes();
10
11    let headers = RawRecordHeader {
12        version: "1.0".to_owned(),
13        headers: vec![
14            (
15                WarcHeader::RecordID,
16                Record::<BufferedBody>::generate_record_id().into_bytes(),
17            ),
18            (
19                WarcHeader::WarcType,
20                RecordType::WarcInfo.to_string().into_bytes(),
21            ),
22            (WarcHeader::Date, date.into_bytes()),
23            (WarcHeader::IPAddress, "127.0.0.1".to_owned().into_bytes()),
24            (
25                WarcHeader::ContentLength,
26                body.len().to_string().into_bytes(),
27            ),
28        ]
29        .into_iter()
30        .collect(),
31    };
32
33    let mut file = WarcWriter::from_path("warc_example.warc")?;
34
35    let bytes_written = file.write_raw(headers, &body)?;
36
37    println!("{} bytes written.", bytes_written);
38
39    Ok(())
40}
More examples
Hide additional examples
examples/write_gzip.rs (line 35)
6fn main() -> Result<(), std::io::Error> {
7    let date = Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true);
8    let body = format!("wrote to the file on {}", date);
9    let body = body.into_bytes();
10
11    let headers = RawRecordHeader {
12        version: "1.0".to_owned(),
13        headers: vec![
14            (
15                WarcHeader::RecordID,
16                Record::<BufferedBody>::generate_record_id().into_bytes(),
17            ),
18            (
19                WarcHeader::WarcType,
20                RecordType::WarcInfo.to_string().into_bytes(),
21            ),
22            (WarcHeader::Date, date.into_bytes()),
23            (WarcHeader::IPAddress, "127.0.0.1".to_owned().into_bytes()),
24            (
25                WarcHeader::ContentLength,
26                body.len().to_string().into_bytes(),
27            ),
28        ]
29        .into_iter()
30        .collect(),
31    };
32
33    let mut file = WarcWriter::from_path_gzip("warc_example.warc.gz")?;
34
35    let bytes_written = file.write_raw(headers, &body)?;
36
37    // NB: the compression stream must be finish()ed, or the file will be truncated
38    let gzip_stream = file.into_inner()?;
39    gzip_stream.finish().into_result()?;
40
41    println!("{} bytes written.", bytes_written);
42
43    Ok(())
44}
Source§

impl<W: Write> WarcWriter<BufWriter<W>>

Source

pub fn into_inner(self) -> Result<W, IntoInnerError<BufWriter<W>>>

Consume this writer and return the inner writer.

§Flushing Compressed Data Streams

This method is necessary to be called at the end of a GZIP-compressed stream. An extra call is needed to flush the buffer of data, and write a trailer to the output stream.

let gzip_stream = writer.into_inner()?;
gzip_writer.finish().into_result()?;
Examples found in repository?
examples/write_gzip.rs (line 38)
6fn main() -> Result<(), std::io::Error> {
7    let date = Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true);
8    let body = format!("wrote to the file on {}", date);
9    let body = body.into_bytes();
10
11    let headers = RawRecordHeader {
12        version: "1.0".to_owned(),
13        headers: vec![
14            (
15                WarcHeader::RecordID,
16                Record::<BufferedBody>::generate_record_id().into_bytes(),
17            ),
18            (
19                WarcHeader::WarcType,
20                RecordType::WarcInfo.to_string().into_bytes(),
21            ),
22            (WarcHeader::Date, date.into_bytes()),
23            (WarcHeader::IPAddress, "127.0.0.1".to_owned().into_bytes()),
24            (
25                WarcHeader::ContentLength,
26                body.len().to_string().into_bytes(),
27            ),
28        ]
29        .into_iter()
30        .collect(),
31    };
32
33    let mut file = WarcWriter::from_path_gzip("warc_example.warc.gz")?;
34
35    let bytes_written = file.write_raw(headers, &body)?;
36
37    // NB: the compression stream must be finish()ed, or the file will be truncated
38    let gzip_stream = file.into_inner()?;
39    gzip_stream.finish().into_result()?;
40
41    println!("{} bytes written.", bytes_written);
42
43    Ok(())
44}
Source§

impl WarcWriter<BufWriter<File>>

Source

pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self>

Create a new writer which writes to a file.

Examples found in repository?
examples/write_file.rs (line 18)
5fn main() -> Result<(), std::io::Error> {
6    let date = Utc::now();
7    let body = format!("wrote to the file on {}", date);
8    let body = body.into_bytes();
9
10    let mut headers = Record::<BufferedBody>::new();
11    headers.set_warc_type(RecordType::WarcInfo);
12    headers.set_date(date);
13    headers
14        .set_header(WarcHeader::IPAddress, "127.0.0.1")
15        .expect("BUG: should be a valid IP address");
16    let record = headers.add_body(body);
17
18    let mut file = WarcWriter::from_path("warc_example.warc")?;
19
20    let bytes_written = file.write(&record)?;
21
22    println!("{} bytes written.", bytes_written);
23
24    Ok(())
25}
More examples
Hide additional examples
examples/write_raw.rs (line 33)
6fn main() -> Result<(), std::io::Error> {
7    let date = Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true);
8    let body = format!("wrote to the file on {}", date);
9    let body = body.into_bytes();
10
11    let headers = RawRecordHeader {
12        version: "1.0".to_owned(),
13        headers: vec![
14            (
15                WarcHeader::RecordID,
16                Record::<BufferedBody>::generate_record_id().into_bytes(),
17            ),
18            (
19                WarcHeader::WarcType,
20                RecordType::WarcInfo.to_string().into_bytes(),
21            ),
22            (WarcHeader::Date, date.into_bytes()),
23            (WarcHeader::IPAddress, "127.0.0.1".to_owned().into_bytes()),
24            (
25                WarcHeader::ContentLength,
26                body.len().to_string().into_bytes(),
27            ),
28        ]
29        .into_iter()
30        .collect(),
31    };
32
33    let mut file = WarcWriter::from_path("warc_example.warc")?;
34
35    let bytes_written = file.write_raw(headers, &body)?;
36
37    println!("{} bytes written.", bytes_written);
38
39    Ok(())
40}
Source§

impl WarcWriter<BufWriter<Encoder<File>>>

Source

pub fn from_path_gzip<P: AsRef<Path>>(path: P) -> Result<Self>

Create a new writer which writes to a GZIP-compressed file.

Examples found in repository?
examples/write_gzip.rs (line 33)
6fn main() -> Result<(), std::io::Error> {
7    let date = Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true);
8    let body = format!("wrote to the file on {}", date);
9    let body = body.into_bytes();
10
11    let headers = RawRecordHeader {
12        version: "1.0".to_owned(),
13        headers: vec![
14            (
15                WarcHeader::RecordID,
16                Record::<BufferedBody>::generate_record_id().into_bytes(),
17            ),
18            (
19                WarcHeader::WarcType,
20                RecordType::WarcInfo.to_string().into_bytes(),
21            ),
22            (WarcHeader::Date, date.into_bytes()),
23            (WarcHeader::IPAddress, "127.0.0.1".to_owned().into_bytes()),
24            (
25                WarcHeader::ContentLength,
26                body.len().to_string().into_bytes(),
27            ),
28        ]
29        .into_iter()
30        .collect(),
31    };
32
33    let mut file = WarcWriter::from_path_gzip("warc_example.warc.gz")?;
34
35    let bytes_written = file.write_raw(headers, &body)?;
36
37    // NB: the compression stream must be finish()ed, or the file will be truncated
38    let gzip_stream = file.into_inner()?;
39    gzip_stream.finish().into_result()?;
40
41    println!("{} bytes written.", bytes_written);
42
43    Ok(())
44}

Auto Trait Implementations§

§

impl<W> Freeze for WarcWriter<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for WarcWriter<W>
where W: RefUnwindSafe,

§

impl<W> Send for WarcWriter<W>
where W: Send,

§

impl<W> Sync for WarcWriter<W>
where W: Sync,

§

impl<W> Unpin for WarcWriter<W>
where W: Unpin,

§

impl<W> UnwindSafe for WarcWriter<W>
where W: UnwindSafe,

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 = 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.