summaryrefslogtreecommitdiffstats
path: root/src/modules/package.rs
diff options
context:
space:
mode:
authorMatan Kushner <hello@matchai.me>2019-06-10 15:56:17 +0100
committerGitHub <noreply@github.com>2019-06-10 15:56:17 +0100
commit097f1b05f1d82967fe2a900ccf7ba3597c04ad77 (patch)
treee5419607802145bf2521defd9823d224f937b653 /src/modules/package.rs
parent8239fbd12befad1126e677fa083ce73947d74d8c (diff)
Add support for prompt configuration (#62)
- Create `Config` struct that is added to `Context` when initialized - Read `~/.confg/starship.toml` during initialization (can be updated later to also look at `$XDG_CONFIG_HOME`) - `Context` now has a method for creating modules. This allows us to provide modules with a reference to the configuration specific to that module
Diffstat (limited to 'src/modules/package.rs')
-rw-r--r--src/modules/package.rs70
1 files changed, 25 insertions, 45 deletions
diff --git a/src/modules/package.rs b/src/modules/package.rs
index b2666c8de..a9af63891 100644
--- a/src/modules/package.rs
+++ b/src/modules/package.rs
@@ -1,23 +1,20 @@
use super::{Context, Module};
+use crate::utils;
use ansi_term::Color;
-use serde_json;
-use std::fs::File;
-use std::io;
-use std::io::Read;
-use std::path::PathBuf;
+use serde_json as json;
use toml;
/// Creates a segment with the current package version
///
/// Will display if a version is defined for your Node.js or Rust project (if one exists)
-pub fn segment(context: &Context) -> Option<Module> {
- match get_package_version(context) {
+pub fn segment<'a>(context: &'a Context) -> Option<Module<'a>> {
+ match get_package_version() {
Some(package_version) => {
const PACKAGE_CHAR: &str = "📦 ";
let module_color = Color::Red.bold();
- let mut module = Module::new("package");
+ let mut module = context.new_module("package");
module.set_style(module_color);
module.get_prefix().set_value("is ");
@@ -30,27 +27,8 @@ pub fn segment(context: &Context) -> Option<Module> {
}
}
-// TODO: Combine into one function and just call for different file names!
-fn is_cargo_toml(dir_entry: &PathBuf) -> bool {
- dir_entry.is_file() && dir_entry.file_name().unwrap_or_default() == "Cargo.toml"
-}
-
-fn is_package_json(dir_entry: &PathBuf) -> bool {
- dir_entry.is_file() && dir_entry.file_name().unwrap_or_default() == "package.json"
-}
-
-// TODO: Move to `utils.rs` file and import
-fn read_file(file_name: &str) -> io::Result<String> {
- let mut file = File::open(file_name)?;
- let mut data = String::new();
-
- file.read_to_string(&mut data)?;
-
- Ok(data)
-}
-
fn extract_cargo_version(file_contents: &str) -> Option<String> {
- let cargo_toml = file_contents.parse::<toml::Value>().ok()?;
+ let cargo_toml: toml::Value = toml::from_str(&file_contents).ok()?;
let raw_version = cargo_toml.get("package")?.get("version")?.as_str()?;
let formatted_version = format_version(raw_version);
@@ -58,7 +36,7 @@ fn extract_cargo_version(file_contents: &str) -> Option<String> {
}
fn extract_package_version(file_contents: &str) -> Option<String> {
- let package_json: serde_json::Value = serde_json::from_str(&file_contents).ok()?;
+ let package_json: json::Value = json::from_str(&file_contents).ok()?;
let raw_version = package_json.get("version")?.as_str()?;
if raw_version == "null" {
return None;
@@ -68,17 +46,15 @@ fn extract_package_version(file_contents: &str) -> Option<String> {
Some(formatted_version)
}
-fn get_package_version(context: &Context) -> Option<String> {
- let has_cargo_toml = context.dir_files.iter().any(is_cargo_toml);
- if has_cargo_toml {
- let file_contents = read_file("Cargo.toml").ok()?;
- return extract_cargo_version(&file_contents);
+fn get_package_version() -> Option<String> {
+ let cargo_toml = utils::read_file("Cargo.toml");
+ if let Ok(cargo_toml) = cargo_toml {
+ return extract_cargo_version(&cargo_toml);
}
- let has_package_json = context.dir_files.iter().any(is_package_json);
- if has_package_json {
- let file_contents = read_file("package.json").ok()?;
- return extract_package_version(&file_contents);
+ let package_json = utils::read_file("package.json");
+ if let Ok(package_json) = package_json {
+ return extract_package_version(&package_json);
}
None
@@ -99,15 +75,19 @@ mod tests {
#[test]
fn test_extract_cargo_version() {
- let cargo_with_version = "[package]
- name = \"starship\"
- version = \"0.1.0\"";
+ let cargo_with_version = r#"
+ [package]
+ name = "starship"
+ version = "0.1.0"
+ "#;
let expected_version = Some("v0.1.0".to_string());
assert_eq!(extract_cargo_version(&cargo_with_version), expected_version);
- let cargo_without_version = "[package]
- name = \"starship\"";
+ let cargo_without_version = r#"
+ [package]
+ name = "starship"
+ "#;
let expected_version = None;
assert_eq!(
@@ -118,7 +98,7 @@ mod tests {
#[test]
fn test_extract_package_version() {
- let package_with_version = serde_json::json!({
+ let package_with_version = json::json!({
"name": "spacefish",
"version": "0.1.0"
})
@@ -130,7 +110,7 @@ mod tests {
expected_version
);
- let package_without_version = serde_json::json!({
+ let package_without_version = json::json!({
"name": "spacefish"
})
.to_string();