Add StringFactory trait (#1537)

This commit is contained in:
Chris Stewart 2020-06-09 15:03:56 -05:00 committed by GitHub
parent f9f1e89a07
commit 5c2fb8f580

View file

@ -0,0 +1,20 @@
package org.bitcoins.crypto
import scala.util.Try
/** A common factory trait that can be re-used to deserialize a string to a type t */
trait StringFactory[T] {
/** Tries to parse a string to type t, throws an exception if fails */
def fromString(string: String): T
/** Treis to parse a string to type t, returns None if failure */
def fromStringOpt(string: String): Option[T] = {
fromStringT(string).toOption
}
/** Tries to parsea string to type t, returns [[scala.util.Failure]] if the fails */
def fromStringT(string: String): Try[T] = {
Try(fromString(string))
}
}