georust / gdal

Rust bindings for GDAL
https://crates.io/crates/gdal
MIT License
359 stars 94 forks source link

Add `DriverIterator` to iterate though the registered drivers #513

Closed Atreyagaurav closed 9 months ago

Atreyagaurav commented 9 months ago

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?

lnicola commented 9 months ago

I'm personally happy enough with the current version.

Atreyagaurav commented 9 months ago

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 commented 9 months ago

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

lnicola commented 9 months ago

r? @metasim