Skip to content

Commit

Permalink
Add replace or push functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
da-kami committed Nov 21, 2024
1 parent 7f3368d commit 949736f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rcgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,30 @@ impl DistinguishedName {
self.entries.push_front((ty, s.into()));
}

/// Replaces the *fist occurrence* of a type with a new value.
/// This is a convenience function to avoid duplicating values.
///
/// If there are multiple occurrences of a type there is currently no way of changing the besides iterating over the types and values of an existing instance and creating a new instance.
///
/// ```
/// # use rcgen::{DistinguishedName, DnType, DnValue};
/// let mut dn = DistinguishedName::new();
/// dn.push(DnType::CommonName, DnValue::PrintableString("Master Cert".try_into().unwrap()));
/// assert_eq!(dn.get(&DnType::CommonName).get(0), Some(&DnValue::PrintableString("Master Cert".try_into().unwrap())).as_ref());
/// dn.push(DnType::CommonName, DnValue::PrintableString("Other Master Cert".try_into().unwrap()));
/// assert_eq!(dn.get(&DnType::CommonName).get(0), Some(&DnValue::PrintableString("Other Master Cert".try_into().unwrap())).as_ref());
/// ```
pub fn replace_or_push(&mut self, ty: DnType, s: impl Into<DnValue>) {
for (dn_type, dn_value) in self.entries.iter_mut() {
if *dn_type == ty {
*dn_value = s.into();
return;
}
}

self.push(ty, s)
}

#[cfg(feature = "x509-parser")]
fn from_name(name: &x509_parser::x509::X509Name) -> Result<Self, Error> {
use x509_parser::der_parser::asn1_rs::Tag;
Expand Down

0 comments on commit 949736f

Please sign in to comment.