Move some generic structs out of isort (#3788)

This commit is contained in:
Charlie Marsh 2023-03-30 08:58:01 -04:00 committed by GitHub
parent 44ae3237b8
commit f79506f5a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 57 deletions

View 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(())
}
}

View file

@ -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;