Skip to content

Commit

Permalink
Import grouping (#701)
Browse files Browse the repository at this point in the history
Part of #155 

This is fairly standard practice - we group the imports in the `std` >
external libraries > current crate categories, separated by newlines.

Some links:
- rust-lang/style-team#24
- rust-lang/rustfmt#1302
- https://www.reddit.com/r/rust/comments/wwbxhw/comment/ilkid50/
  • Loading branch information
Xanewok authored Dec 8, 2023
1 parent 678545e commit 68b8a93
Show file tree
Hide file tree
Showing 118 changed files with 252 additions and 188 deletions.
4 changes: 2 additions & 2 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#
# rustfmt nightly configuration options:
# https://github.com/rust-lang/rustfmt/blob/master/Configurations.md
#

group_imports = "StdExternalCrate"
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use crate::{
compiler::{
analysis::{Analysis, ItemMetadata},
Expand All @@ -6,7 +8,6 @@ use crate::{
internals::Spanned,
model::{Identifier, SpannedItem, SpannedVersionSpecifier},
};
use std::collections::HashSet;

pub(crate) fn analyze_definitions(analysis: &mut Analysis) {
collect_top_level_items(analysis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ mod reachability;
mod references;
mod utils;

use std::rc::Rc;

use indexmap::IndexMap;
use proc_macro2::Span;

use crate::{
compiler::analysis::{
definitions::analyze_definitions, reachability::analyze_reachability,
Expand All @@ -12,9 +17,6 @@ use crate::{
internals::{ErrorsCollection, ParseOutput, Spanned},
model::{Identifier, SpannedItem, SpannedLanguage},
};
use indexmap::IndexMap;
use proc_macro2::Span;
use std::rc::Rc;

pub(crate) struct Analysis {
pub errors: ErrorsCollection,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::collections::HashSet;

use crate::{
compiler::{analysis::Analysis, version_set::VersionSet},
model::{Identifier, SpannedTriviaParser},
};
use std::collections::HashSet;

pub(crate) fn analyze_reachability(analysis: &mut Analysis) {
check_unreachabable_items(analysis);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use std::fmt::Debug;

use indexmap::IndexMap;
use semver::Version;

use crate::{
compiler::{analysis::Analysis, version_set::VersionSet},
internals::Spanned,
Expand All @@ -14,9 +19,6 @@ use crate::{
SpannedVersionSpecifier,
},
};
use indexmap::IndexMap;
use semver::Version;
use std::fmt::Debug;

pub(crate) fn analyze_references(analysis: &mut Analysis) {
let language = analysis.language.clone();
Expand Down
3 changes: 2 additions & 1 deletion crates/codegen/language/definition/src/compiler/emitter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{compiler::analysis::Analysis, internals::WriteOutputTokens};
use inflector::Inflector;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use crate::{compiler::analysis::Analysis, internals::WriteOutputTokens};

pub(crate) struct LanguageEmitter;

impl LanguageEmitter {
Expand Down
3 changes: 2 additions & 1 deletion crates/codegen/language/definition/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ mod analysis;
mod emitter;
mod version_set;

use proc_macro2::TokenStream;

use crate::{
compiler::{analysis::Analysis, emitter::LanguageEmitter},
internals::ParseAdapter,
};
use proc_macro2::TokenStream;

pub struct LanguageCompiler;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use semver::Version;
use std::{fmt::Display, mem::swap, ops::Range};

use semver::Version;

const MAX_VERSION: Version = Version::new(u64::MAX, u64::MAX, u64::MAX);

#[derive(Clone, Debug, Default)]
Expand Down
3 changes: 2 additions & 1 deletion crates/codegen/language/definition/src/internals/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use proc_macro2::{Span, TokenStream};
use std::fmt::Display;

use proc_macro2::{Span, TokenStream};

pub type Result<T> = std::result::Result<T, Error>;

/// Our own proxy for [`syn::Error`] since the latter does not expose the underlying sub-errors.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use proc_macro2::TokenStream;
use syn::parse::{Parse, ParseStream};

use crate::{
internals::{ErrorsCollection, ParseInputTokens, Result},
model::SpannedLanguage,
};
use proc_macro2::TokenStream;
use syn::parse::{Parse, ParseStream};

pub(crate) struct ParseAdapter;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::internals::{
parse_input_tokens::ParseHelpers, ErrorsCollection, ParseInputTokens, Result, Spanned,
};
use std::{fmt::Debug, rc::Rc};

use indexmap::{IndexMap, IndexSet};
use proc_macro2::Ident;
use semver::Version;
use std::{fmt::Debug, rc::Rc};
use syn::{parse::ParseStream, LitBool, LitChar, LitStr};

use crate::internals::{
parse_input_tokens::ParseHelpers, ErrorsCollection, ParseInputTokens, Result, Spanned,
};

impl ParseInputTokens for bool {
fn parse_value(input: ParseStream<'_>, _: &mut ErrorsCollection) -> Result<Self> {
let literal = ParseHelpers::syn::<LitBool>(input)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::internals::{Error, ErrorsCollection, ParseInputTokens, Result, Spanned};
use std::fmt::Debug;

use indexmap::IndexMap;
use proc_macro2::{extra::DelimSpan, Delimiter, Ident, TokenStream};
use std::fmt::Debug;
use syn::{braced, bracketed, parenthesized, parse::ParseStream, Token};

use crate::internals::{Error, ErrorsCollection, ParseInputTokens, Result, Spanned};

pub struct ParseHelpers;

impl ParseHelpers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ mod helpers;

pub(crate) use adapter::*;
pub(crate) use helpers::*;
use syn::parse::ParseStream;

use crate::internals::{ErrorsCollection, Result};
use syn::parse::ParseStream;

pub trait ParseInputTokens: Sized {
/// Main parser entrypoint, and should be implemented by all types.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::internals::{ErrorsCollection, ParseInputTokens, Result, WriteOutputTokens};
use proc_macro2::{Span, TokenStream};
use std::cmp::Ordering;

use proc_macro2::{Span, TokenStream};
use syn::parse::ParseStream;

use crate::internals::{ErrorsCollection, ParseInputTokens, Result, WriteOutputTokens};

#[derive(Clone, Debug)]
pub struct Spanned<T> {
span: Span,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::internals::WriteOutputTokens;
use std::rc::Rc;

use indexmap::{IndexMap, IndexSet};
use proc_macro2::{Literal, TokenStream};
use quote::{format_ident, quote};
use semver::Version;
use std::rc::Rc;

use crate::internals::WriteOutputTokens;

impl WriteOutputTokens for bool {
fn write_output_tokens(&self) -> TokenStream {
Expand Down
7 changes: 4 additions & 3 deletions crates/codegen/language/definition/src/model/item.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use serde::{Deserialize, Serialize};
use strum_macros::EnumDiscriminants;

use crate::model::{
EnumItem, FragmentItem, Identifier, KeywordItem, PrecedenceItem, RepeatedItem, SeparatedItem,
StructItem, TokenItem, TriviaItem,
};
use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use serde::{Deserialize, Serialize};
use strum_macros::EnumDiscriminants;

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(EnumDiscriminants, ParseInputTokens, WriteOutputTokens)]
Expand Down
6 changes: 4 additions & 2 deletions crates/codegen/language/definition/src/model/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::model::{Field, Identifier, Item, TriviaParser, VersionSpecifier};
use std::{collections::BTreeSet, rc::Rc};

use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use indexmap::IndexSet;
use semver::Version;
use serde::{Deserialize, Serialize};
use std::{collections::BTreeSet, rc::Rc};

use crate::model::{Field, Identifier, Item, TriviaParser, VersionSpecifier};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(ParseInputTokens, WriteOutputTokens)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::model::{Identifier, VersionSpecifier};
use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use serde::{Deserialize, Serialize};

use crate::model::{Identifier, VersionSpecifier};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(ParseInputTokens, WriteOutputTokens)]
pub struct EnumItem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::model::{Identifier, VersionSpecifier};
use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use serde::{Deserialize, Serialize};

use crate::model::{Identifier, VersionSpecifier};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(ParseInputTokens, WriteOutputTokens)]
pub struct FieldsErrorRecovery {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::model::{Field, FieldsErrorRecovery, Identifier, VersionSpecifier};
use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

use crate::model::{Field, FieldsErrorRecovery, Identifier, VersionSpecifier};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(ParseInputTokens, WriteOutputTokens)]
pub struct PrecedenceItem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::model::{Identifier, VersionSpecifier};
use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use serde::{Deserialize, Serialize};

use crate::model::{Identifier, VersionSpecifier};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(ParseInputTokens, WriteOutputTokens)]
pub struct RepeatedItem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::model::{Identifier, VersionSpecifier};
use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use serde::{Deserialize, Serialize};

use crate::model::{Identifier, VersionSpecifier};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(ParseInputTokens, WriteOutputTokens)]
pub struct SeparatedItem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::model::{Field, FieldsErrorRecovery, Identifier, VersionSpecifier};
use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

use crate::model::{Field, FieldsErrorRecovery, Identifier, VersionSpecifier};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(ParseInputTokens, WriteOutputTokens)]
pub struct StructItem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::model::{Identifier, Scanner, VersionSpecifier};
use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use serde::{Deserialize, Serialize};

use crate::model::{Identifier, Scanner, VersionSpecifier};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(ParseInputTokens, WriteOutputTokens)]
pub struct FragmentItem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::model::{Identifier, VersionSpecifier};
use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use itertools::Itertools;
use serde::{Deserialize, Serialize};

use crate::model::{Identifier, VersionSpecifier};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(ParseInputTokens, WriteOutputTokens)]
pub struct KeywordItem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::model::Identifier;
use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use indexmap::IndexSet;
use serde::{Deserialize, Serialize};

use crate::model::Identifier;

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(ParseInputTokens, WriteOutputTokens)]
pub enum Scanner {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::model::{Identifier, Scanner, VersionSpecifier};
use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use serde::{Deserialize, Serialize};

use crate::model::{Identifier, Scanner, VersionSpecifier};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(ParseInputTokens, WriteOutputTokens)]
pub struct TokenItem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::model::{Identifier, Scanner};
use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use serde::{Deserialize, Serialize};

use crate::model::{Identifier, Scanner};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(ParseInputTokens, WriteOutputTokens)]
pub enum TriviaParser {
Expand Down
10 changes: 6 additions & 4 deletions crates/codegen/language/definition/src/model/utils/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::internals::{
ErrorsCollection, ParseHelpers, ParseInputTokens, Result, WriteOutputTokens,
};
use std::ops::Deref;

use proc_macro2::{Literal, TokenStream};
use quote::quote;
use serde::{Deserialize, Serialize};
use std::ops::Deref;
use syn::{parse::ParseStream, Ident};

use crate::internals::{
ErrorsCollection, ParseHelpers, ParseInputTokens, Result, WriteOutputTokens,
};

/// A wrapper type to make sure the DSL token is written as an identifier instead of a string literal.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Identifier {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::input_model::{strip_spanned_prefix, InputField, InputItem, InputVariant};
use itertools::Itertools;
use proc_macro2::{Ident, Literal, TokenStream};
use quote::quote;

use crate::input_model::{strip_spanned_prefix, InputField, InputItem, InputVariant};

pub fn parse_input_tokens(item: InputItem) -> TokenStream {
match item {
InputItem::Struct { name, fields } => derive_struct(&name, &fields),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::input_model::{add_spanned_prefix, InputField, InputItem, InputVariant};
use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens};
use syn::{fold::Fold, parse_quote, Error, GenericArgument, Type};

use crate::input_model::{add_spanned_prefix, InputField, InputItem, InputVariant};

pub fn spanned(item: InputItem, spanned_derive_args: TokenStream) -> TokenStream {
let derive_attribute = if spanned_derive_args.is_empty() {
spanned_derive_args
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::input_model::{strip_spanned_prefix, InputField, InputItem, InputVariant};
use itertools::Itertools;
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};

use crate::input_model::{strip_spanned_prefix, InputField, InputItem, InputVariant};

pub fn write_output_tokens(item: InputItem) -> TokenStream {
match item {
InputItem::Struct { name, fields } => derive_struct(&name, &fields),
Expand Down
Loading

0 comments on commit 68b8a93

Please sign in to comment.