1 // Take a look at the license at the top of the repository in the LICENSE file. 2 3 use crate::translate::*; 4 use crate::value::Value; 5 use crate::CStr; 6 use crate::Type; 7 use std::cmp; 8 9 #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] 10 pub enum UserDirectory { 11 #[doc(alias = "G_USER_DIRECTORY_DESKTOP")] 12 Desktop, 13 #[doc(alias = "G_USER_DIRECTORY_DOCUMENTS")] 14 Documents, 15 #[doc(alias = "G_USER_DIRECTORY_DOWNLOAD")] 16 Downloads, 17 #[doc(alias = "G_USER_DIRECTORY_MUSIC")] 18 Music, 19 #[doc(alias = "G_USER_DIRECTORY_PICTURES")] 20 Pictures, 21 #[doc(alias = "G_USER_DIRECTORY_PUBLIC_SHARE")] 22 PublicShare, 23 #[doc(alias = "G_USER_DIRECTORY_TEMPLATES")] 24 Templates, 25 #[doc(alias = "G_USER_DIRECTORY_VIDEOS")] 26 Videos, 27 #[doc(hidden)] 28 #[doc(alias = "G_USER_N_DIRECTORIES")] 29 NDirectories, 30 } 31 32 #[doc(hidden)] 33 impl IntoGlib for UserDirectory { 34 type GlibType = ffi::GUserDirectory; 35 into_glib(self) -> ffi::GUserDirectory36 fn into_glib(self) -> ffi::GUserDirectory { 37 match self { 38 Self::Desktop => ffi::G_USER_DIRECTORY_DESKTOP, 39 Self::Documents => ffi::G_USER_DIRECTORY_DOCUMENTS, 40 Self::Downloads => ffi::G_USER_DIRECTORY_DOWNLOAD, 41 Self::Music => ffi::G_USER_DIRECTORY_MUSIC, 42 Self::Pictures => ffi::G_USER_DIRECTORY_PICTURES, 43 Self::PublicShare => ffi::G_USER_DIRECTORY_PUBLIC_SHARE, 44 Self::Templates => ffi::G_USER_DIRECTORY_TEMPLATES, 45 Self::Videos => ffi::G_USER_DIRECTORY_VIDEOS, 46 Self::NDirectories => ffi::G_USER_N_DIRECTORIES, 47 } 48 } 49 } 50 51 /// Representation of an `enum` for dynamically, at runtime, querying the values of the enum and 52 /// using them. 53 #[derive(Debug)] 54 #[doc(alias = "GEnumClass")] 55 pub struct EnumClass(*mut gobject_ffi::GEnumClass); 56 57 unsafe impl Send for EnumClass {} 58 unsafe impl Sync for EnumClass {} 59 60 impl EnumClass { 61 /// Create a new `EnumClass` from a `Type`. 62 /// 63 /// Returns `None` if `type_` is not representing an enum. new(type_: Type) -> Option<Self>64 pub fn new(type_: Type) -> Option<Self> { 65 unsafe { 66 let is_enum: bool = from_glib(gobject_ffi::g_type_is_a( 67 type_.into_glib(), 68 gobject_ffi::G_TYPE_ENUM, 69 )); 70 if !is_enum { 71 return None; 72 } 73 74 Some(EnumClass( 75 gobject_ffi::g_type_class_ref(type_.into_glib()) as *mut _ 76 )) 77 } 78 } 79 80 /// `Type` of the enum. type_(&self) -> Type81 pub fn type_(&self) -> Type { 82 unsafe { from_glib((*self.0).g_type_class.g_type) } 83 } 84 85 /// Gets `EnumValue` by integer `value`, if existing. 86 /// 87 /// Returns `None` if the enum does not contain any value 88 /// with `value`. 89 #[doc(alias = "g_enum_get_value")] 90 #[doc(alias = "get_value")] value(&self, value: i32) -> Option<EnumValue>91 pub fn value(&self, value: i32) -> Option<EnumValue> { 92 unsafe { 93 let v = gobject_ffi::g_enum_get_value(self.0, value); 94 if v.is_null() { 95 None 96 } else { 97 Some(EnumValue(v, self.clone())) 98 } 99 } 100 } 101 102 /// Gets `EnumValue` by string name `name`, if existing. 103 /// 104 /// Returns `None` if the enum does not contain any value 105 /// with name `name`. 106 #[doc(alias = "g_enum_get_value_by_name")] 107 #[doc(alias = "get_value_by_name")] value_by_name(&self, name: &str) -> Option<EnumValue>108 pub fn value_by_name(&self, name: &str) -> Option<EnumValue> { 109 unsafe { 110 let v = gobject_ffi::g_enum_get_value_by_name(self.0, name.to_glib_none().0); 111 if v.is_null() { 112 None 113 } else { 114 Some(EnumValue(v, self.clone())) 115 } 116 } 117 } 118 119 /// Gets `EnumValue` by string nick `nick`, if existing. 120 /// 121 /// Returns `None` if the enum does not contain any value 122 /// with nick `nick`. 123 #[doc(alias = "g_enum_get_value_by_nick")] 124 #[doc(alias = "get_value_by_nick")] value_by_nick(&self, nick: &str) -> Option<EnumValue>125 pub fn value_by_nick(&self, nick: &str) -> Option<EnumValue> { 126 unsafe { 127 let v = gobject_ffi::g_enum_get_value_by_nick(self.0, nick.to_glib_none().0); 128 if v.is_null() { 129 None 130 } else { 131 Some(EnumValue(v, self.clone())) 132 } 133 } 134 } 135 136 /// Gets all `EnumValue` of this `EnumClass`. 137 #[doc(alias = "get_values")] values(&self) -> Vec<EnumValue>138 pub fn values(&self) -> Vec<EnumValue> { 139 unsafe { 140 let n = (*self.0).n_values; 141 let mut res = Vec::with_capacity(n as usize); 142 for i in 0..(n as usize) { 143 res.push(EnumValue((*self.0).values.add(i), self.clone())) 144 } 145 res 146 } 147 } 148 149 /// Converts integer `value` to a `Value`, if part of the enum. to_value(&self, value: i32) -> Option<Value>150 pub fn to_value(&self, value: i32) -> Option<Value> { 151 self.value(value).map(|v| v.to_value()) 152 } 153 154 /// Converts string name `name` to a `Value`, if part of the enum. to_value_by_name(&self, name: &str) -> Option<Value>155 pub fn to_value_by_name(&self, name: &str) -> Option<Value> { 156 self.value_by_name(name).map(|v| v.to_value()) 157 } 158 159 /// Converts string nick `nick` to a `Value`, if part of the enum. to_value_by_nick(&self, nick: &str) -> Option<Value>160 pub fn to_value_by_nick(&self, nick: &str) -> Option<Value> { 161 self.value_by_nick(nick).map(|v| v.to_value()) 162 } 163 } 164 165 impl Drop for EnumClass { drop(&mut self)166 fn drop(&mut self) { 167 unsafe { 168 gobject_ffi::g_type_class_unref(self.0 as *mut _); 169 } 170 } 171 } 172 173 impl Clone for EnumClass { clone(&self) -> Self174 fn clone(&self) -> Self { 175 unsafe { Self(gobject_ffi::g_type_class_ref(self.type_().into_glib()) as *mut _) } 176 } 177 } 178 179 /// Representation of a single enum value of an `EnumClass`. 180 #[derive(Debug, Clone)] 181 #[doc(alias = "GEnumValue")] 182 pub struct EnumValue(*const gobject_ffi::GEnumValue, EnumClass); 183 184 unsafe impl Send for EnumValue {} 185 unsafe impl Sync for EnumValue {} 186 187 impl EnumValue { 188 /// Get integer value corresponding to the value. 189 #[doc(alias = "get_value")] value(&self) -> i32190 pub fn value(&self) -> i32 { 191 unsafe { (*self.0).value } 192 } 193 194 /// Get name corresponding to the value. 195 #[doc(alias = "get_name")] name(&self) -> &str196 pub fn name(&self) -> &str { 197 unsafe { CStr::from_ptr((*self.0).value_name).to_str().unwrap() } 198 } 199 200 /// Get nick corresponding to the value. 201 #[doc(alias = "get_nick")] nick(&self) -> &str202 pub fn nick(&self) -> &str { 203 unsafe { CStr::from_ptr((*self.0).value_nick).to_str().unwrap() } 204 } 205 206 /// Convert enum value to a `Value`. to_value(&self) -> Value207 pub fn to_value(&self) -> Value { 208 unsafe { 209 let mut v = Value::from_type(self.1.type_()); 210 gobject_ffi::g_value_set_enum(v.to_glib_none_mut().0, (*self.0).value); 211 v 212 } 213 } 214 215 /// Convert enum value from a `Value`. from_value(value: &Value) -> Option<EnumValue>216 pub fn from_value(value: &Value) -> Option<EnumValue> { 217 unsafe { 218 let enum_class = EnumClass::new(value.type_()); 219 enum_class.and_then(|e| e.value(gobject_ffi::g_value_get_enum(value.to_glib_none().0))) 220 } 221 } 222 223 /// Get `EnumClass` to which the enum value belongs. 224 #[doc(alias = "get_class")] class(&self) -> &EnumClass225 pub fn class(&self) -> &EnumClass { 226 &self.1 227 } 228 } 229 230 impl PartialEq for EnumValue { eq(&self, other: &Self) -> bool231 fn eq(&self, other: &Self) -> bool { 232 self.value().eq(&other.value()) 233 } 234 } 235 236 impl Eq for EnumValue {} 237 238 impl PartialOrd for EnumValue { partial_cmp(&self, other: &Self) -> Option<cmp::Ordering>239 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { 240 self.value().partial_cmp(&other.value()) 241 } 242 } 243 244 impl Ord for EnumValue { cmp(&self, other: &Self) -> cmp::Ordering245 fn cmp(&self, other: &Self) -> cmp::Ordering { 246 self.value().cmp(&other.value()) 247 } 248 } 249 250 /// Representation of a `flags` for dynamically, at runtime, querying the values of the enum and 251 /// using them 252 #[derive(Debug)] 253 #[doc(alias = "GFlagsClass")] 254 pub struct FlagsClass(*mut gobject_ffi::GFlagsClass); 255 256 unsafe impl Send for FlagsClass {} 257 unsafe impl Sync for FlagsClass {} 258 259 impl FlagsClass { 260 /// Create a new `FlagsClass` from a `Type` 261 /// 262 /// Returns `None` if `type_` is not representing a flags type. new(type_: Type) -> Option<Self>263 pub fn new(type_: Type) -> Option<Self> { 264 unsafe { 265 let is_flags: bool = from_glib(gobject_ffi::g_type_is_a( 266 type_.into_glib(), 267 gobject_ffi::G_TYPE_FLAGS, 268 )); 269 if !is_flags { 270 return None; 271 } 272 273 Some(FlagsClass( 274 gobject_ffi::g_type_class_ref(type_.into_glib()) as *mut _ 275 )) 276 } 277 } 278 279 /// `Type` of the flags. type_(&self) -> Type280 pub fn type_(&self) -> Type { 281 unsafe { from_glib((*self.0).g_type_class.g_type) } 282 } 283 284 /// Gets `FlagsValue` by integer `value`, if existing. 285 /// 286 /// Returns `None` if the flags do not contain any value 287 /// with `value`. 288 #[doc(alias = "g_flags_get_first_value")] 289 #[doc(alias = "get_value")] value(&self, value: u32) -> Option<FlagsValue>290 pub fn value(&self, value: u32) -> Option<FlagsValue> { 291 unsafe { 292 let v = gobject_ffi::g_flags_get_first_value(self.0, value); 293 if v.is_null() { 294 None 295 } else { 296 Some(FlagsValue(v, self.clone())) 297 } 298 } 299 } 300 301 /// Gets `FlagsValue` by string name `name`, if existing. 302 /// 303 /// Returns `None` if the flags do not contain any value 304 /// with name `name`. 305 #[doc(alias = "g_flags_get_value_by_name")] 306 #[doc(alias = "get_value_by_name")] value_by_name(&self, name: &str) -> Option<FlagsValue>307 pub fn value_by_name(&self, name: &str) -> Option<FlagsValue> { 308 unsafe { 309 let v = gobject_ffi::g_flags_get_value_by_name(self.0, name.to_glib_none().0); 310 if v.is_null() { 311 None 312 } else { 313 Some(FlagsValue(v, self.clone())) 314 } 315 } 316 } 317 318 /// Gets `FlagsValue` by string nick `nick`, if existing. 319 /// 320 /// Returns `None` if the flags do not contain any value 321 /// with nick `nick`. 322 #[doc(alias = "g_flags_get_value_by_nick")] 323 #[doc(alias = "get_value_by_nick")] value_by_nick(&self, nick: &str) -> Option<FlagsValue>324 pub fn value_by_nick(&self, nick: &str) -> Option<FlagsValue> { 325 unsafe { 326 let v = gobject_ffi::g_flags_get_value_by_nick(self.0, nick.to_glib_none().0); 327 if v.is_null() { 328 None 329 } else { 330 Some(FlagsValue(v, self.clone())) 331 } 332 } 333 } 334 335 /// Gets all `FlagsValue` of this `FlagsClass`. 336 #[doc(alias = "get_values")] values(&self) -> Vec<FlagsValue>337 pub fn values(&self) -> Vec<FlagsValue> { 338 unsafe { 339 let n = (*self.0).n_values; 340 let mut res = Vec::with_capacity(n as usize); 341 for i in 0..(n as usize) { 342 res.push(FlagsValue((*self.0).values.add(i), self.clone())) 343 } 344 res 345 } 346 } 347 348 /// Converts integer `value` to a `Value`, if part of the flags. to_value(&self, value: u32) -> Option<Value>349 pub fn to_value(&self, value: u32) -> Option<Value> { 350 self.value(value).map(|v| v.to_value()) 351 } 352 353 /// Converts string name `name` to a `Value`, if part of the flags. to_value_by_name(&self, name: &str) -> Option<Value>354 pub fn to_value_by_name(&self, name: &str) -> Option<Value> { 355 self.value_by_name(name).map(|v| v.to_value()) 356 } 357 358 /// Converts string nick `nick` to a `Value`, if part of the flags. to_value_by_nick(&self, nick: &str) -> Option<Value>359 pub fn to_value_by_nick(&self, nick: &str) -> Option<Value> { 360 self.value_by_nick(nick).map(|v| v.to_value()) 361 } 362 363 /// Checks if the flags corresponding to integer `f` is set in `value`. is_set(&self, value: &Value, f: u32) -> bool364 pub fn is_set(&self, value: &Value, f: u32) -> bool { 365 unsafe { 366 if self.type_() != value.type_() { 367 return false; 368 } 369 370 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0); 371 flags & f != 0 372 } 373 } 374 375 /// Checks if the flags corresponding to string name `name` is set in `value`. is_set_by_name(&self, value: &Value, name: &str) -> bool376 pub fn is_set_by_name(&self, value: &Value, name: &str) -> bool { 377 unsafe { 378 if self.type_() != value.type_() { 379 return false; 380 } 381 382 if let Some(f) = self.value_by_name(name) { 383 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0); 384 flags & f.value() != 0 385 } else { 386 false 387 } 388 } 389 } 390 391 /// Checks if the flags corresponding to string nick `nick` is set in `value`. is_set_by_nick(&self, value: &Value, nick: &str) -> bool392 pub fn is_set_by_nick(&self, value: &Value, nick: &str) -> bool { 393 unsafe { 394 if self.type_() != value.type_() { 395 return false; 396 } 397 398 if let Some(f) = self.value_by_nick(nick) { 399 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0); 400 flags & f.value() != 0 401 } else { 402 false 403 } 404 } 405 } 406 407 /// Set flags value corresponding to integer `f` in `value`, if part of that flags. If the 408 /// flag is already set, it will succeed without doing any changes. 409 /// 410 /// Returns `Ok(value)` with the flag set if successful, or `Err(value)` with the original 411 /// value otherwise. 412 #[doc(alias = "g_value_set_flags")] set(&self, mut value: Value, f: u32) -> Result<Value, Value>413 pub fn set(&self, mut value: Value, f: u32) -> Result<Value, Value> { 414 unsafe { 415 if self.type_() != value.type_() { 416 return Err(value); 417 } 418 419 if let Some(f) = self.value(f) { 420 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0); 421 gobject_ffi::g_value_set_flags(value.to_glib_none_mut().0, flags | f.value()); 422 Ok(value) 423 } else { 424 Err(value) 425 } 426 } 427 } 428 429 /// Set flags value corresponding to string name `name` in `value`, if part of that flags. 430 /// If the flag is already set, it will succeed without doing any changes. 431 /// 432 /// Returns `Ok(value)` with the flag set if successful, or `Err(value)` with the original 433 /// value otherwise. set_by_name(&self, mut value: Value, name: &str) -> Result<Value, Value>434 pub fn set_by_name(&self, mut value: Value, name: &str) -> Result<Value, Value> { 435 unsafe { 436 if self.type_() != value.type_() { 437 return Err(value); 438 } 439 440 if let Some(f) = self.value_by_name(name) { 441 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0); 442 gobject_ffi::g_value_set_flags(value.to_glib_none_mut().0, flags | f.value()); 443 Ok(value) 444 } else { 445 Err(value) 446 } 447 } 448 } 449 450 /// Set flags value corresponding to string nick `nick` in `value`, if part of that flags. 451 /// If the flag is already set, it will succeed without doing any changes. 452 /// 453 /// Returns `Ok(value)` with the flag set if successful, or `Err(value)` with the original 454 /// value otherwise. set_by_nick(&self, mut value: Value, nick: &str) -> Result<Value, Value>455 pub fn set_by_nick(&self, mut value: Value, nick: &str) -> Result<Value, Value> { 456 unsafe { 457 if self.type_() != value.type_() { 458 return Err(value); 459 } 460 461 if let Some(f) = self.value_by_nick(nick) { 462 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0); 463 gobject_ffi::g_value_set_flags(value.to_glib_none_mut().0, flags | f.value()); 464 Ok(value) 465 } else { 466 Err(value) 467 } 468 } 469 } 470 471 /// Unset flags value corresponding to integer `f` in `value`, if part of that flags. 472 /// If the flag is already unset, it will succeed without doing any changes. 473 /// 474 /// Returns `Ok(value)` with the flag unset if successful, or `Err(value)` with the original 475 /// value otherwise. unset(&self, mut value: Value, f: u32) -> Result<Value, Value>476 pub fn unset(&self, mut value: Value, f: u32) -> Result<Value, Value> { 477 unsafe { 478 if self.type_() != value.type_() { 479 return Err(value); 480 } 481 482 if let Some(f) = self.value(f) { 483 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0); 484 gobject_ffi::g_value_set_flags(value.to_glib_none_mut().0, flags & !f.value()); 485 Ok(value) 486 } else { 487 Err(value) 488 } 489 } 490 } 491 492 /// Unset flags value corresponding to string name `name` in `value`, if part of that flags. 493 /// If the flag is already unset, it will succeed without doing any changes. 494 /// 495 /// Returns `Ok(value)` with the flag unset if successful, or `Err(value)` with the original 496 /// value otherwise. unset_by_name(&self, mut value: Value, name: &str) -> Result<Value, Value>497 pub fn unset_by_name(&self, mut value: Value, name: &str) -> Result<Value, Value> { 498 unsafe { 499 if self.type_() != value.type_() { 500 return Err(value); 501 } 502 503 if let Some(f) = self.value_by_name(name) { 504 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0); 505 gobject_ffi::g_value_set_flags(value.to_glib_none_mut().0, flags & !f.value()); 506 Ok(value) 507 } else { 508 Err(value) 509 } 510 } 511 } 512 513 /// Unset flags value corresponding to string nick `nick` in `value`, if part of that flags. 514 /// If the flag is already unset, it will succeed without doing any changes. 515 /// 516 /// Returns `Ok(value)` with the flag unset if successful, or `Err(value)` with the original 517 /// value otherwise. unset_by_nick(&self, mut value: Value, nick: &str) -> Result<Value, Value>518 pub fn unset_by_nick(&self, mut value: Value, nick: &str) -> Result<Value, Value> { 519 unsafe { 520 if self.type_() != value.type_() { 521 return Err(value); 522 } 523 524 if let Some(f) = self.value_by_nick(nick) { 525 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0); 526 gobject_ffi::g_value_set_flags(value.to_glib_none_mut().0, flags & !f.value()); 527 Ok(value) 528 } else { 529 Err(value) 530 } 531 } 532 } 533 534 /// Returns a new `FlagsBuilder` for conveniently setting/unsetting flags 535 /// and building a `Value`. builder(&self) -> FlagsBuilder536 pub fn builder(&self) -> FlagsBuilder { 537 FlagsBuilder::new(self) 538 } 539 540 /// Returns a new `FlagsBuilder` for conveniently setting/unsetting flags 541 /// and building a `Value`. The `Value` is initialized with `value`. builder_with_value(&self, value: Value) -> Option<FlagsBuilder>542 pub fn builder_with_value(&self, value: Value) -> Option<FlagsBuilder> { 543 if self.type_() != value.type_() { 544 return None; 545 } 546 547 Some(FlagsBuilder::with_value(self, value)) 548 } 549 } 550 551 impl Drop for FlagsClass { drop(&mut self)552 fn drop(&mut self) { 553 unsafe { 554 gobject_ffi::g_type_class_unref(self.0 as *mut _); 555 } 556 } 557 } 558 559 impl Clone for FlagsClass { clone(&self) -> Self560 fn clone(&self) -> Self { 561 unsafe { Self(gobject_ffi::g_type_class_ref(self.type_().into_glib()) as *mut _) } 562 } 563 } 564 565 /// Representation of a single flags value of a `FlagsClass`. 566 #[derive(Debug, Clone)] 567 #[doc(alias = "GFlagsValue")] 568 pub struct FlagsValue(*const gobject_ffi::GFlagsValue, FlagsClass); 569 570 unsafe impl Send for FlagsValue {} 571 unsafe impl Sync for FlagsValue {} 572 573 impl FlagsValue { 574 /// Get integer value corresponding to the value. 575 #[doc(alias = "get_value")] value(&self) -> u32576 pub fn value(&self) -> u32 { 577 unsafe { (*self.0).value } 578 } 579 580 /// Get name corresponding to the value. 581 #[doc(alias = "get_name")] name(&self) -> &str582 pub fn name(&self) -> &str { 583 unsafe { CStr::from_ptr((*self.0).value_name).to_str().unwrap() } 584 } 585 586 /// Get nick corresponding to the value. 587 #[doc(alias = "get_nick")] nick(&self) -> &str588 pub fn nick(&self) -> &str { 589 unsafe { CStr::from_ptr((*self.0).value_nick).to_str().unwrap() } 590 } 591 592 /// Convert flags value to a `Value`. to_value(&self) -> Value593 pub fn to_value(&self) -> Value { 594 unsafe { 595 let mut v = Value::from_type(self.1.type_()); 596 gobject_ffi::g_value_set_flags(v.to_glib_none_mut().0, (*self.0).value); 597 v 598 } 599 } 600 601 /// Convert flags values from a `Value`. This returns all flags that are set. from_value(value: &Value) -> Vec<FlagsValue>602 pub fn from_value(value: &Value) -> Vec<FlagsValue> { 603 unsafe { 604 let flags_class = FlagsClass::new(value.type_()); 605 let mut res = Vec::new(); 606 if let Some(flags_class) = flags_class { 607 let f = gobject_ffi::g_value_get_flags(value.to_glib_none().0); 608 for v in flags_class.values() { 609 if v.value() & f != 0 { 610 res.push(v); 611 } 612 } 613 } 614 res 615 } 616 } 617 618 /// Get `FlagsClass` to which the flags value belongs. 619 #[doc(alias = "get_class")] class(&self) -> &FlagsClass620 pub fn class(&self) -> &FlagsClass { 621 &self.1 622 } 623 } 624 625 impl PartialEq for FlagsValue { eq(&self, other: &Self) -> bool626 fn eq(&self, other: &Self) -> bool { 627 self.value().eq(&other.value()) 628 } 629 } 630 631 impl Eq for FlagsValue {} 632 633 /// Builder for conveniently setting/unsetting flags and returning a `Value`. 634 /// 635 /// Example for getting a flags property, unsetting some flags and setting the updated flags on the 636 /// object again: 637 /// 638 /// ```ignore 639 /// let flags = obj.property("flags").unwrap(); 640 /// let flags_class = FlagsClass::new(flags.type_()).unwrap(); 641 /// let flags = flags_class.builder_with_value(flags).unwrap() 642 /// .unset_by_nick("some-flag") 643 /// .unset_by_nick("some-other-flag") 644 /// .build() 645 /// .unwrap(); 646 /// obj.set_property("flags", &flags).unwrap(); 647 /// ``` 648 /// 649 /// If setting/unsetting any value fails, `build()` returns `None`. 650 pub struct FlagsBuilder<'a>(&'a FlagsClass, Option<Value>); 651 impl<'a> FlagsBuilder<'a> { new(flags_class: &FlagsClass) -> FlagsBuilder652 fn new(flags_class: &FlagsClass) -> FlagsBuilder { 653 let value = Value::from_type(flags_class.type_()); 654 FlagsBuilder(flags_class, Some(value)) 655 } 656 with_value(flags_class: &FlagsClass, value: Value) -> FlagsBuilder657 fn with_value(flags_class: &FlagsClass, value: Value) -> FlagsBuilder { 658 FlagsBuilder(flags_class, Some(value)) 659 } 660 661 /// Set flags corresponding to integer value `f`. set(mut self, f: u32) -> Self662 pub fn set(mut self, f: u32) -> Self { 663 if let Some(value) = self.1.take() { 664 self.1 = self.0.set(value, f).ok(); 665 } 666 667 self 668 } 669 670 /// Set flags corresponding to string name `name`. set_by_name(mut self, name: &str) -> Self671 pub fn set_by_name(mut self, name: &str) -> Self { 672 if let Some(value) = self.1.take() { 673 self.1 = self.0.set_by_name(value, name).ok(); 674 } 675 676 self 677 } 678 679 /// Set flags corresponding to string nick `nick`. set_by_nick(mut self, nick: &str) -> Self680 pub fn set_by_nick(mut self, nick: &str) -> Self { 681 if let Some(value) = self.1.take() { 682 self.1 = self.0.set_by_nick(value, nick).ok(); 683 } 684 685 self 686 } 687 688 /// Unsets flags corresponding to integer value `f`. unset(mut self, f: u32) -> Self689 pub fn unset(mut self, f: u32) -> Self { 690 if let Some(value) = self.1.take() { 691 self.1 = self.0.unset(value, f).ok(); 692 } 693 694 self 695 } 696 697 /// Unset flags corresponding to string name `name`. unset_by_name(mut self, name: &str) -> Self698 pub fn unset_by_name(mut self, name: &str) -> Self { 699 if let Some(value) = self.1.take() { 700 self.1 = self.0.unset_by_name(value, name).ok(); 701 } 702 703 self 704 } 705 706 /// Unset flags corresponding to string nick `nick`. unset_by_nick(mut self, nick: &str) -> Self707 pub fn unset_by_nick(mut self, nick: &str) -> Self { 708 if let Some(value) = self.1.take() { 709 self.1 = self.0.unset_by_nick(value, nick).ok(); 710 } 711 712 self 713 } 714 715 /// Converts to the final `Value`, unless any previous setting/unsetting of flags failed. build(self) -> Option<Value>716 pub fn build(self) -> Option<Value> { 717 self.1 718 } 719 } 720