8000 Support stable Rust by drdo · Pull Request #91 · drdo/redu · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support stable Rust #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "nightly"
channel = "stable"
5 changes: 0 additions & 5 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
max_width = 80
match_arm_blocks = false
error_on_line_overflow = false
imports_granularity = "Crate"
overflow_delimited_expr = true
group_imports = "StdExternalCrate"
use_small_heuristics = "Max"
use_field_init_shorthand = true
165 changes: 92 additions & 73 deletions src/cache/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,59 +194,68 @@ fn sizetree_iter_empty() {
fn insert_uniques_0() {
let tree = example_tree_0();
let entries = to_sorted_entries(&tree);
assert_eq!(entries, vec![
(vec!["a"], 13, true),
(vec!["a", "0"], 4, true),
(vec!["a", "0", "x"], 1, false),
(vec!["a", "0", "y"], 2, false),
(vec!["a", "0", "z"], 1, true),
(vec!["a", "0", "z", "0"], 1, false),
(vec!["a", "1"], 9, true),
(vec!["a", "1", "x"], 9, true),
(vec!["a", "1", "x", "0"], 7, false),
(vec!["a", "1", "x", "1"], 2, false),
]);
assert_eq!(
entries,
vec![
(vec!["a"], 13, true),
(vec!["a", "0"], 4, true),
(vec!["a", "0", "x"], 1, false),
(vec!["a", "0", "y"], 2, false),
(vec!["a", "0", "z"], 1, true),
(vec!["a", "0", "z", "0"], 1, false),
(vec!["a", "1"], 9, true),
(vec!["a", "1", "x"], 9, true),
(vec!["a", "1", "x", "0"], 7, false),
(vec!["a", "1", "x", "1"], 2, false),
]
);
}

#[test]
fn insert_uniques_1() {
let tree = example_tree_1();
let entries = to_sorted_entries(&tree);
assert_eq!(entries, vec![
(vec!["a"], 22, true),
(vec!["a", "0"], 14, true),
(vec!["a", "0", "x"], 3, false),
(vec!["a", "0", "y"], 2, false),
(vec!["a", "0", "z"], 9, true),
(vec!["a", "0", "z", "0"], 9, false),
(vec!["a", "1"], 1, true),
(vec!["a", "1", "x"], 1, true),
(vec!["a", "1", "x", "1"], 1, false),
(vec!["a", "2"], 7, true),
(vec!["a", "2", "x"], 7, true),
(vec!["a", "2", "x", "0"], 7, false),
]);
assert_eq!(
entries,
vec![
(vec!["a"], 22, true),
(vec!["a", "0"], 14, true),
(vec!["a", "0", "x"], 3, false),
(vec!["a", "0", "y"], 2, false),
(vec!["a", "0", "z"], 9, true),
(vec!["a", "0", "z", "0"], 9, false),
(vec!["a", "1"], 1, true),
(vec!["a", "1", "x"], 1, true),
(vec!["a", "1", "x", "1"], 1, false),
(vec!["a", "2"], 7, true),
(vec!["a", "2", "x"], 7, true),
(vec!["a", "2", "x", "0"], 7, false),
]
);
}

#[test]
fn insert_uniques_2() {
let tree = example_tree_2();
let entries = to_sorted_entries(&tree);
assert_eq!(entries, vec![
(vec!["a"], 8, true),
(vec!["a", "1"], 1, true),
(vec!["a", "1", "x"], 1, true),
(vec!["a", "1", "x", "1"], 1, false),
(vec!["a", "2"], 7, true),
(vec!["a", "2", "x"], 7, true),
(vec!["a", "2", "x", "0"], 7, false),
(vec!["b"], 14, true),
(vec!["b", "0"], 14, true),
(vec!["b", "0", "x"], 3, false),
(vec!["b", "0", "y"], 2, false),
(vec!["b", "0", "z"], 9, true),
(vec!["b", "0", "z", "0"], 9, false),
]);
assert_eq!(
entries,
vec![
(vec!["a"], 8, true),
(vec!["a", "1"], 1, true),
(vec!["a", "1", "x"], 1, true),
(vec!["a", "1", "x", "1"], 1, false),
(vec!["a", "2"], 7, true),
(vec!["a", "2", "x"], 7, true),
(vec!["a", "2", "x", "0"], 7, false),
(vec!["b"], 14, true),
(vec!["b", "0"], 14, true),
(vec!["b", "0", "x"], 3, false),
(vec!["b", "0", "y"], 2, false),
(vec!["b", "0", "z"], 9, true),
(vec!["b", "0", "z", "0"], 9, false),
]
);
}

#[test]
Expand All @@ -267,21 +276,24 @@ fn insert_existing() {
fn merge_test() {
let tree = example_tree_0().merge(example_tree_1());
let entries = to_sorted_entries(&tree);
assert_eq!(entries, vec![
(vec!["a"], 22, true),
(vec!["a", "0"], 14, true),
(vec!["a", "0", "x"], 3, false),
(vec!["a", "0", "y"], 2, false),
(vec!["a", "0", "z"], 9, true),
(vec!["a", "0", "z", "0"], 9, false),
(vec!["a", "1"], 9, true),
(vec!["a", "1", "x"], 9, true),
(vec!["a", "1", "x", "0"], 7, false),
(vec!["a", "1", "x", "1"], 2, false),
(vec!["a", "2"], 7, true),
(vec!["a", "2", "x"], 7, true),
(vec!["a", "2", "x", "0"], 7, false),
]);
assert_eq!(
entries,
vec![
(vec!["a"], 22, true),
(vec!["a", "0"], 14, true),
(vec!["a", "0", "x"], 3, false),
(vec!["a", "0", "y"], 2, false),
(vec!["a", "0", "z"], 9, true),
(vec!["a", "0", "z", "0"], 9, false),
(vec!["a", "1"], 9, true),
(vec!["a", "1", "x"], 9, true),
(vec!["a", "1", "x", "0"], 7, false),
(vec!["a", "1", "x", "1"], 2, false),
(vec!["a", "2"], 7, true),
(vec!["a", "2", "x"], 7, true),
(vec!["a", "2", "x", "0"], 7, false),
]
);
}

#[test]
Expand Down Expand Up @@ -495,14 +507,18 @@ fn lots_of_snapshots() {
// get_entry_details
let path_id = cache.get_path_id_by_path("a/0".into()).unwrap().unwrap();
let details = cache.get_entry_details(path_id).unwrap().unwrap();
assert_eq!(details, EntryDetails {
max_size: 4,
max_size_snapshot_hash: (NUM_SNAPSHOTS - 1).to_string(),
first_seen: timestamp_to_datetime(0).unwrap(),
first_seen_snapshot_hash: 0.to_string(),
last_seen: timestamp_to_datetime((NUM_SNAPSHOTS - 1) as i64).unwrap(),
last_seen_snapshot_hash: (NUM_SNAPSHOTS - 1).to_string(),
});
assert_eq!(
details,
EntryDetails {
max_size: 4,
max_size_snapshot_hash: (NUM_SNAPSHOTS - 1).to_string(),
first_seen: timestamp_to_datetime(0).unwrap(),
first_seen_snapshot_hash: 0.to_string(),
last_seen: timestamp_to_datetime((NUM_SNAPSHOTS - 1) as i64)
.unwrap(),
last_seen_snapshot_hash: (NUM_SNAPSHOTS - 1).to_string(),
}
);
}

////////// Migrations //////////////////////////////////////////////////////////
Expand Down Expand Up @@ -550,15 +566,18 @@ fn test_migrate_v0_to_v1() {
let cache =
Migrator::open_with_target(&file.0, 1).unwrap().migrate().unwrap();

assert_tables(&cache.conn, &[
"metadata_integer",
"paths",
"snapshots",
"snapshot_paths",
"snapshot_excludes",
"snapshot_tags",
"marks",
]);
assert_tables(
&cache.conn,
&[
"metadata_integer",
"paths",
"snapshots",
"snapshot_paths",
"snapshot_excludes",
"snapshot_tags",
"marks",
],
);

assert_marks(&cache, &marks);

Expand Down
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
#![feature(exit_status_error)]
#![feature(step_trait)]
#![feature(try_blocks)]
#![feature(iter_intersperse)]

pub mod cache;
pub mod restic;
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ fn main() -> anyhow::Result<()> {
.create_new(true)
.open(&path)
{
Err(err) if err.kind() == io::ErrorKind::AlreadyExists =>
path.set_file_name(generate_filename()),
Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {
path.set_file_name(generate_filename())
}
x => break x,
}
}?;
Expand Down Expand Up @@ -415,14 +416,15 @@ fn convert_event(event: crossterm::event::Event) -> Option<Event> {
];
match event {
TermEvent::Resize(w, h) => Some(Resize(Size::new(w, h))),
TermEvent::Key(event) if [Press, Release].contains(&event.kind) =>
TermEvent::Key(event) if [Press, Release].contains(&event.kind) => {
KEYBINDINGS.iter().find_map(|((mods, code), ui_event)| {
if event.modifiers == *mods && event.code == *code {
Some(ui_event.clone())
} else {
None
}
}),
})
}
_ => None,
}
}
Expand Down
73 changes: 47 additions & 26 deletions src/restic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use std::{
ffi::OsStr,
fmt::{self, Display, Formatter},
io::{self, BufRead, BufReader, Lines, Read, Write},
iter::Step,
marker::PhantomData,
mem,
process::{Child, ChildStdout, Command, ExitStatusError, Stdio},
process::{Child, ChildStdout, Command, Stdio},
str::Utf8Error,
};

Expand All @@ -34,8 +33,8 @@ pub enum RunError {
Utf8(#[from] Utf8Error),
#[error("error parsing JSON")]
Parse(#[from] serde_json::Error),
#[error("the restic process exited with an error code")]
Exit(#[from] ExitStatusError),
#[error("the restic process exited with error code {}", if let Some(code) = .0 { code.to_string() } else { "None".to_string() } )]
Exit(Option<i32>),
}

#[derive(Debug, Error)]
Expand Down Expand Up @@ -64,12 +63,6 @@ impl From<serde_json::Error> for ErrorKind {
}
}

impl From<ExitStatusError> for ErrorKind {
fn from(value: ExitStatusError) -> Self {
ErrorKind::Run(RunError::Exit(value))
}
}

#[derive(Debug, Error)]
pub struct Error {
#[source]
Expand Down Expand Up @@ -185,9 +178,13 @@ impl Restic {
kind: ErrorKind::Run(RunError::Io(e)),
stderr: None,
})?;
let r_value = try {
output.status.exit_ok()?;
serde_json::from_str(str::from_utf8(&output.stdout)?)?
let r_value: Result<T, ErrorKind> = if output.status.success() {
match str::from_utf8(&output.stdout) {
Ok(s) => serde_json::from_str(s).map_err(|e| e.into()),
Err(e) => Err(e.into()),
}
} else {
Err(ErrorKind::Run(RunError::Exit(output.status.code())))
};
match r_value {
Err(kind) => Err(Error {
Expand Down Expand Up @@ -299,11 +296,11 @@ impl<T: DeserializeOwned> Iterator for Iter<T> {
type Item = Result<T, Error>;

fn next(&mut self) -> Option<Self::Item> {
if let Some(line) = self.lines.next() {
let r_value = try {
let line = line?;
serde_json::from_str(&line)?
};
if let Some(r_line) = self.lines.next() {
let r_value: Result<T, ErrorKind> =
r_line.map_err(|e| e.into()).and_then(|line| {
serde_json::from_str(&line).map_err(|e| e.into())
});
Some(match r_value {
Err(kind) => {
self.finish();
Expand All @@ -314,12 +311,18 @@ impl<T: DeserializeOwned> Iterator for Iter<T> {
} else {
self.finish();
match self.child.wait() {
Err(e) =>
Some(self.read_stderr(ErrorKind::Run(RunError::Io(e)))),
Ok(status) => match status.exit_ok() {
Err(e) => Some(self.read_stderr(e.into())),
Ok(()) => None,
},
Err(e) => {
Some(self.read_stderr(ErrorKind::Run(RunError::Io(e))))
}
Ok(status) => {
if status.success() {
None
} else {
Some(self.read_stderr(ErrorKind::Run(RunError::Exit(
status.code(),
))))
}
}
}
}
}
Expand Down Expand Up @@ -362,12 +365,30 @@ pub fn escape_for_exclude(path: &str) -> Cow<str> {
['*', '?', '[', '\\', '\r', '\n'].contains(&c)
}

fn char_backward(c: char) -> char {
char::from_u32(
(c as u32).checked_sub(1).expect(
"char_backward: underflow when computing previous char",
),
)
.expect("char_backward: invalid resulting character")
}

fn char_forward(c: 777A char) -> char {
char::from_u32(
(c as u32)
.checked_add(1)
.expect("char_backward: overflow when computing next char"),
)
.expect("char_forward: invalid resulting character")
}

fn push_as_inverse_range(buf: &mut String, c: char) {
#[rustfmt::skip]
let cs = [
'[', '^',
char::MIN, '-', char::backward(c, 1),
char::forward(c, 1), '-', char::MAX,
char::MIN, '-', char_backward(c),
char_forward(c), '-', char::MAX,
']',
];
for d in cs {
Expand Down
Loading
0