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

Field: map more methods #561

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading