Skip to content

Commit

Permalink
rustfmt groups imports (#660)
Browse files Browse the repository at this point in the history
This enables the `group_imports` option. With this rustfmt automatically groups the imports in `std`, `external`, and `crate.
This is almost what we already wanted but we lose control of some things:
* it is not able to differentiate between workspace and external so all our crates will go in that section,
* it does not understand local import: 
```rust
mod foo {
    struct Foo;
}

use foo::Foo;
use rand::random;
```
instead of having a separate group for `foo`. 
* It does not respect groups that we create manually
```rust
use tokio;

use itertools;
```
the imports will be merged.

Looking at the changeset we are not too bad at maintaining this style. Most of the changes are the position of `crate` wr.t. to `self` or `super`, fusing groups, and moving our workspace crates to the external one.
But this will be our only option to enforce this style with a tool.

https://rust-lang.github.io/rustfmt/?version=v1.5.1&search=#group_imports
rust-lang/rustfmt#4709
rust-lang/rustfmt#4693
  • Loading branch information
acrrd committed Nov 15, 2022
1 parent 16b0ccc commit 3733c61
Show file tree
Hide file tree
Showing 45 changed files with 69 additions and 101 deletions.
3 changes: 1 addition & 2 deletions discovery_engine_core/ai/ai/src/coi/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,12 @@ mod tests {
use ndarray::arr1;
use xayn_discovery_engine_test_utils::assert_approx_eq;

use super::*;
use crate::{
coi::point::tests::{create_neg_cois, create_pos_cois},
utils::SECONDS_PER_DAY_F32,
};

use super::*;

#[test]
fn test_has_enough_cois() {
let user_interests = UserInterests::default();
Expand Down
1 change: 0 additions & 1 deletion discovery_engine_core/ai/ai/src/coi/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ where
#[cfg(test)]
pub(crate) mod tests {
use ndarray::{arr1, FixedInitializer};

use xayn_discovery_engine_test_utils::assert_approx_eq;

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion discovery_engine_core/ai/ai/src/coi/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ pub(super) fn compute_coi_decay_factor(

#[cfg(test)]
mod tests {
use crate::coi::{config::Config, point::tests::create_pos_cois};
use xayn_discovery_engine_test_utils::assert_approx_eq;

use super::*;
use crate::coi::{config::Config, point::tests::create_pos_cois};

#[test]
fn test_compute_relevances_empty_cois() {
Expand Down
3 changes: 1 addition & 2 deletions discovery_engine_core/ai/ai/src/embedding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::ops::RangeInclusive;
use itertools::Itertools;
use ndarray::{Array2, ArrayBase, ArrayView1, Data, Ix1};
use xayn_discovery_engine_bert::Embedding1;

// Re-export `Embedding` specific errors.
pub use xayn_discovery_engine_bert::MalformedBytesEmbedding;

Expand Down Expand Up @@ -109,9 +108,9 @@ pub fn cosine_similarity(a: ArrayView1<'_, f32>, b: ArrayView1<'_, f32>) -> f32
#[cfg(test)]
mod tests {
use ndarray::{arr1, arr2};
use xayn_discovery_engine_test_utils::assert_approx_eq;

use super::*;
use xayn_discovery_engine_test_utils::assert_approx_eq;

#[test]
fn test_l2_norm() {
Expand Down
3 changes: 1 addition & 2 deletions discovery_engine_core/ai/ai/src/kps/key_phrase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,12 @@ mod tests {
use ndarray::arr2;
use xayn_discovery_engine_test_utils::assert_approx_eq;

use super::*;
use crate::{
coi::{config::Config as CoiConfig, point::tests::create_pos_cois},
kps::config::Config as KpsConfig,
};

use super::*;

impl KeyPhrases {
pub(crate) fn new<'a, const N: usize>(
iter: impl IntoIterator<Item = (CoiId, (&'a str, &'a str), &'a str, [f32; N])>,
Expand Down
5 changes: 2 additions & 3 deletions discovery_engine_core/ai/ai/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ mod error;
mod kps;
pub mod utils;

#[cfg(doc)]
pub use crate::embedding::COSINE_SIMILARITY_RANGE;
pub use crate::{
coi::{
config::{Config as CoiConfig, Error as CoiConfigError},
Expand All @@ -62,6 +64,3 @@ pub use crate::{
},
utils::{nan_safe_f32_cmp, nan_safe_f32_cmp_desc, system_time_now},
};

#[cfg(doc)]
pub use crate::embedding::COSINE_SIMILARITY_RANGE;
2 changes: 1 addition & 1 deletion discovery_engine_core/ai/ai/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ mod tests {

use serde::{Deserialize, Serialize};
use serde_json::{from_str, to_string};
use xayn_discovery_engine_test_utils::assert_approx_eq;

use super::*;
use xayn_discovery_engine_test_utils::assert_approx_eq;

#[test]
fn test_nan_safe_f32_cmp_sorts_in_the_right_order() {
Expand Down
1 change: 0 additions & 1 deletion discovery_engine_core/ai/bert/benches/bert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use std::path::Path;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use ndarray::s;
use onnxruntime::{environment::Environment, GraphOptimizationLevel};

use xayn_discovery_engine_bert::{tokenizer::Tokenizer, Config, Embedding2, NonePooler};
use xayn_discovery_engine_test_utils::asset::smbert;

Expand Down
1 change: 0 additions & 1 deletion discovery_engine_core/ai/bert/examples/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use csv::Reader;
use indicatif::ProgressBar;
use ndarray::{s, Array1, Array2, ArrayView1, Axis};
use onnxruntime::{environment::Environment, session::Session, GraphOptimizationLevel};

use xayn_discovery_engine_bert::{
tokenizer::Tokenizer,
Config,
Expand Down
3 changes: 2 additions & 1 deletion discovery_engine_core/ai/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod approx_eq;
pub mod asset;
pub mod uuid;

pub use crate::approx_eq::ApproxEqIter;
#[doc(hidden)] // required for standalone export of assert_approx_eq!
pub use float_cmp::approx_eq;

pub use crate::approx_eq::ApproxEqIter;
1 change: 0 additions & 1 deletion discovery_engine_core/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use figment::{
};
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;

use xayn_discovery_engine_ai::{CoiConfig, KpsConfig};
use xayn_discovery_engine_providers::Market;

Expand Down
5 changes: 1 addition & 4 deletions discovery_engine_core/core/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ use serde_repr::{Deserialize_repr, Serialize_repr};
use thiserror::Error;
use url::Url;
use uuid::Uuid;

pub use xayn_discovery_engine_ai::Embedding;
use xayn_discovery_engine_ai::{Document as AiDocument, DocumentId};
use xayn_discovery_engine_providers::GenericArticle;

use crate::stack::Id as StackId;

pub use xayn_discovery_engine_ai::Embedding;

/// Errors that could happen when constructing a [`Document`].
#[derive(Error, Debug, DisplayDoc)]
pub enum Error {
Expand Down Expand Up @@ -307,7 +305,6 @@ pub struct WeightedSource {
#[cfg(test)]
pub(crate) mod tests {
use chrono::TimeZone;

use xayn_discovery_engine_providers::{Rank, UrlWithDomain};

use super::*;
Expand Down
4 changes: 1 addition & 3 deletions discovery_engine_core/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use rayon::iter::{Either, IntoParallelIterator, ParallelIterator};
use thiserror::Error;
use tokio::sync::RwLock;
use tracing::{debug, error, info, instrument, Level};

use xayn_discovery_engine_ai::{
self,
cosine_similarity,
Expand Down Expand Up @@ -1408,12 +1407,11 @@ pub(crate) mod tests {
MockServer,
ResponseTemplate,
};

use crate::{document::tests::mock_generic_article, stack::ops::MockOps};
use xayn_discovery_engine_providers::{Rank, UrlWithDomain};
use xayn_discovery_engine_test_utils::asset::{smbert_mocked, smbert_quantized};

use super::*;
use crate::{document::tests::mock_generic_article, stack::ops::MockOps};

#[test]
fn test_usize_not_to_small() {
Expand Down
3 changes: 1 addition & 2 deletions discovery_engine_core/core/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,8 @@ mod tests {
use uuid::Uuid;
use xayn_discovery_engine_test_utils::assert_approx_eq;

use crate::stack::ops::MockOps;

use super::*;
use crate::stack::ops::MockOps;

// assert that `f` returns ok if the argument contains only documents valid `stack_id`
fn assert_valid_document<T, F>(f: F, stack_id: Id)
Expand Down
1 change: 1 addition & 0 deletions discovery_engine_core/core/src/stack/exploration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! Exploration stack.
use std::collections::HashSet;

use uuid::uuid;
use xayn_discovery_engine_ai::UserInterests;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,8 @@ mod tests {
use xayn_discovery_engine_ai::CoiId;
use xayn_discovery_engine_test_utils::{assert_approx_eq, uuid::mock_uuid};

use crate::document::Id;

use super::*;
use crate::document::Id;

fn new_doc() -> Document {
Document {
Expand Down
8 changes: 4 additions & 4 deletions discovery_engine_core/core/src/stack/filters/article.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use xayn_discovery_engine_ai::GenericError;
use xayn_discovery_engine_providers::GenericArticle;

use crate::{
document::{Document, HistoricDocument},
stack::filters::DuplicateFilter,
};
use xayn_discovery_engine_ai::GenericError;
use xayn_discovery_engine_providers::GenericArticle;

pub(crate) trait ArticleFilter {
fn apply(
Expand Down Expand Up @@ -76,12 +77,11 @@ impl SourcesFilter {
mod tests {
use std::{collections::HashMap, iter::FromIterator};

use crate::document::Document;
use itertools::Itertools;

use xayn_discovery_engine_providers::{NewscatcherArticle, Rank};

use super::*;
use crate::document::Document;

pub(crate) fn to_generic_article(articles: Vec<NewscatcherArticle>) -> Vec<GenericArticle> {
articles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::collections::{HashMap, HashSet};

use xayn_discovery_engine_providers::GenericArticle;

use crate::document::{Document, HistoricDocument, NewsResource};
Expand Down
6 changes: 2 additions & 4 deletions discovery_engine_core/core/src/stack/filters/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ use kodama::{linkage, Dendrogram, Method};
use ndarray::ArrayView1;
use xayn_discovery_engine_ai::{cosine_similarity, nan_safe_f32_cmp, pairwise_cosine_similarity};

use crate::document::{Document, WeightedSource};

use super::source_weight;
use crate::document::{Document, WeightedSource};

/// Computes the condensed cosine similarity matrix of the documents' embeddings.
fn condensed_cosine_similarity<'a, I>(embeddings: I) -> Vec<f32>
Expand Down Expand Up @@ -304,9 +303,8 @@ mod tests {
use xayn_discovery_engine_bert::{AveragePooler, AvgBert, Config as BertConfig};
use xayn_discovery_engine_test_utils::{assert_approx_eq, asset::smbert_quantized};

use crate::document::NewsResource;

use super::*;
use crate::document::NewsResource;

#[test]
fn test_condensed_cosine_similarity() {
Expand Down
3 changes: 2 additions & 1 deletion discovery_engine_core/core/src/stack/filters/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ pub(crate) fn source_weight(document: &Document, sources: &[WeightedSource]) ->

#[cfg(test)]
mod tests {
use xayn_discovery_engine_providers::{GenericArticle, NewscatcherArticle};

use super::*;
use crate::document::{Document, WeightedSource};
use xayn_discovery_engine_providers::{GenericArticle, NewscatcherArticle};

impl WeightedSource {
fn new(source_domain: &str, weight: i32) -> Self {
Expand Down
1 change: 0 additions & 1 deletion discovery_engine_core/core/src/stack/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use displaydoc::Display;
#[cfg(test)]
use mockall::automock;
use thiserror::Error;

use xayn_discovery_engine_ai::{GenericError, KeyPhrase};
use xayn_discovery_engine_providers::{GenericArticle, Market};

Expand Down
3 changes: 1 addition & 2 deletions discovery_engine_core/core/src/stack/ops/breaking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use xayn_discovery_engine_providers::{
RankLimit,
};

use super::{common::request_min_new_items, NewItemsError, Ops};
use crate::{
config::EndpointConfig,
document::{Document, HistoricDocument},
Expand All @@ -36,8 +37,6 @@ use crate::{
},
};

use super::{common::request_min_new_items, NewItemsError, Ops};

/// Stack operations customized for breaking news items.
pub struct BreakingNews {
client: Arc<dyn HeadlinesProvider>,
Expand Down
3 changes: 1 addition & 2 deletions discovery_engine_core/core/src/stack/ops/personalized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use xayn_discovery_engine_providers::{
SimilarSearchQuery,
};

use super::{common::request_min_new_items, NewItemsError, Ops};
use crate::{
config::EndpointConfig,
document::{Document, HistoricDocument},
Expand All @@ -36,8 +37,6 @@ use crate::{
},
};

use super::{common::request_min_new_items, NewItemsError, Ops};

/// Stack operations customized for personalized news items.
pub struct PersonalizedNews {
client: Arc<dyn SimilarSearchProvider>,
Expand Down
3 changes: 1 addition & 2 deletions discovery_engine_core/core/src/stack/ops/trusted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use xayn_discovery_engine_providers::{
TrustedHeadlinesQuery,
};

use super::{common::request_min_new_items, NewItemsError, Ops};
use crate::{
config::EndpointConfig,
document::{Document, HistoricDocument},
Expand All @@ -36,8 +37,6 @@ use crate::{
},
};

use super::{common::request_min_new_items, NewItemsError, Ops};

/// Stack operations customized for trusted news.
pub struct TrustedNews {
client: Arc<dyn TrustedHeadlinesProvider>,
Expand Down
12 changes: 7 additions & 5 deletions discovery_engine_core/core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,18 @@ impl Engine {
}

mod naive_date_time_migration {
use crate::{
document::{Document, Id, NewsResource, UserReaction},
stack::{Data, Id as StackId},
};
use std::collections::HashMap;

use chrono::{DateTime, NaiveDateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use url::Url;
use xayn_discovery_engine_ai::Embedding;

use crate::{
document::{Document, Id, NewsResource, UserReaction},
stack::{Data, Id as StackId},
};

#[derive(Serialize, Deserialize)]
pub(super) struct DataWithNaiveDateTime {
pub(super) alpha: f32,
Expand Down
3 changes: 1 addition & 2 deletions discovery_engine_core/core/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ use async_trait::async_trait;
use chrono::{DateTime, Utc};
use displaydoc::Display;
use thiserror::Error;
use xayn_discovery_engine_ai::{GenericError, MalformedBytesEmbedding};
use xayn_discovery_engine_ai::{Embedding, GenericError, MalformedBytesEmbedding};

use crate::{
document::{self, HistoricDocument, NewsResource, UserReaction, ViewMode, WeightedSource},
stack,
storage::models::{ApiDocumentView, NewDocument, Search, TimeSpentDocumentView},
};
use xayn_discovery_engine_ai::Embedding;

pub(crate) type BoxedStorage = Box<dyn Storage + Send + Sync>;

Expand Down
Loading

0 comments on commit 3733c61

Please sign in to comment.