use std::path::Path;
use std::sync::{Arc, Mutex};
use chrono::DateTime;
use rand::prelude::*;
use rand_chacha::ChaCha20Rng;
use crate::backend::{ChangeId, ObjectId, Signature, Timestamp};
#[derive(Debug, Clone)]
pub struct UserSettings {
config: config::Config,
timestamp: Option<Timestamp>,
rng: Arc<JJRng>,
}
#[derive(Debug, Clone)]
pub struct RepoSettings {
_config: config::Config,
}
#[derive(Debug, Clone)]
pub struct GitSettings {
pub auto_local_branch: bool,
}
impl GitSettings {
pub fn from_config(config: &config::Config) -> Self {
GitSettings {
auto_local_branch: config.get_bool("git.auto-local-branch").unwrap_or(true),
}
}
}
impl Default for GitSettings {
fn default() -> Self {
GitSettings {
auto_local_branch: true,
}
}
}
fn get_timestamp_config(config: &config::Config, key: &str) -> Option<Timestamp> {
match config.get_string(key) {
Ok(timestamp_str) => match DateTime::parse_from_rfc3339(×tamp_str) {
Ok(datetime) => Some(Timestamp::from_datetime(datetime)),
Err(_) => None,
},
Err(_) => None,
}
}
fn get_rng_seed_config(config: &config::Config) -> Option<u64> {
config
.get_string("debug.randomness-seed")
.ok()
.and_then(|str| str.parse().ok())
}
impl UserSettings {
pub fn from_config(config: config::Config) -> Self {
let timestamp = get_timestamp_config(&config, "debug.commit-timestamp");
let rng_seed = get_rng_seed_config(&config);
UserSettings {
config,
timestamp,
rng: Arc::new(JJRng::new(rng_seed)),
}
}
pub fn with_repo(&self, _repo_path: &Path) -> Result<RepoSettings, config::ConfigError> {
let config = self.config.clone();
Ok(RepoSettings { _config: config })
}
pub fn get_rng(&self) -> Arc<JJRng> {
self.rng.clone()
}
pub fn user_name(&self) -> String {
self.config
.get_string("user.name")
.unwrap_or_else(|_| Self::user_name_placeholder().to_string())
}
pub fn user_name_placeholder() -> &'static str {
"(no name configured)"
}
pub fn user_email(&self) -> String {
self.config
.get_string("user.email")
.unwrap_or_else(|_| Self::user_email_placeholder().to_string())
}
pub fn user_email_placeholder() -> &'static str {
"(no email configured)"
}
pub fn operation_timestamp(&self) -> Option<Timestamp> {
get_timestamp_config(&self.config, "debug.operation-timestamp")
}
pub fn operation_hostname(&self) -> String {
self.config
.get_string("operation.hostname")
.unwrap_or_else(|_| whoami::hostname())
}
pub fn operation_username(&self) -> String {
self.config
.get_string("operation.username")
.unwrap_or_else(|_| whoami::username())
}
pub fn push_branch_prefix(&self) -> String {
self.config
.get_string("push.branch-prefix")
.unwrap_or_else(|_| "push-".to_string())
}
pub fn default_revset(&self) -> String {
self.config
.get_string("ui.default-revset")
.unwrap_or_else(|_| {
"@ | (remote_branches() | tags()).. | ((remote_branches() | tags())..)-".to_string()
})
}
pub fn signature(&self) -> Signature {
let timestamp = self.timestamp.clone().unwrap_or_else(Timestamp::now);
Signature {
name: self.user_name(),
email: self.user_email(),
timestamp,
}
}
pub fn allow_native_backend(&self) -> bool {
self.config
.get_bool("ui.allow-init-native")
.unwrap_or(false)
}
pub fn oplog_relative_timestamps(&self) -> bool {
self.config
.get_bool("ui.oplog-relative-timestamps")
.unwrap_or(true)
}
pub fn config(&self) -> &config::Config {
&self.config
}
pub fn git_settings(&self) -> GitSettings {
GitSettings::from_config(&self.config)
}
pub fn graph_style(&self) -> String {
self.config
.get_string("ui.graph.style")
.unwrap_or_else(|_| "curved".to_string())
}
}
#[derive(Debug)]
pub struct JJRng(Mutex<ChaCha20Rng>);
impl JJRng {
pub fn new_change_id(&self, length: usize) -> ChangeId {
let mut rng = self.0.lock().unwrap();
let random_bytes = (0..length).map(|_| rng.gen::<u8>()).collect();
ChangeId::new(random_bytes)
}
fn new(seed: Option<u64>) -> Self {
Self(Mutex::new(JJRng::internal_rng_from_seed(seed)))
}
fn internal_rng_from_seed(seed: Option<u64>) -> ChaCha20Rng {
match seed {
Some(seed) => ChaCha20Rng::seed_from_u64(seed),
None => ChaCha20Rng::from_entropy(),
}
}
}