Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DriverIterator to iterate though the registered drivers #513

Merged
merged 2 commits into from
Jan 5, 2024

Conversation

Atreyagaurav
Copy link
Contributor

  • I agree to follow the project's code of conduct.
  • I added an entry to CHANGES.md if knowledge of this change could be valuable to users.

Fixes #512

I've done the simplest implementation for the first commit:

pub struct DriverIterator {
    current: usize,
}

impl DriverIterator {
    pub fn new() -> Self {
        DriverIterator { current: 0 }
    }
}

impl Iterator for DriverIterator {
    type Item = Driver;

    fn next(&mut self) -> Option<Self::Item> {
        match DriverManager::get_driver(self.current) {
            Ok(d) => {
                self.current += 1;
                Some(d)
            }
            Err(_) => None,
        }
    }
}

Which seems too simple, so I'm thinking of adding the ability to filter by metadata.

Which would be something like this:

pub struct DriverIterator {
    current: usize,
    filters: Vec<String>,
}

impl DriverIterator {
    pub fn all() -> Self {
        DriverIterator {
            current: 0,
            filters: vec![],
        }
    }

    pub fn vectors() -> Self {
        DriverIterator {
            current: 0,
            filters: vec!["DCAP_VECTOR".into()],
        }
    }

    pub fn rasters() -> Self {
        DriverIterator {
            current: 0,
            filters: vec!["DCAP_RASTER".into()],
        }
    }

    pub fn with_metadata(metadata: Vec<String>) -> Self {
        DriverIterator {
            current: 0,
            filters: metadata,
        }
    }
}

impl Iterator for DriverIterator {
    type Item = Driver;

    fn next(&mut self) -> Option<Self::Item> {
        while let Ok(d) = DriverManager::get_driver(self.current) {
            self.current += 1;
            if self
                .filters
                .iter()
                .all(|f| d.metadata_item(f, "").is_some())
            {
                return Some(d);
            }
        }
        None
    }
}

If someone needs to filter by more complex conditions, they can use the filter directly in the iterator, but providing some options for vectors and rasters seems like a reasonable thing to do.

What do you think?

src/driver.rs Outdated Show resolved Hide resolved
src/driver.rs Outdated Show resolved Hide resolved
src/driver.rs Outdated Show resolved Hide resolved
src/driver.rs Outdated Show resolved Hide resolved
src/driver.rs Outdated Show resolved Hide resolved
src/driver.rs Outdated Show resolved Hide resolved
assert_eq!(DriverManager::count(), DriverIterator::new().count());
assert_eq!(DriverManager::count(), DriverManager::all().count());

let drivers: HashSet<String> = DriverManager::all().map(|d| d.short_name()).collect();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a little overkill, I'd just keep the count test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the same in the beginning, but repeating the same driver DriverManager::count() times would pass the test. Although that's a very unlikely mistake, this feels like a complete test.

@lnicola
Copy link
Member

lnicola commented Jan 5, 2024

I'm personally happy enough with the current version.

Co-authored-by: Laurențiu Nicola <[email protected]>
@Atreyagaurav
Copy link
Contributor Author

In that case, it's fine. Someone can always use a filter themselves.

I just thought having named methods might make it easier for the library users instead of having to navigate the metadata for specific driver capabilities.

@lnicola
Copy link
Member

lnicola commented Jan 5, 2024

If we add those, they can use filter and return impl Iterator.

@lnicola
Copy link
Member

lnicola commented Jan 5, 2024

r? @metasim

@metasim metasim merged commit 82db2fe into georust:master Jan 5, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Add DriverIterator to iterate through the available drivers
3 participants