Skip to content

Commit

Permalink
feat: utf8 paths (#18)
Browse files Browse the repository at this point in the history
* feat: utf8 paths

* ci fix

* debug

* bug fix

* bug fix
  • Loading branch information
shixinhuang99 committed Oct 26, 2023
1 parent f47d6ab commit 75b14d4
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 168 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
env:
CARGO_TERM_COLOR: always
CARGO_INCREMENTAL: 0
DEBUG_LOG: 1

jobs:
general-check:
Expand All @@ -21,7 +22,7 @@ jobs:
- name: Install rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2023-08-18
toolchain: nightly-2023-10-06
components: clippy, rustfmt

- name: Rust cache
Expand Down
166 changes: 159 additions & 7 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ exclude = ["assets/*"]

[dependencies]
anyhow = "1.0.70"
camino = "1.1.6"
chrono = "0.4.24"
clap = { version = "4.1.13", features = ["derive"] }
dircpy = "0.3.15"
flate2 = "1.0.25"
fs_extra = "1.3.0"
home = "0.5.4"
owo-colors = "3.5.0"
regex = "1.7.3"
Expand Down
4 changes: 2 additions & 2 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
# 1.73.0
channel = "nightly-2023-08-18"
# 1.75.0
channel = "nightly-2023-10-06"
1 change: 0 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ wrap_comments = true
reorder_impl_items = true
format_strings = true
hard_tabs = true
tab_spaces = 4
5 changes: 3 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use camino::Utf8PathBuf;
use clap::{
builder::{PossibleValuesParser, TypedValueParser as _},
Args, Parser, Subcommand,
Expand Down Expand Up @@ -78,7 +79,7 @@ pub struct AddArgs {

/// Specify scaffold name, if a subdir is provided, the last level of the
/// subdir will be used as the name
#[arg(short, long)]
#[arg(long)]
pub name: Option<String>,
}

Expand All @@ -88,7 +89,7 @@ pub struct CreateArgs {
pub name: String,

/// Specified directory(defaults to the current directory)
pub directory: Option<String>,
pub directory: Option<Utf8PathBuf>,
}

#[derive(Args, Debug)]
Expand Down
13 changes: 7 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::path::{Path, PathBuf};

use anyhow::Result;
use camino::{Utf8Path, Utf8PathBuf};
use serde::{Deserialize, Serialize};

use crate::toml_content::TomlContent;
Expand All @@ -13,12 +12,12 @@ struct ConfigContent {
impl TomlContent for ConfigContent {}

pub struct Config {
path: PathBuf,
path: Utf8PathBuf,
content: ConfigContent,
}

impl Config {
pub fn new(scafalra_dir: &Path) -> Result<Self> {
pub fn new(scafalra_dir: &Utf8Path) -> Result<Self> {
let path = scafalra_dir.join("config.toml");
let content = ConfigContent::load(&path)?;

Expand All @@ -43,21 +42,23 @@ mod tests {
use std::{fs, io::Write};

use anyhow::Result;
use camino::Utf8Path;
use pretty_assertions::assert_eq;
use tempfile::{tempdir, TempDir};

use super::Config;

fn build(create_file: bool) -> Result<(Config, TempDir)> {
let temp_dir = tempdir()?;
let temp_dir_path = Utf8Path::from_path(temp_dir.path()).unwrap();

if create_file {
let file_path = temp_dir.path().join("config.toml");
let file_path = temp_dir_path.join("config.toml");
let mut file = fs::File::create(file_path)?;
file.write_all(b"token = \"token\"\n")?;
}

let config = Config::new(temp_dir.path())?;
let config = Config::new(temp_dir_path)?;

Ok((config, temp_dir))
}
Expand Down
4 changes: 2 additions & 2 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::sync::atomic::{AtomicBool, Ordering};

static DEBUG: AtomicBool = AtomicBool::new(false);

pub fn trun_on_debug(val: bool) {
DEBUG.store(val, Ordering::Relaxed);
pub fn trun_on_debug() {
DEBUG.store(true, Ordering::Relaxed);
}

pub fn is_debug_mode() -> bool {
Expand Down
7 changes: 5 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::path::PathBuf;

use camino::Utf8PathBuf;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ScafalraError {
#[error("Error accessing specified path: `{}`", .0.display())]
IOError(PathBuf),
#[error("Error accessing specified path: `{}`", .0)]
IOError(Utf8PathBuf),
#[error("No GitHub personal access token configured")]
NoToken,
#[error("Call to GitHub api error")]
Expand All @@ -14,4 +15,6 @@ pub enum ScafalraError {
RepositoryParseError(String),
#[error("Serialization or deserialization errors")]
SerdeError,
#[error("`{}` it is not valid UTF-8 path", .0.display())]
NonUtf8Path(PathBuf),
}

0 comments on commit 75b14d4

Please sign in to comment.