Repository: diesel-rs/diesel
File: diesel_cli/src/print_schema.rs (Rust)

Code before:
    let mut components = vec![];

    // Find connected components in table graph. For the intended purpose of this function, we treat
    // the foreign key relation as being symmetrical, i.e. we are operating on the undirected graph.
    //
    // The algorithm is not optimized and suffers from repeated lookups in the foreign key list, but
    // it should be sufficient for typical table counts from a few dozen up to a few hundred tables.
    for table in tables {
        let name = &table.name;
        if visited.contains(name) {
            // This table is already part of another connected component.
            continue;
        }

        visited.insert(name);
        let mut component = vec![];
        let mut pending = vec![name];

        // Start a depth-first search with the current table name, walking the foreign key relations
        // in both directions.
        while let Some(name) = pending.pop() {
            component.push(name);

            let mut visit = |related_name: &'a TableName| {
                if !visited.contains(related_name) {
                    visited.insert(related_name);
                    pending.push(related_name);
                }
            };

            // Visit all remaining child tables that have this table as parent.
            for foreign_key in fk_constraints.iter().filter(|fk| fk.parent_table == *name) {
                visit(&foreign_key.child_table);
            }

            // Visit all remaining parent tables that have this table as child.
            for foreign_key in fk_constraints.iter().filter(|fk| fk.child_table == *name) {
                visit(&foreign_key.parent_table);
            }
        }

        // The component contains all tables that are reachable in either direction from the current
        // table.
        components.push(component);
    }

    components
}

struct TableDefinition<'a> {
    table: &'a TableData,

Review comment (suggestion):
This can also be written as just:
```suggestion
                if visited.insert(related_name) {
```

Code after:
                if visited.insert(related_name) {
                    pending.push(related_name);
                }
            };

            // Visit all remaining child tables that have this table as parent.
            for foreign_key in fk_constraints.iter().filter(|fk| fk.parent_table == *name) {
                visit(&foreign_key.child_table);
            }

            // Visit all remaining parent tables that have this table as child.
            for foreign_key in fk_constraints.iter().filter(|fk| fk.child_table == *name) {
                visit(&foreign_key.parent_table);
            }
        }

        // The component contains all tables that are reachable in either direction from the current
        // table. Sort that list by table name to ensure a stable output that does not depend on the
        // algorithm's specific implementation.
        component.sort();

        components.push(component);
    }

    // Sort the list of components to ensure a stable output that does not depend on the algorithm's
    // specific implementation. This sorts the list of components by the name of the first tables in
    // each component.
    components.sort();

    components
}

struct TableDefinition<'a> {
    table: &'a TableData,
    with_docs: DocConfig,
    import_types: Option<&'a [String]>,
    custom_type_overrides: Option<&'a [Option<ColumnType>]>,
}

fn write_doc_comments(out: &mut impl fmt::Write, doc: &str) -> fmt::Result {
    for line in doc.lines() {
        let line = line.trim();
        writeln!(out, "///{}{}", if line.is_empty() { "" } else { " " }, line)?;
    }
    Ok(())
}

impl Display for TableDefinition<'_> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "diesel::table! {{")?;
        {