Skip to content

Commit

Permalink
Remove cloning and various formating/efficiency improvements (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinlesceller authored Feb 4, 2020
1 parent c76ee7c commit 047ea93
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 25 deletions.
10 changes: 5 additions & 5 deletions config/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,18 @@ fn comments() -> HashMap<String, String> {
}

fn get_key(line: &str) -> String {
if line.contains("[") && line.contains("]") {
if line.contains('[') && line.contains(']') {
return line.to_owned();
} else if line.contains("=") {
return line.split("=").collect::<Vec<&str>>()[0].trim().to_owned();
} else if line.contains('=') {
return line.split('=').collect::<Vec<&str>>()[0].trim().to_owned();
} else {
return "NOT_FOUND".to_owned();
}
}

pub fn insert_comments(orig: String) -> String {
let comments = comments();
let lines: Vec<&str> = orig.split("\n").collect();
let lines: Vec<&str> = orig.split('\n').collect();
let mut out_lines = vec![];
for l in lines {
let key = get_key(l);
Expand All @@ -261,5 +261,5 @@ pub fn insert_comments(orig: String) -> String {
for l in out_lines {
ret_val.push_str(&l);
}
ret_val.to_owned()
ret_val
}
23 changes: 10 additions & 13 deletions config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ use crate::types::{TorConfig, WalletConfig};
use crate::util::logger::LoggingConfig;

/// Wallet configuration file name
pub const WALLET_CONFIG_FILE_NAME: &'static str = "grin-wallet.toml";
const WALLET_LOG_FILE_NAME: &'static str = "grin-wallet.log";
const GRIN_HOME: &'static str = ".grin";
pub const WALLET_CONFIG_FILE_NAME: &str = "grin-wallet.toml";
const WALLET_LOG_FILE_NAME: &str = "grin-wallet.log";
const GRIN_HOME: &str = ".grin";
/// Wallet data directory
pub const GRIN_WALLET_DIR: &'static str = "wallet_data";
pub const GRIN_WALLET_DIR: &str = "wallet_data";
/// Node API secret
pub const API_SECRET_FILE_NAME: &'static str = ".api_secret";
pub const API_SECRET_FILE_NAME: &str = ".api_secret";
/// Owner API secret
pub const OWNER_API_SECRET_FILE_NAME: &'static str = ".owner_api_secret";
pub const OWNER_API_SECRET_FILE_NAME: &str = ".owner_api_secret";

fn get_grin_path(
chain_type: &global::ChainTypes,
Expand Down Expand Up @@ -123,7 +123,7 @@ fn check_api_secret_file(
Some(p) => p,
None => get_grin_path(chain_type, false)?,
};
let mut api_secret_path = grin_path.clone();
let mut api_secret_path = grin_path;
api_secret_path.push(file_name);
if !api_secret_path.exists() {
init_api_secret(&api_secret_path)
Expand All @@ -150,7 +150,7 @@ pub fn initial_setup_wallet(
(path, GlobalWalletConfig::new(p.to_str().unwrap())?)
} else {
// Check if grin dir exists
let grin_path = match data_path.clone() {
let grin_path = match data_path {
Some(p) => p,
None => get_grin_path(chain_type, create_path)?,
};
Expand Down Expand Up @@ -265,7 +265,7 @@ impl GlobalWalletConfig {
.unwrap()
.clone(),
),
String::from(format!("{}", e)),
format!("{}", e),
));
}
}
Expand Down Expand Up @@ -311,10 +311,7 @@ impl GlobalWalletConfig {
match encoded {
Ok(enc) => return Ok(enc),
Err(e) => {
return Err(ConfigError::SerializationError(String::from(format!(
"{}",
e
))));
return Err(ConfigError::SerializationError(format!("{}", e)));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions config/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl WalletConfig {
/// Use value from config file, defaulting to sensible value if missing.
pub fn owner_api_listen_port(&self) -> u16 {
self.owner_api_listen_port
.unwrap_or(WalletConfig::default_owner_api_listen_port())
.unwrap_or_else(WalletConfig::default_owner_api_listen_port)
}

/// Owner API listen address
Expand Down Expand Up @@ -166,7 +166,7 @@ impl From<io::Error> for ConfigError {
fn from(error: io::Error) -> ConfigError {
ConfigError::FileIOError(
String::from(""),
String::from(format!("Error loading config file: {}", error)),
format!("Error loading config file: {}", error),
)
}
}
Expand Down
6 changes: 3 additions & 3 deletions controller/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,15 @@ pub fn tx_messages(tx: &TxLogEntry, dark_background_color_scheme: bool) -> Resul

let msgs = match tx.messages.clone() {
None => {
writeln!(t, "{}", "None").unwrap();
writeln!(t, "None").unwrap();
t.reset().unwrap();
return Ok(());
}
Some(m) => m.clone(),
};

if msgs.messages.is_empty() {
writeln!(t, "{}", "None").unwrap();
writeln!(t, "None").unwrap();
t.reset().unwrap();
return Ok(());
}
Expand Down Expand Up @@ -529,7 +529,7 @@ pub fn payment_proof(tx: &TxLogEntry) -> Result<(), Error> {

let pp = match &tx.payment_proof {
None => {
writeln!(t, "{}", "None").unwrap();
writeln!(t, "None").unwrap();
t.reset().unwrap();
return Ok(());
}
Expand Down
2 changes: 1 addition & 1 deletion libwallet/src/slate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl ParticipantMessageData {

impl fmt::Display for ParticipantMessageData {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "")?;
writeln!(f)?;
write!(f, "Participant ID {} ", self.id)?;
if self.id == 0 {
writeln!(f, "(Sender)")?;
Expand Down
2 changes: 1 addition & 1 deletion util/src/ov3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl OnionV3Address {
}
};
let d_pub_key: DalekPublicKey = (&d_skey).into();
Ok(OnionV3Address(d_pub_key.as_bytes().clone()))
Ok(OnionV3Address(*d_pub_key.as_bytes()))
}

/// return dalek public key
Expand Down

0 comments on commit 047ea93

Please sign in to comment.