Struct unidirs::SimpleBuilder

source ·
pub struct SimpleBuilder<Q, O, A> { /* private fields */ }
Expand description

The simple builder is constructed through the UnifiedDirs::simple method and allows to further configure ways of detecting whether the application is run as a service or by the user.

with and all the with_* functions are called and evaluated in order and immediately (not delayed until the call to build). If service mode is detected by any technique, further functions won’t be evaluated anymore.

Implementations§

source§

impl<Q, O, A> SimpleBuilder<Q, O, A>where Q: AsRef<str>, O: AsRef<str>, A: AsRef<str>,

source

pub fn with_env(self) -> Self

Use certain environment variable names to detect to be in service mode. The value of each variable doesn’t matter, just whether the variable is present.

Currently the name SERVICE and DAEMON indicate the service mode.

source

pub fn with_args(self) -> Self

Use certain program arguments to detect to be in service mode.

Currently the arguments --service and --daemon indicate the service mode.

source

pub fn with_username(self) -> Self

Compare the executing user’s account name against the application name to detect the service mode.

It is common to create a separate a separate user with the same name as the application, which is then used to execute the application. Therefore, if the application name passed in UnifiedDirs::simple matches the executing username, it indicates the service mode.

source

pub fn with(self, f: impl FnOnce(&Self) -> bool) -> Self

Define a custom detection logic for the service mode. A positive value means service mode, a negative value means user mode.

The provided closure is only called if previous techniques didn’t detect a service mode yet.

Example

Consider the application was called with a SERVICE environment variable present and the following builder was used. The closure will not be called as the application was already detected to be in service mode due to the environment variable.

use std::env;
use unidirs::UnifiedDirs;

env::set_var("SERVICE", "true");

let mut called = false;
let dirs = UnifiedDirs::simple("com", "example", "app")
    .with_env()
    .with(|_| {
        called = true;
        true
    })
    .build()
    .unwrap();

assert!(!called);

As the with_* function are called in order, the same setup but in reverse order will call the lambda as it is evaluated first:

use std::env;
use unidirs::UnifiedDirs;

env::set_var("SERVICE", "true");

let mut called = false;
let dirs = UnifiedDirs::simple("com", "example", "app")
    .with(|_| {
        called = true;
        true
    })
    .with_env() // called after `with` now
    .build()
    .unwrap();

assert!(called);
source

pub fn build(self) -> Option<UnifiedDirs>

Construct the UnifiedDirs instance with the backend decided by previously configured techniques.

  • If the application was built in debug mode (or with debug_assertions enabled), it will always pick LocalDirs.
  • If any of the configured techniques detected that the application is run in service mode, the backend will be ServiceDirs.
  • Otherwise, it’ll be UserDirs.
source

pub fn default(self) -> Option<UnifiedDirs>

Configure and execute the builder with all detection techniques enabled.

This is a convenience shorthand for manually calling with_env, with_args and with_username followed by build.

Example
use unidirs::UnifiedDirs;

UnifiedDirs::simple("com", "example", "app").default();

// The `default()` call is a shorthand for:

UnifiedDirs::simple("com", "example", "app")
    .with_env()
    .with_args()
    .with_username()
    .build();

Auto Trait Implementations§

§

impl<Q, O, A> RefUnwindSafe for SimpleBuilder<Q, O, A>where A: RefUnwindSafe, O: RefUnwindSafe, Q: RefUnwindSafe,

§

impl<Q, O, A> Send for SimpleBuilder<Q, O, A>where A: Send, O: Send, Q: Send,

§

impl<Q, O, A> Sync for SimpleBuilder<Q, O, A>where A: Sync, O: Sync, Q: Sync,

§

impl<Q, O, A> Unpin for SimpleBuilder<Q, O, A>where A: Unpin, O: Unpin, Q: Unpin,

§

impl<Q, O, A> UnwindSafe for SimpleBuilder<Q, O, A>where A: UnwindSafe, O: UnwindSafe, Q: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.