Skip to content

Commit

Permalink
Field: map more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault authored and lnicola committed Sep 28, 2024
1 parent 69f708e commit aeefb27
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Add a `bundled` feature for `gdal-sys` that allows to build and statically link a minimal bundled version of gdal during `cargo build`
- Add methods ``alternative_name``, ``is_nullable``, ``is_unique``, ``default_value`` to ``Field``.

## 0.17.1

Expand Down
38 changes: 38 additions & 0 deletions src/vector/defn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,55 @@ impl<'a> Field<'a> {
_string(rv)
}

/// Get the alternative name (alias) of this field.
///
/// This API is new as of GDAL 3.2.
#[cfg(any(major_ge_4, all(major_is_3, minor_ge_2)))]
pub fn alternative_name(&'a self) -> String {
let rv = unsafe { gdal_sys::OGR_Fld_GetAlternativeNameRef(self.c_field_defn) };
_string(rv)
}

/// Get the data type of this field.
pub fn field_type(&'a self) -> OGRFieldType::Type {
unsafe { gdal_sys::OGR_Fld_GetType(self.c_field_defn) }
}

/// Get the formatting width for this field.
///
/// Zero means no specified width.
pub fn width(&'a self) -> i32 {
unsafe { gdal_sys::OGR_Fld_GetWidth(self.c_field_defn) }
}

/// Get the formatting precision for this field.
///
/// This should normally be zero for fields of types other than Real.
pub fn precision(&'a self) -> i32 {
unsafe { gdal_sys::OGR_Fld_GetPrecision(self.c_field_defn) }
}

/// Return whether this field can receive null values.
pub fn is_nullable(&'a self) -> bool {
unsafe { gdal_sys::OGR_Fld_IsNullable(self.c_field_defn) != 0 }
}

/// Return whether this field has a unique constraint.
///
/// This API is new as of GDAL 3.2.
#[cfg(any(major_ge_4, all(major_is_3, minor_ge_2)))]
pub fn is_unique(&'a self) -> bool {
unsafe { gdal_sys::OGR_Fld_IsUnique(self.c_field_defn) != 0 }
}

/// Get default field value.
pub fn default_value(&'a self) -> Option<String> {
let rv = unsafe { gdal_sys::OGR_Fld_GetDefault(self.c_field_defn) };
if rv.is_null() {
return None;
}
Some(_string(rv))
}
}

pub struct GeomFieldIterator<'a> {
Expand Down
10 changes: 10 additions & 0 deletions src/vector/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,16 @@ mod tests {
.map(|s| (s.0.to_string(), s.1))
.collect::<Vec<_>>();
assert_eq!(name_list, ok_names_types);

let field = layer.defn().fields().next().unwrap();
#[cfg(any(major_ge_4, all(major_is_3, minor_ge_2)))]
assert_eq!(field.alternative_name(), "");
assert_eq!(field.width(), 0);
assert_eq!(field.precision(), 0);
assert!(field.is_nullable());
#[cfg(any(major_ge_4, all(major_is_3, minor_ge_2)))]
assert!(!field.is_unique());
assert_eq!(field.default_value(), None);
}

#[test]
Expand Down

0 comments on commit aeefb27

Please sign in to comment.