MCPcopy Create free account
hub / github.com/bendudson/EuraliOS / var

Function var

euralios_std/src/env.rs:67–96  ·  view source on GitHub ↗
(key: &str)

Source from the content-addressed store, hash-verified

65}
66
67pub fn var(key: &str) -> Result<String, VarError> {
68 let envs = as_str().map_err(|_| VarError::NotPresent)?;
69
70 if let Some(pos) = envs.find(key) {
71 if pos + key.len() == envs.len() {
72 return Err(VarError::NotPresent);
73 }
74 let env_bytes = envs.as_bytes();
75
76 // Check that the next character is '='
77 if env_bytes[pos + key.len()] != b'=' {
78 return Err(VarError::NotPresent);
79 }
80
81 // Value terminated by 0x03
82 let value_bytes = &env_bytes[(pos + key.len() + 1)..];
83 let mut value_len = 0;
84 for b in value_bytes {
85 if *b == 0x03 {
86 break;
87 }
88 value_len += 1;
89 }
90 return Ok(String::from(
91 unsafe {
92 str::from_utf8_unchecked(&value_bytes[..value_len])
93 }));
94 }
95 Err(VarError::NotPresent)
96}
97
98pub fn current_dir() -> Result<PathBuf, VarError> {
99 let pwd = var("PWD")?;

Callers 1

current_dirFunction · 0.85

Calls 3

as_strFunction · 0.85
findMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected