pub struct WarcWriter<W> { /* private fields */ }Expand description
A writer which writes records to an output stream.
Implementations§
Source§impl<W: Write> WarcWriter<W>
impl<W: Write> WarcWriter<W>
Sourcepub fn write(&mut self, record: &Record<BufferedBody>) -> Result<usize>
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}Sourcepub fn write_raw<B>(
&mut self,
headers: RawRecordHeader,
body: &B,
) -> Result<usize>
pub fn write_raw<B>( &mut self, headers: RawRecordHeader, body: &B, ) -> Result<usize>
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
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>>
impl<W: Write> WarcWriter<BufWriter<W>>
Sourcepub fn into_inner(self) -> Result<W, IntoInnerError<BufWriter<W>>>
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>>
impl WarcWriter<BufWriter<File>>
Sourcepub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self>
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
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>>>
impl WarcWriter<BufWriter<Encoder<File>>>
Sourcepub fn from_path_gzip<P: AsRef<Path>>(path: P) -> Result<Self>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more