mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 10:48:32 +00:00
Move some generic structs out of isort
(#3788)
This commit is contained in:
parent
44ae3237b8
commit
f79506f5a4
3 changed files with 65 additions and 57 deletions
61
crates/ruff_python_ast/src/imports.rs
Normal file
61
crates/ruff_python_ast/src/imports.rs
Normal file
|
@ -0,0 +1,61 @@
|
|||
use std::fmt;
|
||||
|
||||
/// A representation of an individual name imported via any import statement.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum AnyImport<'a> {
|
||||
Import(Import<'a>),
|
||||
ImportFrom(ImportFrom<'a>),
|
||||
}
|
||||
|
||||
/// A representation of an individual name imported via an `import` statement.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Import<'a> {
|
||||
pub name: Alias<'a>,
|
||||
}
|
||||
|
||||
/// A representation of an individual name imported via a `from ... import` statement.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ImportFrom<'a> {
|
||||
pub module: Option<&'a str>,
|
||||
pub name: Alias<'a>,
|
||||
pub level: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Alias<'a> {
|
||||
pub name: &'a str,
|
||||
pub as_name: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl fmt::Display for AnyImport<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
AnyImport::Import(import) => write!(f, "{import}"),
|
||||
AnyImport::ImportFrom(import_from) => write!(f, "{import_from}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Import<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "import {}", self.name.name)?;
|
||||
if let Some(as_name) = self.name.as_name {
|
||||
write!(f, " as {as_name}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ImportFrom<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "from ")?;
|
||||
if let Some(level) = self.level {
|
||||
write!(f, "{}", ".".repeat(level))?;
|
||||
}
|
||||
if let Some(module) = self.module {
|
||||
write!(f, "{module}")?;
|
||||
}
|
||||
write!(f, " import {}", self.name.name)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ pub mod context;
|
|||
pub mod function_type;
|
||||
pub mod hashable;
|
||||
pub mod helpers;
|
||||
pub mod imports;
|
||||
pub mod logging;
|
||||
pub mod newlines;
|
||||
pub mod operations;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue