From 1f9c0f41f2a2222a6b34add086f372f51ff14740 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 8 Jan 2020 16:56:28 +0100 Subject: [PATCH 1/3] Improve code readability, documentation and fix some doc examples --- src/blobs.rs | 3 +++ src/error.rs | 4 ++++ src/groups.rs | 9 +++++++++ src/lib.rs | 8 ++++---- src/objects.rs | 26 ++++++++++++++++++++------ src/reader.rs | 4 ++-- 6 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/blobs.rs b/src/blobs.rs index 3a3cb677b..31444eb5a 100644 --- a/src/blobs.rs +++ b/src/blobs.rs @@ -28,6 +28,7 @@ rental! { impl<'a> Iterator for rent::OsmObjs { type Item = OsmObj; + fn next(&mut self) -> Option { self.rent_mut(|objs| objs.next()) } @@ -35,10 +36,12 @@ impl<'a> Iterator for rent::OsmObjs { /// An iterator on `Result`. pub struct OsmObjs(OsmObjsImpl); + enum OsmObjsImpl { OkIter(iter::Map Result>), ErrIter(iter::Once>), } + impl Iterator for OsmObjs { type Item = Result; fn next(&mut self) -> Option { diff --git a/src/error.rs b/src/error.rs index b8ddbe84e..654069b0d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,6 +18,7 @@ pub enum Error { UnsupportedData, InvalidData, } + impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { @@ -28,6 +29,7 @@ impl fmt::Display for Error { } } } + impl std::error::Error for Error { fn description(&self) -> &str { match *self { @@ -45,11 +47,13 @@ impl std::error::Error for Error { } } } + impl From for Error { fn from(err: io::Error) -> Error { Error::Io(err) } } + impl From for Error { fn from(err: protobuf::ProtobufError) -> Error { Error::Pbf(err) diff --git a/src/groups.rs b/src/groups.rs index 2f4f54d68..0c83da351 100644 --- a/src/groups.rs +++ b/src/groups.rs @@ -48,6 +48,7 @@ pub struct SimpleNodes<'a> { iter: slice::Iter<'a, osmformat::Node>, block: &'a PrimitiveBlock, } + impl<'a> Iterator for SimpleNodes<'a> { type Item = Node; fn next(&mut self) -> Option { @@ -76,6 +77,7 @@ pub fn dense_nodes<'a>(group: &'a PrimitiveGroup, block: &'a PrimitiveBlock) -> cur_lon: 0, } } + pub struct DenseNodes<'a> { block: &'a PrimitiveBlock, dids: slice::Iter<'a, i64>, @@ -86,6 +88,7 @@ pub struct DenseNodes<'a> { cur_lat: i64, cur_lon: i64, } + impl<'a> Iterator for DenseNodes<'a> { type Item = Node; fn next(&mut self) -> Option { @@ -125,10 +128,12 @@ pub fn ways<'a>(group: &'a PrimitiveGroup, block: &'a PrimitiveBlock) -> Ways<'a block: block, } } + pub struct Ways<'a> { iter: slice::Iter<'a, osmformat::Way>, block: &'a PrimitiveBlock, } + impl<'a> Iterator for Ways<'a> { type Item = Way; fn next(&mut self) -> Option { @@ -164,6 +169,7 @@ pub struct Relations<'a> { iter: slice::Iter<'a, osmformat::Relation>, block: &'a PrimitiveBlock, } + impl<'a> Iterator for Relations<'a> { type Item = Relation; fn next(&mut self) -> Option { @@ -202,14 +208,17 @@ impl<'a> Iterator for Relations<'a> { fn make_string(k: usize, block: &osmformat::PrimitiveBlock) -> String { String::from_utf8_lossy(&*block.get_stringtable().get_s()[k]).into_owned() } + fn make_lat(c: i64, b: &osmformat::PrimitiveBlock) -> i32 { let granularity = b.get_granularity() as i64; ((b.get_lat_offset() + granularity * c) / 100) as i32 } + fn make_lon(c: i64, b: &osmformat::PrimitiveBlock) -> i32 { let granularity = b.get_granularity() as i64; ((b.get_lon_offset() + granularity * c) / 100) as i32 } + fn make_tags(keys: &[u32], vals: &[u32], b: &PrimitiveBlock) -> Tags { let mut tags: Tags = keys .iter() diff --git a/src/lib.rs b/src/lib.rs index 0bf908ca6..a78cffd4c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,12 +17,12 @@ //! the nodes of this way). For that, an easy to use function is //! availlable. //! -//! ``` +//! ```rust //! let mut pbf = osmpbfreader::OsmPbfReader::new(std::io::Cursor::new([])); //! let objs = pbf.get_objs_and_deps(|obj| { -//! obj.is_way() && obj.tags().contains_key("highway") -//! }) -//! .unwrap(); +//! obj.is_way() && obj.tags().contains_key("highway") +//! }) +//! .unwrap(); //! for (id, obj) in &objs { //! println!("{:?}: {:?}", id, obj); //! } diff --git a/src/objects.rs b/src/objects.rs index 79ce65d90..794f62502 100644 --- a/src/objects.rs +++ b/src/objects.rs @@ -18,6 +18,7 @@ use std::ops::{Deref, DerefMut}; /// information. #[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] pub struct Tags(TagsImpl); + /// FlatMap representing the key-value pairs of the tags pub type TagsImpl = ::flat_map::FlatMap; impl Tags { @@ -30,17 +31,21 @@ impl Tags { self.0.get(key).map_or(false, |v| v.as_str() == value) } } + impl Deref for Tags { type Target = TagsImpl; + fn deref(&self) -> &Self::Target { &self.0 } } + impl DerefMut for Tags { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } + impl FromIterator<(String, String)> for Tags { fn from_iter>(iter: T) -> Self { Tags(iter.into_iter().collect()) @@ -69,6 +74,7 @@ pub enum OsmId { /// The identifier of a relation Relation(RelationId), } + impl OsmId { /// Returns `true` if the id is a node id. pub fn is_node(&self) -> bool { @@ -82,21 +88,21 @@ impl OsmId { pub fn is_relation(&self) -> bool { self.relation().is_some() } - /// Returns the `NodeId` wrapped in an `Option`. + /// Returns the `NodeId` if it is a node, otherwise returns `None`. pub fn node(&self) -> Option { match *self { OsmId::Node(id) => Some(id), _ => None, } } - /// Returns the `WayId` wrapped in an `Option`. + /// Returns the `WayId` if it is a way, otherwise returns `None`. pub fn way(&self) -> Option { match *self { OsmId::Way(id) => Some(id), _ => None, } } - /// Returns the `RelationId` wrapped in an `Option`. + /// Returns the `RelationId` if it is a relation, otherwise returns `None`. pub fn relation(&self) -> Option { match *self { OsmId::Relation(id) => Some(id), @@ -123,6 +129,7 @@ pub enum OsmObj { /// A relation Relation(Relation), } + impl OsmObj { /// Returns the tags of the object. pub fn tags(&self) -> &Tags { @@ -152,21 +159,21 @@ impl OsmObj { pub fn is_relation(&self) -> bool { self.relation().is_some() } - /// Gets a reference to the node in an `Option`. + /// Returns a reference to the `Node` if `self` is a node, otherwise returns `None`. pub fn node(&self) -> Option<&Node> { match *self { OsmObj::Node(ref n) => Some(n), _ => None, } } - /// Gets a reference to the way in an `Option`. + /// Returns a reference to the `Way` if `self` is a way, otherwise returns `None`. pub fn way(&self) -> Option<&Way> { match *self { OsmObj::Way(ref w) => Some(w), _ => None, } } - /// Gets a reference to the relation in an `Option`. + /// Returns a reference to the `Relation` if `self` is a relation, otherwise returns `None`. pub fn relation(&self) -> Option<&Relation> { match *self { OsmObj::Relation(ref r) => Some(r), @@ -189,6 +196,7 @@ pub struct Node { /// The longitude in decimicro degrees (10⁻⁷ degrees). pub decimicro_lon: i32, } + impl Node { /// Returns the latitude of the node in degrees. pub fn lat(&self) -> f64 { @@ -212,6 +220,7 @@ pub struct Way { /// The ordered list of nodes as id. pub nodes: Vec, } + impl Way { /// Returns true if the way is /// [open](http://wiki.openstreetmap.org/wiki/Way#Open_way). @@ -252,26 +261,31 @@ impl ::std::convert::From for OsmId { OsmId::Node(n) } } + impl ::std::convert::From for OsmId { fn from(w: WayId) -> Self { OsmId::Way(w) } } + impl ::std::convert::From for OsmId { fn from(r: RelationId) -> Self { OsmId::Relation(r) } } + impl ::std::convert::From for OsmObj { fn from(n: Node) -> Self { OsmObj::Node(n) } } + impl ::std::convert::From for OsmObj { fn from(w: Way) -> Self { OsmObj::Way(w) } } + impl ::std::convert::From for OsmObj { fn from(r: Relation) -> Self { OsmObj::Relation(r) diff --git a/src/reader.rs b/src/reader.rs index 72f150d01..3bbae9b1a 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -57,7 +57,7 @@ impl OsmPbfReader { /// Returns an iterator on the OsmObj of the pbf file. /// - /// #Example + /// # Example /// /// ``` /// let mut pbf = osmpbfreader::OsmPbfReader::new(std::io::empty()); @@ -75,7 +75,7 @@ impl OsmPbfReader { /// CPU usage are guaranteed to be bounded even if the caller stop /// consuming items. /// - /// #Example + /// # Example /// /// ``` /// let mut pbf = osmpbfreader::OsmPbfReader::new(std::io::empty()); From ebf09d47dd45e381d88ba277be04276d28bd7b74 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 8 Jan 2020 17:53:38 +0100 Subject: [PATCH 2/3] Remove TagsImpl to make Deref appears in documentation --- src/objects.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/objects.rs b/src/objects.rs index 794f62502..a3bbf1456 100644 --- a/src/objects.rs +++ b/src/objects.rs @@ -17,23 +17,21 @@ use std::ops::{Deref, DerefMut}; /// tags](http://wiki.openstreetmap.org/wiki/Tags) for more /// information. #[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] -pub struct Tags(TagsImpl); +pub struct Tags(::flat_map::FlatMap); -/// FlatMap representing the key-value pairs of the tags -pub type TagsImpl = ::flat_map::FlatMap; impl Tags { /// Creates a new, empty `Tags` object. pub fn new() -> Tags { - Tags(TagsImpl::new()) + Tags(::flat_map::FlatMap::new()) } - /// Returns if contains the tag `(key, value)`. + /// Returns if it contains the a tag with the given `key` and `value`. pub fn contains(&self, key: &str, value: &str) -> bool { self.0.get(key).map_or(false, |v| v.as_str() == value) } } impl Deref for Tags { - type Target = TagsImpl; + type Target = ::flat_map::FlatMap; fn deref(&self) -> &Self::Target { &self.0 From adf0af44bb4a7804304bc791965cfae5576d06c5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 8 Jan 2020 17:57:11 +0100 Subject: [PATCH 3/3] Update crate version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5eca2a184..d6bf0cd99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "osmpbfreader" -version = "0.13.2" +version = "0.13.3" authors = ["Guillaume Pinot "] description = "Read OpenStreetMap PBF files in rust." documentation = "https://docs.rs/osmpbfreader"