-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement to/from sql for semver version
- Loading branch information
1 parent
186994e
commit 8b03b9a
Showing
15 changed files
with
124 additions
and
83 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
use diesel::backend::{Backend, RawValue}; | ||
use diesel::deserialize::{self, FromSql}; | ||
use diesel::query_builder::bind_collector::RawBytesBindCollector; | ||
use diesel::serialize::{self, ToSql}; | ||
use diesel::sql_types; | ||
use omicron_common::api::external; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
// We wrap semver::Version in external to impl JsonSchema, and we wrap it again | ||
// here to impl ToSql/FromSql | ||
|
||
#[derive( | ||
Clone, Debug, AsExpression, FromSqlRow, Serialize, Deserialize, PartialEq, | ||
)] | ||
#[diesel(sql_type = sql_types::Text)] | ||
pub struct SemverVersion(external::SemverVersion); | ||
|
||
NewtypeFrom! { () pub struct SemverVersion(external::SemverVersion); } | ||
NewtypeDeref! { () pub struct SemverVersion(external::SemverVersion); } | ||
|
||
impl SemverVersion { | ||
pub fn new(major: u64, minor: u64, patch: u64) -> Self { | ||
Self(external::SemverVersion(semver::Version::new(major, minor, patch))) | ||
} | ||
} | ||
|
||
impl<DB> ToSql<sql_types::Text, DB> for SemverVersion | ||
where | ||
DB: Backend<BindCollector = RawBytesBindCollector<DB>>, | ||
String: ToSql<sql_types::Text, DB>, | ||
{ | ||
fn to_sql<'a>( | ||
&'a self, | ||
out: &mut serialize::Output<'a, '_, DB>, | ||
) -> serialize::Result { | ||
(self.0).0.to_string().to_sql(&mut out.reborrow()) | ||
} | ||
} | ||
|
||
impl<DB> FromSql<sql_types::Text, DB> for SemverVersion | ||
where | ||
DB: Backend, | ||
String: FromSql<sql_types::Text, DB>, | ||
{ | ||
fn from_sql(raw: RawValue<DB>) -> deserialize::Result<Self> { | ||
String::from_sql(raw)? | ||
.parse() | ||
.map(|s| SemverVersion(external::SemverVersion(s))) | ||
.map_err(|e| e.into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.