Skip to content

Add ToSql / FromSql for IpInet and IpCidr from cidr crate #877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 17, 2022

Conversation

timando
Copy link
Contributor

@timando timando commented Mar 16, 2022

No description provided.

fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
let inet = types::inet_from_sql(raw)?;
Ok(IpCidr::new(inet.addr(), inet.netmask())
.expect("postgres cidr type has zeroed host portion"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why panic here instead of returning an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the type only accepts postgres cidr type which postgres guarantees to be a valid cidr (i.e. mask length within the limits for the address type and zeroed host portion) which is the same as the requirements to create an IpCidr.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you assume the value passed to from_sql is always correct, then there's no need for any of these implementations to return an error. The library should not assume that the input is valid.

fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
let inet = types::inet_from_sql(raw)?;
Ok(IpInet::new(inet.addr(), inet.netmask())
.expect("postgres enforces maximum length of netmask"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar, postgres guarantees that the inet type has a mask length corresponding to the address type (<= 32 for IPv4, <=128 for IPv6)

@sfackler sfackler merged commit a638ada into sfackler:master Mar 17, 2022
@sfackler
Copy link
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants