Task/Environment_variable.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
use std::{ffi::CString, fmt::Debug, sync::Arc};
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct Environment_variable_type(Arc<CString>, usize);
impl Debug for Environment_variable_type {
fn fmt(&self, Formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Formatter
.debug_struct("Environment_variable_type")
.field("Name", &self.Get_name())
.field("Value", &self.Get_value())
.finish()
}
}
impl Environment_variable_type {
/// Create a new environment variable.
pub fn New(Name: &str, Value: &str) -> Self {
let Environment_variable = CString::new(format!("{}={}", Name, Value)).unwrap();
Self(Arc::new(Environment_variable), Name.len())
}
/// Create a new environment variable from a raw string.
///
/// # Example
///
/// ```
/// use Task::Environment_variable_type;
///
/// let Environment_variable = Environment_variable_type::New("Name", "Value");
///
/// assert_eq!(Environment_variable.Get_name(), "Name");
/// ```
pub fn Get_name(&self) -> &str {
self.0.to_str().unwrap()[..self.1].trim_end_matches('\0')
}
/// Get the value of the environment variable.
///
/// # Example
///
/// ```
/// use Task::Environment_variable_type;
///
/// let Environment_variable = Environment_variable_type::New("Name", "Value");
///
/// assert_eq!(Environment_variable.Get_value(), "Value");
/// ```
pub fn Get_value(&self) -> &str {
self.0.to_str().unwrap()[self.1 + 1..].trim_end_matches('\0')
}
/// Get the inner raw CString.
pub fn Get_raw(&self) -> &CString {
&self.0
}
}