Skip to content

Commit 8aa6ef1

Browse files
committed
implemented toolchain_channel
1 parent 94d9cd7 commit 8aa6ef1

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

dylint/src/package_options/revs.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::common::clippy_repository;
22
use anyhow::{Context, Result, anyhow};
3-
use dylint_internal::git2::{Commit, Oid, Repository, Sort};
3+
use dylint_internal::git2::{Commit, Oid, Repository, Sort, Tree};
44
use if_chain::if_chain;
55
use semver::Version;
66
use std::{path::Path, rc::Rc, time::Instant};
@@ -19,6 +19,39 @@ pub struct Revs {
1919
quiet: bool,
2020
}
2121

22+
fn toolchain_channel(repo: &Repository, tree: &Tree) -> Result<Option<String>> {
23+
// Try both filenames
24+
let entry = match tree
25+
.get_path(Path::new("rust-toolchain"))
26+
.or_else(|_| tree.get_path(Path::new("rust-toolchain.toml")))
27+
{
28+
Ok(entry) => entry,
29+
Err(_) => return Ok(None),
30+
};
31+
32+
// Load its blob
33+
let blob = repo
34+
.find_blob(entry.id())
35+
.context("failed to load rust-toolchain blob")?;
36+
37+
// Read as UTF-8 and trim
38+
let raw = std::str::from_utf8(blob.content())
39+
.context("rust-toolchain is not valid UTF-8")?
40+
.trim();
41+
42+
// If it's valid TOML, extract [toolchain].channel; otherwise use raw
43+
let channel = toml::from_str::<Value>(raw)
44+
.ok()
45+
.and_then(|doc| {
46+
doc.get("toolchain")
47+
.and_then(|t| t.get("channel"))
48+
.and_then(|c| c.as_str().map(ToString::to_string))
49+
})
50+
.unwrap_or_else(|| raw.to_string());
51+
52+
Ok(Some(channel))
53+
}
54+
2255
impl Revs {
2356
pub fn new(quiet: bool) -> Result<Self> {
2457
let start = Instant::now();
@@ -56,15 +89,9 @@ impl Revs {
5689
};
5790

5891
// Try to get channel from rust-toolchain
59-
let channel = if_chain! {
60-
if let Ok(toolchain_entry) = tree.get_path(Path::new("rust-toolchain"));
61-
if let Ok(blob) = self.repo.find_blob(toolchain_entry.id());
62-
if let Ok(content) = std::str::from_utf8(blob.content());
63-
then {
64-
content.trim().to_string()
65-
} else {
66-
return Ok(None);
67-
}
92+
let channel = match toolchain_channel(&self.repo, &tree)? {
93+
Some(chan) => chan,
94+
None => return Ok(None),
6895
};
6996

7097
Ok(Some(Rev {

0 commit comments

Comments
 (0)