Skip to content
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

feat(ci): add build, test and clippy #34

Merged
merged 2 commits into from
Oct 17, 2024
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
44 changes: 44 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Rust

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose

lint:
name: Check code style
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
name: Checkout repository
- uses: dtolnay/rust-toolchain@master
name: Set up toolchain
with:
toolchain: stable
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
name: Cache toolchain and dependencies
- uses: actions-rs/cargo@v1
name: Check code with cargo fmt
with:
command: fmt
args: --all -- --check
- uses: actions-rs/cargo@v1
name: Check code with cargo clippy
with:
command: clippy
args: --all-targets -- -D warnings
10 changes: 6 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ pub struct Options {
impl Options {
pub fn from_matches(matches: ArgMatches) -> Result<Options> {
// UNWRAP: It's safe because we define sed-script in the CLI code above, so we are certain it exists.
let sed_script: PathBuf = PathBuf::from_str(matches.get_one::<String>("sed-script").unwrap())
.with_context(|| "Failed to load sed script path")?;
let sed_script: PathBuf =
PathBuf::from_str(matches.get_one::<String>("sed-script").unwrap())
.with_context(|| "Failed to load sed script path")?;
// UNWRAP: It's safe because we define input-file in the CLI code above, so we are certain it exists.
let input_file: PathBuf = PathBuf::from_str(matches.get_one::<String>("input-file").unwrap())
.with_context(|| "Failed to load input file path.")?;
let input_file: PathBuf =
PathBuf::from_str(matches.get_one::<String>("input-file").unwrap())
.with_context(|| "Failed to load input file path.")?;

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

Expand Down
12 changes: 5 additions & 7 deletions src/file_watcher/inotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ use inotify::Inotify;

pub struct FileWatcherImpl {
inotify: Inotify,
watches: Vec<FileWatchImpl>
watches: Vec<FileWatchImpl>,
}

pub struct FileWatchImpl {
descriptor: inotify::WatchDescriptor,
}

impl FileWatcherImpl {
pub fn init() -> Result<FileWatcherImpl> {
pub fn init() -> Result<FileWatcherImpl> {
let ino = match Inotify::init() {
Ok(i) => i,
Err(msg) => return Result::Err(msg),
};

Result::Ok(FileWatcherImpl {
inotify: ino,
watches: vec![]
watches: vec![],
})
}

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

let fw = FileWatchImpl {
descriptor: watch,
};
let fw = FileWatchImpl { descriptor: watch };

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

Result::Err(Error::new(
ErrorKind::InvalidInput,
"Passed FileWatch does not belong to this FileWatcher instance"
"Passed FileWatch does not belong to this FileWatcher instance",
))
}

Expand Down
46 changes: 20 additions & 26 deletions src/file_watcher/kqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use std::path::PathBuf;
use std::vec::Vec;

extern crate kqueue;
use kqueue::FilterFlag;
use kqueue::Watcher as KQueue;
use kqueue::FilterFlag as FilterFlag;

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

pub struct FileWatcherImpl {
kq: KQueue,
watches: Vec<FileWatchImpl>
watches: Vec<FileWatchImpl>,
}

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

return Result::Ok(FileWatcherImpl {
Ok(FileWatcherImpl {
kq,
watches: vec![]
});
watches: vec![],
})
}

pub fn add_watch(&mut self, file_path: &PathBuf) -> Result<&FileWatchImpl> {
let flags: FilterFlag =
FilterFlag::NOTE_WRITE |
FilterFlag::NOTE_EXTEND |
FilterFlag::NOTE_RENAME |
FilterFlag::NOTE_DELETE |
FilterFlag::NOTE_LINK;
let flags: FilterFlag = FilterFlag::NOTE_WRITE
| FilterFlag::NOTE_EXTEND
| FilterFlag::NOTE_RENAME
| FilterFlag::NOTE_DELETE
| FilterFlag::NOTE_LINK;

let file = File::open(file_path)?;
let _ = match self.kq.add_file(&file, FILTER, flags) {
Ok(w) => w,
Err(msg) => return Result::Err(msg),
};
self.kq.add_file(&file, FILTER, flags)?;

let fw = FileWatchImpl {
file
};
let fw = FileWatchImpl { file };

self.watches.push(fw);
return Result::Ok(&self.watches.last().unwrap());
Ok(self.watches.last().unwrap())
}

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

return Result::Err(Error::new(
Err(Error::new(
ErrorKind::InvalidInput,
"Passed FileWatch does not belong to this FileWatcher instance"
));
"Passed FileWatch does not belong to this FileWatcher instance",
))
}

pub fn start(&mut self) -> Result<()> {
return self.kq.watch();
self.kq.watch()
}

pub fn any_events(&mut self) -> Result<bool> {
match self.kq.poll(None) {
Some(_) => return Result::Ok(true),
None => return Result::Ok(false),
Some(_) => Ok(true),
None => Ok(false),
}
}
}
8 changes: 4 additions & 4 deletions src/file_watcher/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ use std::vec::Vec;

pub struct FileWatcherImpl {
highest_id: usize,
watches: Vec<FileWatchImpl>
watches: Vec<FileWatchImpl>,
}

pub struct FileWatchImpl {
id: usize,
}

impl FileWatcherImpl {
pub fn init() -> Result<FileWatcherImpl> {
pub fn init() -> Result<FileWatcherImpl> {
return Result::Ok(FileWatcherImpl {
highest_id: 0,
watches: vec![]
watches: vec![],
});
}

Expand All @@ -41,7 +41,7 @@ impl FileWatcherImpl {

return Result::Err(Error::new(
ErrorKind::InvalidInput,
"Passed FileWatch does not belong to this FileWatcher instance"
"Passed FileWatch does not belong to this FileWatcher instance",
));
}

Expand Down
12 changes: 4 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ mod sed;
use sed::debugger::Debugger;
mod cli;
use cli::Options;
mod ui;
mod file_watcher;
use file_watcher::FileWatcher;
mod ui;
use anyhow::Result;
use file_watcher::FileWatcher;
use ui::generic::{ApplicationExitReason, UiAgent};
use ui::tui::Tui;

Expand Down Expand Up @@ -41,11 +41,7 @@ fn run(target_state_number: usize) -> Result<()> {
let debugger = Debugger::new(settings)?;
let tui = Tui::new(&debugger, watcher, target_state_number)?;
match tui.start()? {
ApplicationExitReason::UserExit => {
Ok(())
}
ApplicationExitReason::Reload(instruction_number) => {
run(instruction_number)
}
ApplicationExitReason::UserExit => Ok(()),
ApplicationExitReason::Reload(instruction_number) => run(instruction_number),
}
}
6 changes: 4 additions & 2 deletions src/sed/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ impl SedCommunicator {
path_to_be_used = path;
}

let mandatory_parameters = ["--debug",
let mandatory_parameters = [
"--debug",
"-f",
self.options
.sed_script
Expand All @@ -39,7 +40,8 @@ impl SedCommunicator {
self.options
.input_file
.to_str()
.with_context(|| "Invalid input path. Is it valid UTF-8?".to_string())?];
.with_context(|| "Invalid input path. Is it valid UTF-8?".to_string())?,
];
let constructed_cmd_line = self
.options
.sed_parameters
Expand Down
2 changes: 1 addition & 1 deletion src/sed/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Debugger {
})
}
/// Peek at state with target number (0-based).
///
///
/// This will return None if the state doesn't exist.
pub fn peek_at_state(&self, frame: usize) -> Option<&DebuggingState> {
self.state_frames.get(frame)
Expand Down
Loading