From 2727e5f3b791d9bceae308982aca51eb1edaa1ac Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 5 Nov 2022 16:07:21 -0400 Subject: [PATCH] Remove some usages of Ruff internals in ruff_dev (#610) --- ruff_dev/src/generate_source_code.rs | 4 ++-- ruff_dev/src/print_ast.rs | 4 ++-- ruff_dev/src/print_tokens.rs | 4 ++-- src/fs.rs | 10 +++++----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/ruff_dev/src/generate_source_code.rs b/ruff_dev/src/generate_source_code.rs index e2b2fbd946072..91161543ba871 100644 --- a/ruff_dev/src/generate_source_code.rs +++ b/ruff_dev/src/generate_source_code.rs @@ -1,11 +1,11 @@ //! Run round-trip source code generation on a given Python file. +use std::fs; use std::path::PathBuf; use anyhow::Result; use clap::Args; use ruff::code_gen::SourceGenerator; -use ruff::fs; use rustpython_parser::parser; #[derive(Args)] @@ -16,7 +16,7 @@ pub struct Cli { } pub fn main(cli: &Cli) -> Result<()> { - let contents = fs::read_file(&cli.file)?; + let contents = fs::read_to_string(&cli.file)?; let python_ast = parser::parse_program(&contents, &cli.file.to_string_lossy())?; let mut generator = SourceGenerator::new(); generator.unparse_suite(&python_ast)?; diff --git a/ruff_dev/src/print_ast.rs b/ruff_dev/src/print_ast.rs index 33e4a4712b14d..edcfebfe3f53d 100644 --- a/ruff_dev/src/print_ast.rs +++ b/ruff_dev/src/print_ast.rs @@ -1,10 +1,10 @@ //! Print the AST for a given Python file. +use std::fs; use std::path::PathBuf; use anyhow::Result; use clap::Args; -use ruff::fs; use rustpython_parser::parser; #[derive(Args)] @@ -15,7 +15,7 @@ pub struct Cli { } pub fn main(cli: &Cli) -> Result<()> { - let contents = fs::read_file(&cli.file)?; + let contents = fs::read_to_string(&cli.file)?; let python_ast = parser::parse_program(&contents, &cli.file.to_string_lossy())?; println!("{:#?}", python_ast); Ok(()) diff --git a/ruff_dev/src/print_tokens.rs b/ruff_dev/src/print_tokens.rs index 12f796dbd2cb7..db1d4d2b51b55 100644 --- a/ruff_dev/src/print_tokens.rs +++ b/ruff_dev/src/print_tokens.rs @@ -1,10 +1,10 @@ //! Print the token stream for a given Python file. +use std::fs; use std::path::PathBuf; use anyhow::Result; use clap::Args; -use ruff::fs; use rustpython_parser::lexer; #[derive(Args)] @@ -15,7 +15,7 @@ pub struct Cli { } pub fn main(cli: &Cli) -> Result<()> { - let contents = fs::read_file(&cli.file)?; + let contents = fs::read_to_string(&cli.file)?; for (_, tok, _) in lexer::make_tokenizer(&contents).flatten() { println!("{:#?}", tok); } diff --git a/src/fs.rs b/src/fs.rs index b8f79a80fa480..888bf276d6d83 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -118,7 +118,7 @@ pub fn iter_python_files<'a>( } /// Create tree set with codes matching the pattern/code pairs. -pub fn ignores_from_path<'a>( +pub(crate) fn ignores_from_path<'a>( path: &Path, pattern_code_pairs: &'a [PerFileIgnore], ) -> Result> { @@ -138,7 +138,7 @@ pub fn ignores_from_path<'a>( /// Convert any path to an absolute path (based on the current working /// directory). -pub fn normalize_path(path: &Path) -> PathBuf { +pub(crate) fn normalize_path(path: &Path) -> PathBuf { if let Ok(path) = path.absolutize() { return path.to_path_buf(); } @@ -146,7 +146,7 @@ pub fn normalize_path(path: &Path) -> PathBuf { } /// Convert any path to an absolute path (based on the specified project root). -pub fn normalize_path_to(path: &Path, project_root: &Path) -> PathBuf { +pub(crate) fn normalize_path_to(path: &Path, project_root: &Path) -> PathBuf { if let Ok(path) = path.absolutize_from(project_root) { return path.to_path_buf(); } @@ -154,7 +154,7 @@ pub fn normalize_path_to(path: &Path, project_root: &Path) -> PathBuf { } /// Convert an absolute path to be relative to the current working directory. -pub fn relativize_path(path: &Path) -> Cow { +pub(crate) fn relativize_path(path: &Path) -> Cow { if let Ok(path) = path.strip_prefix(path_dedot::CWD.deref()) { return path.to_string_lossy(); } @@ -162,7 +162,7 @@ pub fn relativize_path(path: &Path) -> Cow { } /// Read a file's contents from disk. -pub fn read_file(path: &Path) -> Result { +pub(crate) fn read_file(path: &Path) -> Result { let file = File::open(path)?; let mut buf_reader = BufReader::new(file); let mut contents = String::new();