Skip to content

Commit

Permalink
update original
Browse files Browse the repository at this point in the history
  • Loading branch information
funkill committed Nov 29, 2024
1 parent 275553c commit 209d89b
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 55 deletions.
4 changes: 2 additions & 2 deletions rust-cookbook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ walkdir = "2.0"
syslog = "5.0"

[build-dependencies]
skeptic = { git = 'https://github.com/andygauge/rust-skeptic'}
skeptic = "0.13"
walkdir = "2.0"

[dev-dependencies]
skeptic = { git = 'https://github.com/andygauge/rust-skeptic'}
skeptic = "0.13"
walkdir = "2.0"
2 changes: 1 addition & 1 deletion rust-cookbook/src/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ work.

For the sake of readability error handling boilerplate is hidden by
default like below. In order to read full contents click on the
"expand" (<i class="fa fa-expand"></i>) button located in the top
"Show hidden lines" (<i class="fa fa-eye"></i>) button located in the top
right corner of the snippet.

```rust,edition2018
Expand Down
26 changes: 25 additions & 1 deletion rust-cookbook/src/compression/tar/tar-compress.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Compress `/var/log` directory into `archive.tar.gz`.

Creates a [`File`] wrapped in [`GzEncoder`]
and [`tar::Builder`]. </br>Adds contents of `/var/log` directory recursively into the archive
under `backup/logs`path with [`Builder::append_dir_all`].
under `backup/logs` path with [`Builder::append_dir_all`].
[`GzEncoder`] is responsible for transparently compressing the
data prior to writing it into `archive.tar.gz`.

Expand All @@ -21,11 +21,35 @@ fn main() -> Result<(), std::io::Error> {
let enc = GzEncoder::new(tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
tar.append_dir_all("backup/logs", "/var/log")?;
tar.finish()?;
Ok(())
}
```

To add the contents without renaming them, an empty string can be used as the first argument of [`Builder::append_dir_all`]:

```rust,edition2018,no_run
use std::fs::File;
use flate2::Compression;
use flate2::write::GzEncoder;
fn main() -> Result<(), std::io::Error> {
let tar_gz = File::create("archive.tar.gz")?;
let enc = GzEncoder::new(tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
tar.append_dir_all("", "/var/log")?;
tar.finish()?;
Ok(())
}
```

The default behavior of [`tar::Builder`] differs from the GNU `tar` utility's defaults [tar(1)],
notably [`tar::Builder::follow_symlinks(true)`] is the equivalent of `tar --dereference`.

[tar(1)]: https://man7.org/linux/man-pages/man1/tar.1.html
[`Builder::append_dir_all`]: https://docs.rs/tar/*/tar/struct.Builder.html#method.append_dir_all
[`File`]: https://doc.rust-lang.org/std/fs/struct.File.html
[`GzEncoder`]: https://docs.rs/flate2/*/flate2/write/struct.GzEncoder.html
[`tar::Builder`]: https://docs.rs/tar/*/tar/struct.Builder.html
[`tar::Builder::follow_symlinks(true)`]: https://docs.rs/tar/latest/tar/struct.Builder.html#method.follow_symlinks
4 changes: 2 additions & 2 deletions rust-cookbook/src/compression/tar/tar-strip-prefix.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use tar::Archive;
# }
# }
fn main() -> Result<()> {
fn main() -> Result<(), std::io::Error> {
let file = File::open("archive.tar.gz")?;
let mut archive = Archive::new(GzDecoder::new(file));
let prefix = "bundle/logs";
Expand All @@ -29,7 +29,7 @@ fn main() -> Result<()> {
archive
.entries()?
.filter_map(|e| e.ok())
.map(|mut entry| -> Result<PathBuf> {
.map(|mut entry| -> Result<PathBuf, Box<dyn std::error::Error>> {
let path = entry.path()?.strip_prefix(prefix)?.to_owned();
entry.unpack(&path)?;
Ok(path)
Expand Down
2 changes: 1 addition & 1 deletion rust-cookbook/src/data_structures/bitfield.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Custom
# Bitfield

{{#include bitfield/bitfield.md}}

Expand Down
47 changes: 34 additions & 13 deletions rust-cookbook/src/development_tools.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,52 @@
# Development Tools

{{#include development_tools/debugging.md}}
## Debugging

| Recipe | Crates | Categories |
| -------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | --------------------------------------- |
| [Log a debug message to the console][ex-log-debug] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Log an error message to the console][ex-log-error] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Log to stdout instead of stderr][ex-log-stdout] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Log messages with a custom logger][ex-log-custom-logger] | [![log-badge]][log] | [![cat-debugging-badge]][cat-debugging] |
| [Log to the Unix syslog][ex-log-syslog] | [![log-badge]][log] [![syslog-badge]][syslog] | [![cat-debugging-badge]][cat-debugging] |
| [Enable log levels per module][ex-log-mod] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Use a custom environment variable to set up logging][ex-log-env-variable] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Include timestamp in log messages][ex-log-timestamp] | [![log-badge]][log] [![env_logger-badge]][env_logger] [![chrono-badge]][chrono] | [![cat-debugging-badge]][cat-debugging] |
| [Log messages to a custom location][ex-log-custom] | [![log-badge]][log] [![log4rs-badge]][log4rs] | [![cat-debugging-badge]][cat-debugging] |

[ex-log-debug]: development_tools/debugging/log.html#log-a-debug-message-to-the-console
[ex-log-error]: development_tools/debugging/log.html#log-an-error-message-to-the-console
[ex-log-stdout]: development_tools/debugging/log.html#log-to-stdout-instead-of-stderr
[ex-log-custom-logger]: development_tools/debugging/log.html#log-messages-with-a-custom-logger
[ex-log-syslog]: development_tools/debugging/log.html#log-to-the-unix-syslog
[ex-log-mod]: development_tools/debugging/config_log.html#enable-log-levels-per-module
[ex-log-env-variable]: development_tools/debugging/config_log.html#use-a-custom-environment-variable-to-set-up-logging
[ex-log-timestamp]: development_tools/debugging/config_log.html#include-timestamp-in-log-messages
[ex-log-custom]: development_tools/debugging/config_log.html#log-messages-to-a-custom-location

## Versioning

| Recipe | Crates | Categories |
|--------|--------|------------|
| [Parse and increment a version string][ex-semver-increment] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] |
| [Parse a complex version string][ex-semver-complex] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] |
| [Check if given version is pre-release][ex-semver-prerelease] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] |
| [Find the latest version satisfying given range][ex-semver-latest] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] |
| [Check external command version for compatibility][ex-semver-command] | [![semver-badge]][semver] | [![cat-text-processing-badge]][cat-text-processing] [![cat-os-badge]][cat-os]
| Recipe | Crates | Categories |
| --------------------------------------------------------------------- | ------------------------- | ----------------------------------------------------------------------------- |
| [Parse and increment a version string][ex-semver-increment] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] |
| [Parse a complex version string][ex-semver-complex] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] |
| [Check if given version is pre-release][ex-semver-prerelease] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] |
| [Find the latest version satisfying given range][ex-semver-latest] | [![semver-badge]][semver] | [![cat-config-badge]][cat-config] |
| [Check external command version for compatibility][ex-semver-command] | [![semver-badge]][semver] | [![cat-text-processing-badge]][cat-text-processing] [![cat-os-badge]][cat-os] |

## Build Time

| Recipe | Crates | Categories |
|--------|--------|------------|
| [Compile and link statically to a bundled C library][ex-cc-static-bundled] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
| Recipe | Crates | Categories |
| -------------------------------------------------------------------------------- | ----------------- | ------------------------------------------------------- |
| [Compile and link statically to a bundled C library][ex-cc-static-bundled] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
| [Compile and link statically to a bundled C++ library][ex-cc-static-bundled-cpp] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
| [Compile a C library while setting custom defines][ex-cc-custom-defines] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
| [Compile a C library while setting custom defines][ex-cc-custom-defines] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |

[ex-semver-increment]: development_tools/versioning.html#parse-and-increment-a-version-string
[ex-semver-complex]: development_tools/versioning.html#parse-a-complex-version-string
[ex-semver-prerelease]: development_tools/versioning.html#check-if-given-version-is-pre-release
[ex-semver-latest]: development_tools/versioning.html#find-the-latest-version-satisfying-given-range
[ex-semver-command]: development_tools/versioning.html#check-external-command-version-for-compatibility

[ex-cc-static-bundled]: development_tools/build_tools.html#compile-and-link-statically-to-a-bundled-c-library
[ex-cc-static-bundled-cpp]: development_tools/build_tools.html#compile-and-link-statically-to-a-bundled-c-library-1
[ex-cc-custom-defines]: development_tools/build_tools.html#compile-a-c-library-while-setting-custom-defines
Expand Down
40 changes: 20 additions & 20 deletions rust-cookbook/src/development_tools/debugging.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
## Debugging

| Recipe | Crates | Categories |
|--------|--------|------------|
| [Log a debug message to the console][ex-log-debug] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Log an error message to the console][ex-log-error] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Log to stdout instead of stderr][ex-log-stdout] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Log messages with a custom logger][ex-log-custom-logger] | [![log-badge]][log] | [![cat-debugging-badge]][cat-debugging] |
| [Log to the Unix syslog][ex-log-syslog] | [![log-badge]][log] [![syslog-badge]][syslog] | [![cat-debugging-badge]][cat-debugging] |
| [Enable log levels per module][ex-log-mod] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Use a custom environment variable to set up logging][ex-log-env-variable] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Include timestamp in log messages][ex-log-timestamp] | [![log-badge]][log] [![env_logger-badge]][env_logger] [![chrono-badge]][chrono] | [![cat-debugging-badge]][cat-debugging] |
| [Log messages to a custom location][ex-log-custom] | [![log-badge]][log] [![log4rs-badge]][log4rs] | [![cat-debugging-badge]][cat-debugging] |
| Recipe | Crates | Categories |
| -------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | --------------------------------------- |
| [Log a debug message to the console][ex-log-debug] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Log an error message to the console][ex-log-error] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Log to stdout instead of stderr][ex-log-stdout] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Log messages with a custom logger][ex-log-custom-logger] | [![log-badge]][log] | [![cat-debugging-badge]][cat-debugging] |
| [Log to the Unix syslog][ex-log-syslog] | [![log-badge]][log] [![syslog-badge]][syslog] | [![cat-debugging-badge]][cat-debugging] |
| [Enable log levels per module][ex-log-mod] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Use a custom environment variable to set up logging][ex-log-env-variable] | [![log-badge]][log] [![env_logger-badge]][env_logger] | [![cat-debugging-badge]][cat-debugging] |
| [Include timestamp in log messages][ex-log-timestamp] | [![log-badge]][log] [![env_logger-badge]][env_logger] [![chrono-badge]][chrono] | [![cat-debugging-badge]][cat-debugging] |
| [Log messages to a custom location][ex-log-custom] | [![log-badge]][log] [![log4rs-badge]][log4rs] | [![cat-debugging-badge]][cat-debugging] |

[ex-log-debug]: development_tools/debugging/log.html#log-a-debug-message-to-the-console
[ex-log-error]: development_tools/debugging/log.html#log-an-error-message-to-the-console
[ex-log-stdout]: development_tools/debugging/log.html#log-to-stdout-instead-of-stderr
[ex-log-custom-logger]: development_tools/debugging/log.html#log-messages-with-a-custom-logger
[ex-log-syslog]: development_tools/debugging/log.html#log-to-the-unix-syslog
[ex-log-mod]: development_tools/debugging/config_log.html#enable-log-levels-per-module
[ex-log-env-variable]: development_tools/debugging/config_log.html#use-a-custom-environment-variable-to-set-up-logging
[ex-log-timestamp]: development_tools/debugging/config_log.html#include-timestamp-in-log-messages
[ex-log-custom]: development_tools/debugging/config_log.html#log-messages-to-a-custom-location
[ex-log-debug]: debugging/log.html#log-a-debug-message-to-the-console
[ex-log-error]: debugging/log.html#log-an-error-message-to-the-console
[ex-log-stdout]: debugging/log.html#log-to-stdout-instead-of-stderr
[ex-log-custom-logger]: debugging/log.html#log-messages-with-a-custom-logger
[ex-log-syslog]: debugging/log.html#log-to-the-unix-syslog
[ex-log-mod]: debugging/config_log.html#enable-log-levels-per-module
[ex-log-env-variable]: debugging/config_log.html#use-a-custom-environment-variable-to-set-up-logging
[ex-log-timestamp]: debugging/config_log.html#include-timestamp-in-log-messages
[ex-log-custom]: debugging/config_log.html#log-messages-to-a-custom-location

{{#include ../links.md}}
2 changes: 1 addition & 1 deletion rust-cookbook/src/encoding/string/hex.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ representation of the data.
Similarly, a `HEXUPPER::decode` method is provided which takes a `&[u8]` and
returns a `Vec<u8>` if the input data is successfully decoded.

The example below coverts `&[u8]` data to hexadecimal equivalent. Compares this
The example below converts `&[u8]` data to hexadecimal equivalent. Compares this
value to the expected value.

```rust,edition2018
Expand Down
6 changes: 3 additions & 3 deletions rust-cookbook/src/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Keep lines sorted.
[postgres]: https://docs.rs/postgres/0.15.2/postgres/
[rand-badge]: https://badge-cache.kominick.com/crates/v/rand.svg?label=rand
[rand]: https://docs.rs/rand/
[rand_distr-badge]: https://badge-cache.kominick.com/crates/v/rand.svg?label=rand_distr
[rand_distr-badge]: https://badge-cache.kominick.com/crates/v/rand_distr.svg?label=rand_distr
[rand_distr]: https://docs.rs/rand_distr/
[rayon-badge]: https://badge-cache.kominick.com/crates/v/rayon.svg?label=rayon
[rayon]: https://docs.rs/rayon/
Expand Down Expand Up @@ -133,8 +133,8 @@ Keep lines sorted.
[syslog]: https://docs.rs/syslog/
[tar-badge]: https://badge-cache.kominick.com/crates/v/tar.svg?label=tar
[tar]: https://docs.rs/tar/
[tempdir-badge]: https://badge-cache.kominick.com/crates/v/tempdir.svg?label=tempdir
[tempdir]: https://docs.rs/tempdir/
[tempfile-badge]: https://badge-cache.kominick.com/crates/v/tempfile.svg?label=tempfile
[tempfile]: https://docs.rs/tempfile/
[threadpool-badge]: https://badge-cache.kominick.com/crates/v/threadpool.svg?label=threadpool
[threadpool]: https://docs.rs/threadpool/
[toml-badge]: https://badge-cache.kominick.com/crates/v/toml.svg?label=toml
Expand Down
10 changes: 4 additions & 6 deletions rust-cookbook/src/net/server/listen-unused.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
[![std-badge]][std] [![cat-net-badge]][cat-net]

In this example, the port is displayed on the console, and the program will
listen until a request is made. `SocketAddrV4` assigns a random port when
setting port to 0.
listen until a request is made. `TcpListener::bind` uses a random port
allocated by the OS when requested to bind to port 0.

```rust,edition2018,no_run
use std::net::{SocketAddrV4, Ipv4Addr, TcpListener};
use std::net::TcpListener;
use std::io::{Read, Error};
fn main() -> Result<(), Error> {
let loopback = Ipv4Addr::new(127, 0, 0, 1);
let socket = SocketAddrV4::new(loopback, 0);
let listener = TcpListener::bind(socket)?;
let listener = TcpListener::bind("localhost:0")?;
let port = listener.local_addr()?;
println!("Listening on {}, access this port to end the program", port);
let (mut tcp_stream, addr) = listener.accept()?; //block until requested
Expand Down
Loading

0 comments on commit 209d89b

Please sign in to comment.