Skip to content

Commit 95b4f7e

Browse files
committed
Replace Result return types with Cow<'static, str>
1 parent 517df9d commit 95b4f7e

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

src/cache.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::project::Project;
22
use serde_derive::*;
33
use std::{
4+
borrow::Cow,
45
collections::hash_map::DefaultHasher,
56
fs::*,
67
hash::*,
@@ -43,15 +44,16 @@ impl Cache {
4344
}
4445
}
4546

46-
fn get_all_files(project: &Project) -> Result<Vec<FileData>, String> {
47+
fn get_all_files(project: &Project) -> Result<Vec<FileData>, Cow<'static, str>> {
4748
let dir_contents: Vec<PathBuf> = match read_dir(project.get_directories().get_source_dir()) {
4849
Ok(cont) => cont,
4950
Err(e) =>
5051
return Err(format!(
5152
"Could not read source directory (\"{}\"): {}",
5253
project.get_directories().get_source_dir(),
5354
e
54-
)),
55+
)
56+
.into()),
5557
}
5658
.into_iter()
5759
.map(|x| x.unwrap().path())
@@ -88,15 +90,15 @@ impl Cache {
8890
Ok(files)
8991
}
9092

91-
pub fn new(project: &Project) -> Result<Self, String> {
93+
pub fn new(project: &Project) -> Result<Self, Cow<'static, str>> {
9294
Ok(Self {
9395
files: Self::get_all_files(project)?,
9496
})
9597
}
9698

97-
pub fn get_changed(&self, project: &Project) -> Result<Vec<PathBuf>, String> {
99+
pub fn get_changed(&self, project: &Project) -> Result<Vec<PathBuf>, Cow<'static, str>> {
98100
if !Path::new("Ocean.lock").exists() {
99-
return Err("Cannot find Ocean.lock in project root.".to_string());
101+
return Err("Cannot find Ocean.lock in project root.".into());
100102
}
101103

102104
let mut changed = vec![];
@@ -112,9 +114,9 @@ impl Cache {
112114
Ok(changed)
113115
}
114116

115-
pub fn update_cache<'a>(&mut self, project: &Project) -> Result<(), &'a str> {
117+
pub fn update_cache<'a>(&mut self, project: &Project) -> Result<(), Cow<'static, str>> {
116118
if !Path::new("Ocean.lock").exists() {
117-
return Err("Cannot find Ocean.lock in project root.");
119+
return Err("Cannot find Ocean.lock in project root.".into());
118120
}
119121

120122
self.files = Self::get_all_files(project).unwrap();

src/commands.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{cache::Cache, editors::*, language::*, platform::*, project::*};
22
use std::{
3+
borrow::Cow,
34
env::{self, current_dir, set_current_dir},
45
ffi::OsStr,
56
fs::{create_dir_all, read_dir, remove_dir_all, remove_file, rename, File},
@@ -28,12 +29,12 @@ impl Commands {
2829
split.join("\n")
2930
}
3031

31-
fn get_toml(path: Option<&str>, search_count: Option<u32>) -> Result<Project, String> {
32+
fn get_toml(path: Option<&str>, search_count: Option<u32>) -> Result<Project, Cow<'static, str>> {
3233
let search_count = search_count.unwrap_or(0);
3334

3435
if search_count > 4 {
3536
return Err(
36-
"Could not find Ocean.toml, please make sure that you are in a valid project directory.".to_string(),
37+
"Could not find Ocean.toml, please make sure that you are in a valid project directory.".into(),
3738
);
3839
}
3940

@@ -47,7 +48,7 @@ impl Commands {
4748

4849
if let Ok(mut f) = File::open("Ocean.toml") {
4950
if f.read_to_string(&mut contents).is_err() {
50-
return Err("Could not read file".to_string());
51+
return Err("Could not read file".into());
5152
}
5253
};
5354

src/editors.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::project::Project;
2-
use std::{collections::HashMap, env, path::Path};
2+
use std::{borrow::Cow, collections::HashMap, env, path::Path};
33

44
pub trait Editor<T> {
5-
fn get_compiler_path(project: &Project) -> Result<String, String> {
5+
fn get_compiler_path(project: &Project) -> Result<String, Cow<'static, str>> {
66
let mut ret_path = String::new();
77

88
match env::var_os("PATH") {
@@ -31,11 +31,11 @@ pub trait Editor<T> {
3131
ret_path = path.to_str().unwrap().to_string();
3232
}
3333
},
34-
None => return Err("Cannot find PATH environment variable".to_string()),
34+
None => return Err("Cannot find PATH environment variable".into()),
3535
};
3636

3737
if ret_path == "" {
38-
return Err("Cannot find compiler in PATH. Did you add the compiler's directory to the PATH?".to_string());
38+
return Err("Cannot find compiler in PATH. Did you add the compiler's directory to the PATH?".into());
3939
}
4040

4141
Ok(ret_path)

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ mod platform;
99
mod project;
1010

1111
use commands::Commands;
12-
use std::env;
12+
use std::{borrow::Cow, env};
1313

14-
fn parse_args(mut args: Vec<String>) -> Result<(), String> {
14+
fn parse_args(mut args: Vec<String>) -> Result<(), Cow<'static, str>> {
1515
let platforms = ["linux", "osx", "windows"];
1616

1717
if args[1..].is_empty() {
1818
Commands::help(None);
19-
return Err("No arguments were specified".to_string());
19+
return Err("No arguments were specified".into());
2020
} else {
2121
args = args[1..].to_vec();
2222
}

0 commit comments

Comments
 (0)