Skip to content

Commit

Permalink
fix: replace new constructor with try_from for struct Config
Browse files Browse the repository at this point in the history
  • Loading branch information
Mcdostone committed Jan 18, 2025
1 parent f2f7a9b commit 75687d7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/semver-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ permissions:

on:
pull_request:
types: [review_requested, ready_for_review]
types: [opened, reopened, synchronize, ready_for_review]
branches:
- main
paths-ignore:
Expand Down Expand Up @@ -48,6 +48,13 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Delete old cargo-semver-checks comments
continue-on-error: true
run: |
gh pr view "${{ github.event.number }}" --json comments --jq '.comments[] | select(.author.login == "github-actions") | .url' | grep -E 'issuecomment-(.+)' | cut -d '-' -f2 > comments.txt
xargs -I {} gh api -X DELETE "/repos/MAIF/yozefu/issues/comments/{}" < comments.txt
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/target
target
.vscode
**/*DS_Store
akhq
Expand All @@ -8,6 +8,7 @@ lol/rust/Cargo.lock
semver-checks/
demo.sh
ee.json
.github/

crates/**/*.cdx.xml
crates/**/*.cdx.json
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ services:
timeout: 1s
retries: 10

yozefu:
container_name: yozefu
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped

akhq:
container_name: yozefu-akhq
image: tchiotludo/akhq
Expand Down
25 changes: 19 additions & 6 deletions crates/app/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ fn default_show_shortcuts() -> bool {
true
}

impl Config {
pub fn new(path: &Path) -> Self {
Self {
impl TryFrom<&PathBuf> for Config {
type Error = Error;

fn try_from(path: &PathBuf) -> Result<Self, Self::Error> {
Ok(Self {
path: path.to_path_buf(),
yozefu_directory: PathBuf::default(),
yozefu_directory: Self::yozefu_directory()?,
logs: None,
default_url_template: default_url_template(),
history: EXAMPLE_PROMPTS.iter().map(|e| e.to_string()).collect_vec(),
Expand All @@ -126,9 +128,11 @@ impl Config {
theme: default_theme(),
show_shortcuts: true,
export_directory: default_export_directory(),
}
})
}
}

impl Config {
/// The default config file path
pub fn path() -> Result<PathBuf, Error> {
Self::yozefu_directory().map(|d| d.join("config.json"))
Expand All @@ -145,7 +149,16 @@ impl Config {

/// Reads a configuration file.
pub fn read(file: &Path) -> Result<Config, Error> {
let content = fs::read_to_string(file)?;
let content = fs::read_to_string(file);
if let Err(e) = &content {
return Err(Error::Error(format!(
"Failed to read the configuration file {:?}: {}",
file.display(),
e
)));
}

let content = content.unwrap();
let mut config: Config = serde_json::from_str(&content).map_err(|e| {
Error::Error(format!(
"Failed to parse the configuration file {:?}: {}",
Expand Down
2 changes: 1 addition & 1 deletion crates/command/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn init_config_file() -> Result<PathBuf, Error> {
if fs::metadata(&path).is_ok() {
return Ok(path);
}
let mut config = Config::new(&path);
let mut config = Config::try_from(&path)?;
let mut localhost_config = IndexMap::new();
localhost_config.insert(
"bootstrap.servers".to_string(),
Expand Down

0 comments on commit 75687d7

Please sign in to comment.