Skip to content

Commit 03d85b1

Browse files
authored
Return Option<Self> in fallible constructors (#141)
1 parent ba52826 commit 03d85b1

File tree

6 files changed

+23
-39
lines changed

6 files changed

+23
-39
lines changed

src/types/geometrycollection.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use geo_traits::GeometryCollectionTrait;
1616

17-
use crate::error::Error;
1817
use crate::to_wkt::write_geometry_collection;
1918
use crate::tokenizer::{PeekableTokens, Token};
2019
use crate::types::Dimension;
@@ -51,13 +50,13 @@ impl<T: WktNum> GeometryCollection<T> {
5150
///
5251
/// To handle empty input iterators, consider calling `unwrap_or` on the result and defaulting
5352
/// to an [empty][Self::empty] geometry with specified dimension.
54-
pub fn from_geometries(geoms: impl IntoIterator<Item = Wkt<T>>) -> Result<Self, Error> {
53+
pub fn from_geometries(geoms: impl IntoIterator<Item = Wkt<T>>) -> Option<Self> {
5554
let geoms = geoms.into_iter().collect::<Vec<_>>();
5655
if geoms.is_empty() {
57-
Err(Error::UnknownDimension)
56+
None
5857
} else {
5958
let dim = geoms[0].dimension();
60-
Ok(Self::new(geoms, dim))
59+
Some(Self::new(geoms, dim))
6160
}
6261
}
6362

src/types/linestring.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use geo_traits::LineStringTrait;
1616

17-
use crate::error::Error;
1817
use crate::to_wkt::write_linestring;
1918
use crate::tokenizer::PeekableTokens;
2019
use crate::types::coord::Coord;
@@ -46,19 +45,17 @@ impl<T: WktNum> LineString<T> {
4645
/// This will infer the dimension from the first coordinate, and will not validate that all
4746
/// coordinates have the same dimension.
4847
///
49-
/// ## Errors
50-
///
51-
/// If the input iterator is empty.
48+
/// Returns `None` if the input iterator is empty.
5249
///
5350
/// To handle empty input iterators, consider calling `unwrap_or` on the result and defaulting
5451
/// to an [empty][Self::empty] geometry with specified dimension.
55-
pub fn from_coords(coords: impl IntoIterator<Item = Coord<T>>) -> Result<Self, Error> {
52+
pub fn from_coords(coords: impl IntoIterator<Item = Coord<T>>) -> Option<Self> {
5653
let coords = coords.into_iter().collect::<Vec<_>>();
5754
if coords.is_empty() {
58-
Err(Error::UnknownDimension)
55+
None
5956
} else {
6057
let dim = coords[0].dimension();
61-
Ok(Self::new(coords, dim))
58+
Some(Self::new(coords, dim))
6259
}
6360
}
6461

src/types/multilinestring.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use geo_traits::MultiLineStringTrait;
1616

17-
use crate::error::Error;
1817
use crate::to_wkt::write_multi_linestring;
1918
use crate::tokenizer::PeekableTokens;
2019
use crate::types::linestring::LineString;
@@ -46,21 +45,19 @@ impl<T: WktNum> MultiLineString<T> {
4645
/// This will infer the dimension from the first line string, and will not validate that all
4746
/// line strings have the same dimension.
4847
///
49-
/// ## Errors
50-
///
51-
/// If the input iterator is empty.
48+
/// Returns `None` if the input iterator is empty.
5249
///
5350
/// To handle empty input iterators, consider calling `unwrap_or` on the result and defaulting
5451
/// to an [empty][Self::empty] geometry with specified dimension.
5552
pub fn from_line_strings(
5653
line_strings: impl IntoIterator<Item = LineString<T>>,
57-
) -> Result<Self, Error> {
54+
) -> Option<Self> {
5855
let line_strings = line_strings.into_iter().collect::<Vec<_>>();
5956
if line_strings.is_empty() {
60-
Err(Error::UnknownDimension)
57+
None
6158
} else {
6259
let dim = line_strings[0].dimension();
63-
Ok(Self::new(line_strings, dim))
60+
Some(Self::new(line_strings, dim))
6461
}
6562
}
6663

src/types/multipoint.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use geo_traits::MultiPointTrait;
1616

17-
use crate::error::Error;
1817
use crate::to_wkt::write_multi_point;
1918
use crate::tokenizer::PeekableTokens;
2019
use crate::types::point::Point;
@@ -46,19 +45,17 @@ impl<T: WktNum> MultiPoint<T> {
4645
/// This will infer the dimension from the first point, and will not validate that all
4746
/// points have the same dimension.
4847
///
49-
/// ## Errors
50-
///
51-
/// If the input iterator is empty.
48+
/// Returns `None` if the input iterator is empty.
5249
///
5350
/// To handle empty input iterators, consider calling `unwrap_or` on the result and defaulting
5451
/// to an [empty][Self::empty] geometry with specified dimension.
55-
pub fn from_points(points: impl IntoIterator<Item = Point<T>>) -> Result<Self, Error> {
52+
pub fn from_points(points: impl IntoIterator<Item = Point<T>>) -> Option<Self> {
5653
let points = points.into_iter().collect::<Vec<_>>();
5754
if points.is_empty() {
58-
Err(Error::UnknownDimension)
55+
None
5956
} else {
6057
let dim = points[0].dimension();
61-
Ok(Self::new(points, dim))
58+
Some(Self::new(points, dim))
6259
}
6360
}
6461

src/types/multipolygon.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use geo_traits::MultiPolygonTrait;
1616

17-
use crate::error::Error;
1817
use crate::to_wkt::write_multi_polygon;
1918
use crate::tokenizer::PeekableTokens;
2019
use crate::types::polygon::Polygon;
@@ -46,19 +45,17 @@ impl<T: WktNum> MultiPolygon<T> {
4645
/// This will infer the dimension from the first polygon, and will not validate that all
4746
/// polygons have the same dimension.
4847
///
49-
/// ## Errors
50-
///
51-
/// If the input iterator is empty.
48+
/// Returns `None` if the input iterator is empty.
5249
///
5350
/// To handle empty input iterators, consider calling `unwrap_or` on the result and defaulting
5451
/// to an [empty][Self::empty] geometry with specified dimension.
55-
pub fn from_polygons(polygons: impl IntoIterator<Item = Polygon<T>>) -> Result<Self, Error> {
52+
pub fn from_polygons(polygons: impl IntoIterator<Item = Polygon<T>>) -> Option<Self> {
5653
let polygons = polygons.into_iter().collect::<Vec<_>>();
5754
if polygons.is_empty() {
58-
Err(Error::UnknownDimension)
55+
None
5956
} else {
6057
let dim = polygons[0].dimension();
61-
Ok(Self::new(polygons, dim))
58+
Some(Self::new(polygons, dim))
6259
}
6360
}
6461

src/types/polygon.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use geo_traits::PolygonTrait;
1616

17-
use crate::error::Error;
1817
use crate::to_wkt::write_polygon;
1918
use crate::tokenizer::PeekableTokens;
2019
use crate::types::linestring::LineString;
@@ -46,19 +45,17 @@ impl<T: WktNum> Polygon<T> {
4645
/// This will infer the dimension from the first line string, and will not validate that all
4746
/// line strings have the same dimension.
4847
///
49-
/// ## Errors
50-
///
51-
/// If the input iterator is empty.
48+
/// Returns `None` if the input iterator is empty.
5249
///
5350
/// To handle empty input iterators, consider calling `unwrap_or` on the result and defaulting
5451
/// to an [empty][Self::empty] geometry with specified dimension.
55-
pub fn from_rings(rings: impl IntoIterator<Item = LineString<T>>) -> Result<Self, Error> {
52+
pub fn from_rings(rings: impl IntoIterator<Item = LineString<T>>) -> Option<Self> {
5653
let rings = rings.into_iter().collect::<Vec<_>>();
5754
if rings.is_empty() {
58-
Err(Error::UnknownDimension)
55+
None
5956
} else {
6057
let dim = rings[0].dimension();
61-
Ok(Self::new(rings, dim))
58+
Some(Self::new(rings, dim))
6259
}
6360
}
6461

0 commit comments

Comments
 (0)