-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
106: fix: harden login routine even more r=crepererum a=crepererum Co-authored-by: Marco Neumann <[email protected]>
- Loading branch information
Showing
3 changed files
with
63 additions
and
35 deletions.
There are no files selected for viewing
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,43 @@ | ||
use std::{ops::Deref, str::FromStr}; | ||
|
||
/// Non-empty [`String`]. | ||
#[derive(Clone)] | ||
pub struct NonEmptyString(String); | ||
|
||
impl std::fmt::Debug for NonEmptyString { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
impl std::fmt::Display for NonEmptyString { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
impl Deref for NonEmptyString { | ||
type Target = str; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.0.deref() | ||
} | ||
} | ||
|
||
impl AsRef<str> for NonEmptyString { | ||
fn as_ref(&self) -> &str { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl FromStr for NonEmptyString { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { | ||
if s.is_empty() { | ||
Err("cannot be empty".to_owned()) | ||
} else { | ||
Ok(Self(s.to_owned())) | ||
} | ||
} | ||
} |