Skip to content

Commit eff3598

Browse files
authored
Merge pull request #34 from SoptikHa2/setup-ci
feat(ci): add build, test and clippy
2 parents 8127c07 + 793592f commit eff3598

File tree

8 files changed

+88
-52
lines changed

8 files changed

+88
-52
lines changed

.github/workflows/rust.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Rust
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- name: Build
18+
run: cargo build --verbose
19+
- name: Run tests
20+
run: cargo test --verbose
21+
22+
lint:
23+
name: Check code style
24+
runs-on: ubuntu-latest
25+
steps:
26+
- uses: actions/checkout@v4
27+
name: Checkout repository
28+
- uses: dtolnay/rust-toolchain@master
29+
name: Set up toolchain
30+
with:
31+
toolchain: stable
32+
components: rustfmt, clippy
33+
- uses: Swatinem/rust-cache@v2
34+
name: Cache toolchain and dependencies
35+
- uses: actions-rs/cargo@v1
36+
name: Check code with cargo fmt
37+
with:
38+
command: fmt
39+
args: --all -- --check
40+
- uses: actions-rs/cargo@v1
41+
name: Check code with cargo clippy
42+
with:
43+
command: clippy
44+
args: --all-targets -- -D warnings

src/cli.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ pub struct Options {
7878
impl Options {
7979
pub fn from_matches(matches: ArgMatches) -> Result<Options> {
8080
// UNWRAP: It's safe because we define sed-script in the CLI code above, so we are certain it exists.
81-
let sed_script: PathBuf = PathBuf::from_str(matches.get_one::<String>("sed-script").unwrap())
82-
.with_context(|| "Failed to load sed script path")?;
81+
let sed_script: PathBuf =
82+
PathBuf::from_str(matches.get_one::<String>("sed-script").unwrap())
83+
.with_context(|| "Failed to load sed script path")?;
8384
// UNWRAP: It's safe because we define input-file in the CLI code above, so we are certain it exists.
84-
let input_file: PathBuf = PathBuf::from_str(matches.get_one::<String>("input-file").unwrap())
85-
.with_context(|| "Failed to load input file path.")?;
85+
let input_file: PathBuf =
86+
PathBuf::from_str(matches.get_one::<String>("input-file").unwrap())
87+
.with_context(|| "Failed to load input file path.")?;
8688

8789
let sed_path: Option<String> = matches.get_one::<String>("sed-path").map(ToOwned::to_owned);
8890

src/file_watcher/inotify.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ use inotify::Inotify;
77

88
pub struct FileWatcherImpl {
99
inotify: Inotify,
10-
watches: Vec<FileWatchImpl>
10+
watches: Vec<FileWatchImpl>,
1111
}
1212

1313
pub struct FileWatchImpl {
1414
descriptor: inotify::WatchDescriptor,
1515
}
1616

1717
impl FileWatcherImpl {
18-
pub fn init() -> Result<FileWatcherImpl> {
18+
pub fn init() -> Result<FileWatcherImpl> {
1919
let ino = match Inotify::init() {
2020
Ok(i) => i,
2121
Err(msg) => return Result::Err(msg),
2222
};
2323

2424
Result::Ok(FileWatcherImpl {
2525
inotify: ino,
26-
watches: vec![]
26+
watches: vec![],
2727
})
2828
}
2929

@@ -35,9 +35,7 @@ impl FileWatcherImpl {
3535
Err(msg) => return Result::Err(msg),
3636
};
3737

38-
let fw = FileWatchImpl {
39-
descriptor: watch,
40-
};
38+
let fw = FileWatchImpl { descriptor: watch };
4139

4240
self.watches.push(fw);
4341
return Result::Ok(self.watches.last().unwrap());
@@ -54,7 +52,7 @@ impl FileWatcherImpl {
5452

5553
Result::Err(Error::new(
5654
ErrorKind::InvalidInput,
57-
"Passed FileWatch does not belong to this FileWatcher instance"
55+
"Passed FileWatch does not belong to this FileWatcher instance",
5856
))
5957
}
6058

src/file_watcher/kqueue.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use std::path::PathBuf;
44
use std::vec::Vec;
55

66
extern crate kqueue;
7+
use kqueue::FilterFlag;
78
use kqueue::Watcher as KQueue;
8-
use kqueue::FilterFlag as FilterFlag;
99

1010
const FILTER: kqueue::EventFilter = kqueue::EventFilter::EVFILT_VNODE;
1111

1212
pub struct FileWatcherImpl {
1313
kq: KQueue,
14-
watches: Vec<FileWatchImpl>
14+
watches: Vec<FileWatchImpl>,
1515
}
1616

1717
pub struct FileWatchImpl {
@@ -25,57 +25,51 @@ impl FileWatcherImpl {
2525
Err(msg) => return Result::Err(msg),
2626
};
2727

28-
return Result::Ok(FileWatcherImpl {
28+
Ok(FileWatcherImpl {
2929
kq,
30-
watches: vec![]
31-
});
30+
watches: vec![],
31+
})
3232
}
3333

3434
pub fn add_watch(&mut self, file_path: &PathBuf) -> Result<&FileWatchImpl> {
35-
let flags: FilterFlag =
36-
FilterFlag::NOTE_WRITE |
37-
FilterFlag::NOTE_EXTEND |
38-
FilterFlag::NOTE_RENAME |
39-
FilterFlag::NOTE_DELETE |
40-
FilterFlag::NOTE_LINK;
35+
let flags: FilterFlag = FilterFlag::NOTE_WRITE
36+
| FilterFlag::NOTE_EXTEND
37+
| FilterFlag::NOTE_RENAME
38+
| FilterFlag::NOTE_DELETE
39+
| FilterFlag::NOTE_LINK;
4140

4241
let file = File::open(file_path)?;
43-
let _ = match self.kq.add_file(&file, FILTER, flags) {
44-
Ok(w) => w,
45-
Err(msg) => return Result::Err(msg),
46-
};
42+
self.kq.add_file(&file, FILTER, flags)?;
4743

48-
let fw = FileWatchImpl {
49-
file
50-
};
44+
let fw = FileWatchImpl { file };
5145

5246
self.watches.push(fw);
53-
return Result::Ok(&self.watches.last().unwrap());
47+
Ok(self.watches.last().unwrap())
5448
}
5549

5650
pub fn rm_watch(&mut self, fw: &FileWatchImpl) -> Result<()> {
5751
for i in 0..self.watches.len() {
5852
let item_ref = self.watches.get(i).unwrap();
59-
if item_ref as *const FileWatchImpl == fw as *const FileWatchImpl {
53+
if std::ptr::eq(item_ref, fw) {
6054
let item = self.watches.remove(i);
6155
return self.kq.remove_file(&item.file, FILTER);
6256
}
6357
}
6458

65-
return Result::Err(Error::new(
59+
Err(Error::new(
6660
ErrorKind::InvalidInput,
67-
"Passed FileWatch does not belong to this FileWatcher instance"
68-
));
61+
"Passed FileWatch does not belong to this FileWatcher instance",
62+
))
6963
}
7064

7165
pub fn start(&mut self) -> Result<()> {
72-
return self.kq.watch();
66+
self.kq.watch()
7367
}
7468

7569
pub fn any_events(&mut self) -> Result<bool> {
7670
match self.kq.poll(None) {
77-
Some(_) => return Result::Ok(true),
78-
None => return Result::Ok(false),
71+
Some(_) => Ok(true),
72+
None => Ok(false),
7973
}
8074
}
8175
}

src/file_watcher/mock.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ use std::vec::Vec;
44

55
pub struct FileWatcherImpl {
66
highest_id: usize,
7-
watches: Vec<FileWatchImpl>
7+
watches: Vec<FileWatchImpl>,
88
}
99

1010
pub struct FileWatchImpl {
1111
id: usize,
1212
}
1313

1414
impl FileWatcherImpl {
15-
pub fn init() -> Result<FileWatcherImpl> {
15+
pub fn init() -> Result<FileWatcherImpl> {
1616
return Result::Ok(FileWatcherImpl {
1717
highest_id: 0,
18-
watches: vec![]
18+
watches: vec![],
1919
});
2020
}
2121

@@ -41,7 +41,7 @@ impl FileWatcherImpl {
4141

4242
return Result::Err(Error::new(
4343
ErrorKind::InvalidInput,
44-
"Passed FileWatch does not belong to this FileWatcher instance"
44+
"Passed FileWatch does not belong to this FileWatcher instance",
4545
));
4646
}
4747

src/main.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ mod sed;
22
use sed::debugger::Debugger;
33
mod cli;
44
use cli::Options;
5-
mod ui;
65
mod file_watcher;
7-
use file_watcher::FileWatcher;
6+
mod ui;
87
use anyhow::Result;
8+
use file_watcher::FileWatcher;
99
use ui::generic::{ApplicationExitReason, UiAgent};
1010
use ui::tui::Tui;
1111

@@ -41,11 +41,7 @@ fn run(target_state_number: usize) -> Result<()> {
4141
let debugger = Debugger::new(settings)?;
4242
let tui = Tui::new(&debugger, watcher, target_state_number)?;
4343
match tui.start()? {
44-
ApplicationExitReason::UserExit => {
45-
Ok(())
46-
}
47-
ApplicationExitReason::Reload(instruction_number) => {
48-
run(instruction_number)
49-
}
44+
ApplicationExitReason::UserExit => Ok(()),
45+
ApplicationExitReason::Reload(instruction_number) => run(instruction_number),
5046
}
5147
}

src/sed/communication.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ impl SedCommunicator {
3030
path_to_be_used = path;
3131
}
3232

33-
let mandatory_parameters = ["--debug",
33+
let mandatory_parameters = [
34+
"--debug",
3435
"-f",
3536
self.options
3637
.sed_script
@@ -39,7 +40,8 @@ impl SedCommunicator {
3940
self.options
4041
.input_file
4142
.to_str()
42-
.with_context(|| "Invalid input path. Is it valid UTF-8?".to_string())?];
43+
.with_context(|| "Invalid input path. Is it valid UTF-8?".to_string())?,
44+
];
4345
let constructed_cmd_line = self
4446
.options
4547
.sed_parameters

src/sed/debugger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl Debugger {
5252
})
5353
}
5454
/// Peek at state with target number (0-based).
55-
///
55+
///
5656
/// This will return None if the state doesn't exist.
5757
pub fn peek_at_state(&self, frame: usize) -> Option<&DebuggingState> {
5858
self.state_frames.get(frame)

0 commit comments

Comments
 (0)