Skip to content

Commit

Permalink
feat: use "serde_json" for "BlobType" string conversion (#2364)
Browse files Browse the repository at this point in the history
* feat: use "serde_json" for "BlobType" string conversion

* chore: rebuild CLI.md

* fix: use "serde_json" inside original string conversion code

* fix: improve FromStr impl

* fix: roll back "clap-markdown" to "0.1.3"

* fix: roll back "clap" to "4.4.11"

* fix: roll back "Cargo.lock"

* fix: use "match" to avoid "unwrap"

* chore: clippy
  • Loading branch information
duguorong009 authored Aug 9, 2024
1 parent fb59fd4 commit 4189b55
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions linera-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ rand.workspace = true
serde.workspace = true
serde-name.workspace = true
serde_bytes.workspace = true
serde_json.workspace = true
sha3.workspace = true
test-strategy = { workspace = true, optional = true }
thiserror.workspace = true
Expand Down
14 changes: 5 additions & 9 deletions linera-base/src/identifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,18 @@ pub enum BlobType {

impl Display for BlobType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BlobType::Data => write!(f, "Data")?,
};

Ok(())
match serde_json::to_string(self) {
Ok(s) => write!(f, "{}", s),
Err(_) => Err(fmt::Error),
}
}
}

impl FromStr for BlobType {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Data" => Ok(BlobType::Data),
_ => Err(anyhow!("Invalid blob type: {}", s)),
}
serde_json::from_str(s).with_context(|| format!("Invalid BlobType: {}", s))
}
}

Expand Down

0 comments on commit 4189b55

Please sign in to comment.