-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement sys.get_os (#238) * Implemented, tested, documented. * Fix lib test.
- Loading branch information
Showing
5 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use anyhow::Result; | ||
use starlark::collections::SmallMap; | ||
use starlark::const_frozen_string; | ||
use starlark::values::Heap; | ||
use starlark::values::dict::Dict; | ||
|
||
#[derive(Debug)] | ||
struct OsInfo { | ||
arch: String, | ||
desktop_env: String, | ||
distro: String, | ||
platform: String, | ||
} | ||
|
||
pub fn get_os(starlark_heap: &Heap) -> Result<Dict> { | ||
|
||
let cmd_res = handle_get_os()?; | ||
|
||
let res = SmallMap::new(); | ||
let mut dict_res = Dict::new(res); | ||
let arch_value = starlark_heap.alloc_str(&cmd_res.arch); | ||
dict_res.insert_hashed(const_frozen_string!("arch").to_value().get_hashed().unwrap(), arch_value.to_value()); | ||
|
||
let desktop_env_value = starlark_heap.alloc_str(&cmd_res.desktop_env); | ||
dict_res.insert_hashed(const_frozen_string!("desktop_env").to_value().get_hashed().unwrap(), desktop_env_value.to_value()); | ||
|
||
let distro = starlark_heap.alloc_str(&cmd_res.distro); | ||
dict_res.insert_hashed(const_frozen_string!("distro").to_value().get_hashed().unwrap(), distro.to_value()); | ||
|
||
let platform = starlark_heap.alloc_str(&cmd_res.platform); | ||
dict_res.insert_hashed(const_frozen_string!("platform").to_value().get_hashed().unwrap(), platform.to_value()); | ||
|
||
Ok(dict_res) | ||
} | ||
|
||
fn handle_get_os() -> Result<OsInfo> { | ||
return Ok(OsInfo { | ||
arch: whoami::arch().to_string(), | ||
desktop_env: whoami::desktop_env().to_string(), | ||
distro: whoami::distro().to_string(), | ||
platform: whoami::platform().to_string(), | ||
}); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_sys_get_os() -> anyhow::Result<()>{ | ||
|
||
let test_heap = Heap::new(); | ||
let res = get_os(&test_heap)?; | ||
println!("{}", res.to_string()); | ||
assert!(res.to_string().contains(r#""arch": "x86_64""#)); | ||
Ok(()) | ||
} | ||
} |