Skip to content

Commit 93d486b

Browse files
committed
CrateScope: Implement To/FromSql traits
1 parent e11cb19 commit 93d486b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/models/token/scopes.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ impl TryFrom<&str> for CrateScope {
7070
}
7171
}
7272

73+
impl TryFrom<String> for CrateScope {
74+
type Error = String;
75+
76+
fn try_from(pattern: String) -> Result<Self, Self::Error> {
77+
match CrateScope::is_valid_pattern(&pattern) {
78+
true => Ok(CrateScope { pattern }),
79+
false => Err("Invalid crate scope pattern".to_string()),
80+
}
81+
}
82+
}
83+
84+
impl FromSql<Text, Pg> for CrateScope {
85+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
86+
let value = <String as FromSql<Text, Pg>>::from_sql(bytes)?;
87+
Ok(CrateScope::try_from(value)?)
88+
}
89+
}
90+
91+
impl ToSql<Text, Pg> for CrateScope {
92+
fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> serialize::Result {
93+
<String as ToSql<Text, Pg>>::to_sql(&self.pattern, out)
94+
}
95+
}
96+
7397
impl CrateScope {
7498
fn is_valid_pattern(pattern: &str) -> bool {
7599
if pattern.is_empty() {

0 commit comments

Comments
 (0)