1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-22 19:08:00 +01:00

Add syntactic sugar for SignatureSubpacketCallback factory methods

This commit is contained in:
Paul Schaub 2024-01-24 18:59:35 +01:00
parent b103f3ecc2
commit bd26268533
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
5 changed files with 257 additions and 1 deletions

View file

@ -156,4 +156,52 @@ interface BaseSignatureSubpackets {
fun addEmbeddedSignature(embeddedSignature: EmbeddedSignature): BaseSignatureSubpackets
fun clearEmbeddedSignatures(): BaseSignatureSubpackets
companion object {
/** Factory method for a [Callback] that does nothing. */
@JvmStatic fun nop() = object : Callback {}
/**
* Factory function with receiver, which returns a [Callback] that modifies the hashed
* subpacket area of a [BaseSignatureSubpackets] object.
*
* Can be called like this:
* ```
* val callback = BaseSignatureSubpackets.applyHashed {
* setCreationTime(date)
* ...
* }
* ```
*/
@JvmStatic
fun applyHashed(function: BaseSignatureSubpackets.() -> Unit): Callback {
return object : Callback {
override fun modifyHashedSubpackets(hashedSubpackets: BaseSignatureSubpackets) {
function(hashedSubpackets)
}
}
}
/**
* Factory function with receiver, which returns a [Callback] that modifies the unhashed
* subpacket area of a [BaseSignatureSubpackets] object.
*
* Can be called like this:
* ```
* val callback = BaseSignatureSubpackets.applyUnhashed {
* setCreationTime(date)
* ...
* }
* ```
*/
@JvmStatic
fun applyUnhashed(function: BaseSignatureSubpackets.() -> Unit): Callback {
return object : Callback {
override fun modifyUnhashedSubpackets(unhashedSubpackets: BaseSignatureSubpackets) {
function(unhashedSubpackets)
}
}
}
}
}

View file

@ -7,4 +7,52 @@ package org.pgpainless.signature.subpackets
interface CertificationSubpackets : BaseSignatureSubpackets {
interface Callback : SignatureSubpacketCallback<CertificationSubpackets>
companion object {
/** Factory method for a [Callback] that does nothing. */
@JvmStatic fun nop() = object : Callback {}
/**
* Factory function with receiver, which returns a [Callback] that modifies the hashed
* subpacket area of a [CertificationSubpackets] object.
*
* Can be called like this:
* ```
* val callback = CertificationSubpackets.applyHashed {
* setCreationTime(date)
* ...
* }
* ```
*/
@JvmStatic
fun applyHashed(function: CertificationSubpackets.() -> Unit): Callback {
return object : Callback {
override fun modifyHashedSubpackets(hashedSubpackets: CertificationSubpackets) {
function(hashedSubpackets)
}
}
}
/**
* Factory function with receiver, which returns a [Callback] that modifies the unhashed
* subpacket area of a [CertificationSubpackets] object.
*
* Can be called like this:
* ```
* val callback = CertificationSubpackets.applyUnhashed {
* setCreationTime(date)
* ...
* }
* ```
*/
@JvmStatic
fun applyUnhashed(function: CertificationSubpackets.() -> Unit): Callback {
return object : Callback {
override fun modifyUnhashedSubpackets(unhashedSubpackets: CertificationSubpackets) {
function(unhashedSubpackets)
}
}
}
}
}

View file

@ -27,4 +27,56 @@ interface RevocationSignatureSubpackets : BaseSignatureSubpackets {
): RevocationSignatureSubpackets
fun setRevocationReason(reason: RevocationReason?): RevocationSignatureSubpackets
companion object {
/** Factory method for a [Callback] that does nothing. */
@JvmStatic fun nop() = object : Callback {}
/**
* Factory function with receiver, which returns a [Callback] that modifies the hashed
* subpacket area of a [RevocationSignatureSubpackets] object.
*
* Can be called like this:
* ```
* val callback = RevocationSignatureSubpackets.applyHashed {
* setCreationTime(date)
* ...
* }
* ```
*/
@JvmStatic
fun applyHashed(function: RevocationSignatureSubpackets.() -> Unit): Callback {
return object : Callback {
override fun modifyHashedSubpackets(
hashedSubpackets: RevocationSignatureSubpackets
) {
function(hashedSubpackets)
}
}
}
/**
* Factory function with receiver, which returns a [Callback] that modifies the unhashed
* subpacket area of a [RevocationSignatureSubpackets] object.
*
* Can be called like this:
* ```
* val callback = RevocationSignatureSubpackets.applyUnhashed {
* setCreationTime(date)
* ...
* }
* ```
*/
@JvmStatic
fun applyUnhashed(function: RevocationSignatureSubpackets.() -> Unit): Callback {
return object : Callback {
override fun modifyUnhashedSubpackets(
unhashedSubpackets: RevocationSignatureSubpackets
) {
function(unhashedSubpackets)
}
}
}
}
}

View file

@ -16,7 +16,22 @@ import org.pgpainless.algorithm.*
interface SelfSignatureSubpackets : BaseSignatureSubpackets {
interface Callback : SignatureSubpacketCallback<SelfSignatureSubpackets>
interface Callback : SignatureSubpacketCallback<SelfSignatureSubpackets> {
fun then(nextCallback: SignatureSubpacketCallback<SelfSignatureSubpackets>): Callback {
val currCallback = this
return object : Callback {
override fun modifyHashedSubpackets(hashedSubpackets: SelfSignatureSubpackets) {
currCallback.modifyHashedSubpackets(hashedSubpackets)
nextCallback.modifyHashedSubpackets(hashedSubpackets)
}
override fun modifyUnhashedSubpackets(unhashedSubpackets: SelfSignatureSubpackets) {
currCallback.modifyUnhashedSubpackets(unhashedSubpackets)
nextCallback.modifyUnhashedSubpackets(unhashedSubpackets)
}
}
}
}
fun setKeyFlags(vararg keyflags: KeyFlag): SelfSignatureSubpackets
@ -116,4 +131,52 @@ interface SelfSignatureSubpackets : BaseSignatureSubpackets {
fun setFeatures(isCritical: Boolean, vararg features: Feature): SelfSignatureSubpackets
fun setFeatures(features: Features?): SelfSignatureSubpackets
companion object {
/** Factory method for a [Callback] that does nothing. */
@JvmStatic fun nop() = object : Callback {}
/**
* Factory function with receiver, which returns a [Callback] that modifies the hashed
* subpacket area of a [SelfSignatureSubpackets] object.
*
* Can be called like this:
* ```
* val callback = SelfSignatureSubpackets.applyHashed {
* setCreationTime(date)
* ...
* }
* ```
*/
@JvmStatic
fun applyHashed(function: SelfSignatureSubpackets.() -> Unit): Callback {
return object : Callback {
override fun modifyHashedSubpackets(hashedSubpackets: SelfSignatureSubpackets) {
function(hashedSubpackets)
}
}
}
/**
* Factory function with receiver, which returns a [Callback] that modifies the unhashed
* subpacket area of a [SelfSignatureSubpackets] object.
*
* Can be called like this:
* ```
* val callback = SelfSignatureSubpackets.applyUnhashed {
* setCreationTime(date)
* ...
* }
* ```
*/
@JvmStatic
fun applyUnhashed(function: SelfSignatureSubpackets.() -> Unit): Callback {
return object : Callback {
override fun modifyUnhashedSubpackets(unhashedSubpackets: SelfSignatureSubpackets) {
function(unhashedSubpackets)
}
}
}
}
}

View file

@ -89,6 +89,51 @@ class SignatureSubpackets :
fun createEmptySubpackets(): SignatureSubpackets {
return SignatureSubpackets()
}
/** Factory method for a [Callback] that does nothing. */
@JvmStatic fun nop() = object : Callback {}
/**
* Factory function with receiver, which returns a [Callback] that modifies the hashed
* subpacket area of a [SignatureSubpackets] object.
*
* Can be called like this:
* ```
* val callback = SignatureSubpackets.applyHashed {
* setCreationTime(date)
* ...
* }
* ```
*/
@JvmStatic
fun applyHashed(function: SignatureSubpackets.() -> Unit): Callback {
return object : Callback {
override fun modifyHashedSubpackets(hashedSubpackets: SignatureSubpackets) {
function(hashedSubpackets)
}
}
}
/**
* Factory function with receiver, which returns a [Callback] that modifies the unhashed
* subpacket area of a [SignatureSubpackets] object.
*
* Can be called like this:
* ```
* val callback = SignatureSubpackets.applyUnhashed {
* setCreationTime(date)
* ...
* }
* ```
*/
@JvmStatic
fun applyUnhashed(function: SignatureSubpackets.() -> Unit): Callback {
return object : Callback {
override fun modifyUnhashedSubpackets(unhashedSubpackets: SignatureSubpackets) {
function(unhashedSubpackets)
}
}
}
}
override fun setRevocationReason(