mirror of
https://github.com/aramperes/nut-rs.git
synced 2025-09-08 21:18:31 -04:00
Update readme example
This commit is contained in:
parent
09c964e8fc
commit
36202d9f1f
1 changed files with 9 additions and 9 deletions
18
README.md
18
README.md
|
@ -38,25 +38,25 @@ Below is a sample program using this library (`cargo run --example blocking`).
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::net::ToSocketAddrs;
|
|
||||||
|
|
||||||
use nut_client::blocking::Connection;
|
use nut_client::blocking::Connection;
|
||||||
use nut_client::{Auth, ConfigBuilder, Host};
|
use nut_client::{Auth, ConfigBuilder};
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
fn main() -> nut_client::Result<()> {
|
fn main() -> nut_client::Result<()> {
|
||||||
let addr = env::var("NUT_ADDR")
|
let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
|
||||||
.unwrap_or_else(|_| "localhost:3493".into())
|
let port = env::var("NUT_PORT")
|
||||||
.to_socket_addrs()
|
.ok()
|
||||||
.unwrap()
|
.map(|s| s.parse::<u16>().ok())
|
||||||
.next()
|
.flatten()
|
||||||
.unwrap();
|
.unwrap_or(3493);
|
||||||
|
|
||||||
let username = env::var("NUT_USER").ok();
|
let username = env::var("NUT_USER").ok();
|
||||||
let password = env::var("NUT_PASSWORD").ok();
|
let password = env::var("NUT_PASSWORD").ok();
|
||||||
let auth = username.map(|username| Auth::new(username, password));
|
let auth = username.map(|username| Auth::new(username, password));
|
||||||
|
|
||||||
let config = ConfigBuilder::new()
|
let config = ConfigBuilder::new()
|
||||||
.with_host(Host::Tcp(addr))
|
.with_host((host, port).try_into().unwrap_or_default())
|
||||||
.with_auth(auth)
|
.with_auth(auth)
|
||||||
.with_debug(false) // Turn this on for debugging network chatter
|
.with_debug(false) // Turn this on for debugging network chatter
|
||||||
.build();
|
.build();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue