Skip to content

Commit

Permalink
Improve version incompatibility error
Browse files Browse the repository at this point in the history
With this change, `xplr` will only raise version incompatibility error
if the major version changes. Minor version updates are assumed to be
backwards compatible.

If the major version is `v0`, the minor version will be considered as
the major version and the security/patch version will be considered as
minor version and the same logic will apply.
  • Loading branch information
sayanarijit committed Apr 6, 2021
1 parent 981ead8 commit eeee339
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xplr"
version = "0.3.0" # Update app.rs
version = "0.3.1" # Update app.rs
authors = ["Arijit Basu <[email protected]>"]
edition = "2018"
description = "A hackable, minimal, fast TUI file explorer, stealing ideas from nnn and fzf"
Expand Down
97 changes: 56 additions & 41 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::fs;
use std::io;
use std::path::PathBuf;

pub const VERSION: &str = "v0.3.0"; // Update Cargo.toml
pub const VERSION: &str = "v0.3.1"; // Update Cargo.toml

pub const TEMPLATE_TABLE_ROW: &str = "TEMPLATE_TABLE_ROW";

Expand Down Expand Up @@ -703,6 +703,21 @@ pub enum HelpMenuLine {
Paragraph(String),
}

pub fn is_compatible(existing: &str, required: &str) -> bool {
let mut existing = existing.split('.');
let mut required = required.split('.');

let mut major_existing = existing.next().unwrap_or_default();
let mut major_required = required.next().unwrap_or_default();

if major_existing == "v0" && major_required == "v0" {
major_existing = existing.next().unwrap_or_default();
major_required = required.next().unwrap_or_default();
};

major_existing == major_required
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct App {
config: Config,
Expand Down Expand Up @@ -734,7 +749,7 @@ impl App {
Config::default()
};

if config.version != VERSION {
if config.version != VERSION && !is_compatible(&config.version, VERSION) {
bail!(
"incompatible configuration version in {}
You config version is : {}
Expand All @@ -744,47 +759,47 @@ impl App {
config.version,
VERSION,
)
} else {
let mode = config
.modes
.get(&"default".to_string())
.map(|k| k.to_owned())
.unwrap_or_default();

let pid = std::process::id();
let session_path = dirs::runtime_dir()
.unwrap_or_else(|| "/tmp".into())
.join("xplr")
.join("session")
.join(&pid.to_string())
.to_string_lossy()
.to_string();

let mut explorer_config = ExplorerConfig::default();
if !config.general.show_hidden {
explorer_config.filters.push(NodeFilterApplicable::new(
NodeFilter::RelativePathDoesNotStartWith,
".".into(),
Default::default(),
));
}
};

Ok(Self {
config,
pwd: pwd.to_string_lossy().to_string(),
directory_buffers: Default::default(),
tasks: Default::default(),
selection: Default::default(),
msg_out: Default::default(),
mode,
input_buffer: Default::default(),
pid,
session_path: session_path.clone(),
pipe: Pipe::from_session_path(&session_path)?,
explorer_config,
logs: Default::default(),
})
let mode = config
.modes
.get(&"default".to_string())
.map(|k| k.to_owned())
.unwrap_or_default();

let pid = std::process::id();
let session_path = dirs::runtime_dir()
.unwrap_or_else(|| "/tmp".into())
.join("xplr")
.join("session")
.join(&pid.to_string())
.to_string_lossy()
.to_string();

let mut explorer_config = ExplorerConfig::default();
if !config.general.show_hidden {
explorer_config.filters.push(NodeFilterApplicable::new(
NodeFilter::RelativePathDoesNotStartWith,
".".into(),
Default::default(),
));
}

Ok(Self {
config,
pwd: pwd.to_string_lossy().to_string(),
directory_buffers: Default::default(),
tasks: Default::default(),
selection: Default::default(),
msg_out: Default::default(),
mode,
input_buffer: Default::default(),
pid,
session_path: session_path.clone(),
pipe: Pipe::from_session_path(&session_path)?,
explorer_config,
logs: Default::default(),
})
}

pub fn focused_node(&self) -> Option<&Node> {
Expand Down
14 changes: 14 additions & 0 deletions tests/test_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use xplr::*;

#[test]
fn test_version_incompatibility() {
assert!(app::is_compatible("v0.1.0", "v0.1.2"));
assert!(app::is_compatible("v0.2.0", "v0.2.2"));
assert!(!app::is_compatible("v0.1.0", "v0.2.0"));
assert!(!app::is_compatible("v0.1.0", "v1.1.0"));
assert!(app::is_compatible("v1.1.0", "v1.1.0"));
assert!(app::is_compatible("v1.1.0", "v1.1.1"));
assert!(app::is_compatible("v1.1.0", "v1.2.1"));
assert!(app::is_compatible("v1.1.0", "v1.2.1"));
assert!(!app::is_compatible("v1.1.0", "v2.0.0"));
}

0 comments on commit eeee339

Please sign in to comment.