Struct repo_icons::RepoIcon
source · pub struct RepoIcon {
pub url: Url,
pub headers: HashMap<String, String>,
pub kind: RepoIconKind,
pub info: IconInfo,
}
Fields§
§url: Url
§headers: HashMap<String, String>
§kind: RepoIconKind
§info: IconInfo
Implementations§
source§impl RepoIcon
impl RepoIcon
sourcepub fn new(url: Url, kind: RepoIconKind, info: IconInfo) -> Self
pub fn new(url: Url, kind: RepoIconKind, info: IconInfo) -> Self
Examples found in repository?
src/repo_icons.rs (lines 138-142)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
pub async fn load(
owner: &str,
repo: &str,
best_matches_only: bool,
) -> Result<Self, Box<dyn Error>> {
let mut repo_icons = Vec::new();
let readme = github_api::Readme::load(owner, repo).shared();
let mut futures: Vec<Pin<Box<dyn Future<Output = Result<LoadedKind, Box<dyn Error>>>>>> = vec![
async {
// Check if the repo contains the owner's username, and load the user's avatar
let docs = regex!("^(docs|documentation)$");
let icon = if repo.to_lowercase().contains(&owner_name_lowercase(owner))
|| docs.is_match(&repo.to_lowercase()).unwrap()
{
RepoIcon::load_user_avatar(owner).await
} else {
None
};
Ok(LoadedKind::UserAvatar(icon))
}
.boxed_local(),
// Try and find prefixed repos, and load icons for them on GitHub
async {
let repos = github_api::get_user_repos(owner).await?;
Ok(LoadedKind::PrefixedRepo(
join_all(
repos
.into_iter()
.filter(|possibly_prefixed_repo| {
possibly_prefixed_repo != &repo.to_lowercase()
&& repo.to_lowercase().contains(possibly_prefixed_repo)
})
.map(async move |repo| {
RepoIcons::load(owner, &repo, best_matches_only)
.await
.map(|icons| icons.0.into_vec())
.unwrap_or(Vec::new())
}),
)
.await
.into_iter()
.flatten()
.collect::<Vec<_>>()
.try_into()
.ok(),
))
}
.boxed_local(),
async {
let blob_icons = match github_api::get_blobs(owner, repo).await? {
Some((is_icon_field, blobs)) => Some(
try_join_all(
blobs
.into_iter()
.map(|blob| RepoIcon::load_blob(blob, is_icon_field)),
)
.await?
.try_into()
.unwrap(),
),
None => None,
};
Ok(LoadedKind::Blob(blob_icons))
}
.boxed_local(),
async {
let mut icons = Icons::new_with_blacklist(|url| is_blacklisted_homepage(url));
let homepage = readme.clone().await?.homepage;
if let Some(homepage) = &homepage {
warn_err!(
icons.load_website(homepage.clone()).await,
"failed to load website {}",
homepage
);
}
Ok(LoadedKind::Homepage(
icons
.entries()
.await
.into_iter()
.filter(|icon| !is_badge_url(&icon.url))
.map(|icon| {
RepoIcon::new(
icon.url,
(homepage.clone().unwrap(), icon.kind).into(),
icon.info,
)
})
.collect::<Vec<_>>()
.try_into()
.ok(),
))
}
.boxed_local(),
// Try and extract images from the readme website, or directly in it
async {
let image = readme
.clone()
.await?
.load_body()
.await?
.into_iter()
.find(|image| image.in_primary_heading);
let icon = match image {
Some(image) => Some(
RepoIcon::load_with_headers(image.src, image.headers, RepoIconKind::ReadmeImage)
.await?,
),
None => None,
};
Ok(LoadedKind::ReadmeImage(icon))
}
.boxed_local(),
];
let mut previous_loads = Vec::new();
let mut found_best_match = false;
let mut error = Ok(());
while !futures.is_empty() {
let (loaded, index, _) = select_all(&mut futures).await;
futures.remove(index);
let loaded = match loaded {
Err(err) => {
error = Err(err);
continue;
}
Ok(loaded) => loaded,
};
match &loaded {
LoadedKind::Blob(blob_icons) => {
if let Some(mut blob_icons) = blob_icons.clone() {
for blob_icon in &mut blob_icons {
blob_icon.set_repo_private(readme.clone().await?.private);
if matches!(blob_icon.kind, RepoIconKind::IconField(_)) {
found_best_match = true;
}
}
repo_icons.extend(blob_icons);
if previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Homepage(_)))
{
found_best_match = true;
}
}
}
LoadedKind::UserAvatar(user_avatar) => {
if let Some(blob_kinds) = previous_loads.iter().find_map(|loaded| {
if let LoadedKind::Blob(blob_icons) = loaded {
Some(blob_icons.as_ref().map(|blob_icons| {
blob_icons
.iter()
.map(|blob| blob.kind.clone())
.collect::<Vec<_>>()
}))
} else {
None
}
}) {
if let Some(blob_kinds) = blob_kinds {
for blob_kind in blob_kinds {
if matches!(blob_kind, RepoIconKind::RepoFile(_)) {
found_best_match = true;
}
}
} else {
found_best_match = true;
}
}
if let Some(user_avatar) = user_avatar {
repo_icons.push(user_avatar.clone());
}
}
LoadedKind::ReadmeImage(readme_image) => {
if let Some(readme_image) = readme_image {
if previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Blob(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Homepage(_)))
{
found_best_match = true;
}
repo_icons.push(readme_image.clone());
}
}
LoadedKind::PrefixedRepo(icons) => {
if let Some(icons) = icons {
repo_icons.extend(icons.clone());
}
}
LoadedKind::Homepage(site_icons) => {
if let Some(site_icons) = site_icons {
repo_icons.extend(site_icons.clone());
if site_icons.iter().any(|icon| {
matches!(
icon.kind,
RepoIconKind::AppIcon { .. } | RepoIconKind::SiteFavicon { .. }
)
}) && previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Blob(_)))
{
found_best_match = true;
}
}
}
}
previous_loads.push(loaded);
repo_icons.sort_by(|a, b| a.info.cmp(&b.info));
repo_icons.sort_by(|a, b| a.kind.cmp(&b.kind));
if best_matches_only && found_best_match {
break;
}
}
error?;
let repo_icons = repo_icons
.into_iter()
.unique_by(|icon| icon.url.clone())
.collect::<Vec<_>>();
let repo_icons: Vec1<RepoIcon> = repo_icons
.try_into()
.map_err(|_| "no icons found for repo")?;
Ok(RepoIcons(repo_icons))
}
sourcepub fn new_with_headers(
url: Url,
headers: HashMap<String, String>,
kind: RepoIconKind,
info: IconInfo
) -> Self
pub fn new_with_headers(
url: Url,
headers: HashMap<String, String>,
kind: RepoIconKind,
info: IconInfo
) -> Self
Examples found in repository?
src/repo_icon.rs (line 179)
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
pub fn new(url: Url, kind: RepoIconKind, info: IconInfo) -> Self {
Self::new_with_headers(url, HashMap::new(), kind, info)
}
pub fn new_with_headers(
url: Url,
headers: HashMap<String, String>,
kind: RepoIconKind,
info: IconInfo,
) -> Self {
Self {
url,
headers,
kind,
info,
#[cfg(feature = "image")]
image: RefCell::new(None),
}
}
pub async fn load(url: Url, kind: RepoIconKind) -> Result<Self, Box<dyn Error>> {
Self::load_with_headers(url, HashMap::new(), kind).await
}
pub async fn load_with_headers(
url: Url,
headers: HashMap<String, String>,
kind: RepoIconKind,
) -> Result<Self, Box<dyn Error>> {
let info = IconInfo::load(url.clone(), (&headers).try_into()?, None).await?;
Ok(Self::new_with_headers(url, headers, kind, info))
}
sourcepub async fn load_with_headers(
url: Url,
headers: HashMap<String, String>,
kind: RepoIconKind
) -> Result<Self, Box<dyn Error>>
pub async fn load_with_headers(
url: Url,
headers: HashMap<String, String>,
kind: RepoIconKind
) -> Result<Self, Box<dyn Error>>
Examples found in repository?
src/repo_icon.rs (line 199)
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
pub async fn load(url: Url, kind: RepoIconKind) -> Result<Self, Box<dyn Error>> {
Self::load_with_headers(url, HashMap::new(), kind).await
}
pub async fn load_with_headers(
url: Url,
headers: HashMap<String, String>,
kind: RepoIconKind,
) -> Result<Self, Box<dyn Error>> {
let info = IconInfo::load(url.clone(), (&headers).try_into()?, None).await?;
Ok(Self::new_with_headers(url, headers, kind, info))
}
pub async fn load_user_avatar(owner: &str) -> Option<Self> {
let user_avatar_url: Url = format!("https://github.com/{}.png", owner).parse().unwrap();
RepoIcon::load(user_avatar_url.clone(), RepoIconKind::UserAvatar)
.await
.ok()
}
pub async fn load_blob(blob: RepoBlob, is_icon_field: bool) -> Result<Self, Box<dyn Error>> {
let url = Url::parse(&format!(
"https://api.github.com/repos/{}/git/blobs/{}",
blob.slug, blob.sha
))
.unwrap();
let mut headers = hashmap! {
"Accept".to_string() => "application/vnd.github.raw".to_string(),
};
if let Some(token) = get_token() {
headers.insert("Authorization".to_string(), format!("Bearer {}", token));
}
RepoIcon::load_with_headers(
url,
headers,
if is_icon_field {
RepoIconKind::IconField(blob)
} else {
RepoIconKind::RepoFile(blob)
},
)
.await
}
More examples
src/repo_icons.rs (line 162)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
pub async fn load(
owner: &str,
repo: &str,
best_matches_only: bool,
) -> Result<Self, Box<dyn Error>> {
let mut repo_icons = Vec::new();
let readme = github_api::Readme::load(owner, repo).shared();
let mut futures: Vec<Pin<Box<dyn Future<Output = Result<LoadedKind, Box<dyn Error>>>>>> = vec![
async {
// Check if the repo contains the owner's username, and load the user's avatar
let docs = regex!("^(docs|documentation)$");
let icon = if repo.to_lowercase().contains(&owner_name_lowercase(owner))
|| docs.is_match(&repo.to_lowercase()).unwrap()
{
RepoIcon::load_user_avatar(owner).await
} else {
None
};
Ok(LoadedKind::UserAvatar(icon))
}
.boxed_local(),
// Try and find prefixed repos, and load icons for them on GitHub
async {
let repos = github_api::get_user_repos(owner).await?;
Ok(LoadedKind::PrefixedRepo(
join_all(
repos
.into_iter()
.filter(|possibly_prefixed_repo| {
possibly_prefixed_repo != &repo.to_lowercase()
&& repo.to_lowercase().contains(possibly_prefixed_repo)
})
.map(async move |repo| {
RepoIcons::load(owner, &repo, best_matches_only)
.await
.map(|icons| icons.0.into_vec())
.unwrap_or(Vec::new())
}),
)
.await
.into_iter()
.flatten()
.collect::<Vec<_>>()
.try_into()
.ok(),
))
}
.boxed_local(),
async {
let blob_icons = match github_api::get_blobs(owner, repo).await? {
Some((is_icon_field, blobs)) => Some(
try_join_all(
blobs
.into_iter()
.map(|blob| RepoIcon::load_blob(blob, is_icon_field)),
)
.await?
.try_into()
.unwrap(),
),
None => None,
};
Ok(LoadedKind::Blob(blob_icons))
}
.boxed_local(),
async {
let mut icons = Icons::new_with_blacklist(|url| is_blacklisted_homepage(url));
let homepage = readme.clone().await?.homepage;
if let Some(homepage) = &homepage {
warn_err!(
icons.load_website(homepage.clone()).await,
"failed to load website {}",
homepage
);
}
Ok(LoadedKind::Homepage(
icons
.entries()
.await
.into_iter()
.filter(|icon| !is_badge_url(&icon.url))
.map(|icon| {
RepoIcon::new(
icon.url,
(homepage.clone().unwrap(), icon.kind).into(),
icon.info,
)
})
.collect::<Vec<_>>()
.try_into()
.ok(),
))
}
.boxed_local(),
// Try and extract images from the readme website, or directly in it
async {
let image = readme
.clone()
.await?
.load_body()
.await?
.into_iter()
.find(|image| image.in_primary_heading);
let icon = match image {
Some(image) => Some(
RepoIcon::load_with_headers(image.src, image.headers, RepoIconKind::ReadmeImage)
.await?,
),
None => None,
};
Ok(LoadedKind::ReadmeImage(icon))
}
.boxed_local(),
];
let mut previous_loads = Vec::new();
let mut found_best_match = false;
let mut error = Ok(());
while !futures.is_empty() {
let (loaded, index, _) = select_all(&mut futures).await;
futures.remove(index);
let loaded = match loaded {
Err(err) => {
error = Err(err);
continue;
}
Ok(loaded) => loaded,
};
match &loaded {
LoadedKind::Blob(blob_icons) => {
if let Some(mut blob_icons) = blob_icons.clone() {
for blob_icon in &mut blob_icons {
blob_icon.set_repo_private(readme.clone().await?.private);
if matches!(blob_icon.kind, RepoIconKind::IconField(_)) {
found_best_match = true;
}
}
repo_icons.extend(blob_icons);
if previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Homepage(_)))
{
found_best_match = true;
}
}
}
LoadedKind::UserAvatar(user_avatar) => {
if let Some(blob_kinds) = previous_loads.iter().find_map(|loaded| {
if let LoadedKind::Blob(blob_icons) = loaded {
Some(blob_icons.as_ref().map(|blob_icons| {
blob_icons
.iter()
.map(|blob| blob.kind.clone())
.collect::<Vec<_>>()
}))
} else {
None
}
}) {
if let Some(blob_kinds) = blob_kinds {
for blob_kind in blob_kinds {
if matches!(blob_kind, RepoIconKind::RepoFile(_)) {
found_best_match = true;
}
}
} else {
found_best_match = true;
}
}
if let Some(user_avatar) = user_avatar {
repo_icons.push(user_avatar.clone());
}
}
LoadedKind::ReadmeImage(readme_image) => {
if let Some(readme_image) = readme_image {
if previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Blob(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Homepage(_)))
{
found_best_match = true;
}
repo_icons.push(readme_image.clone());
}
}
LoadedKind::PrefixedRepo(icons) => {
if let Some(icons) = icons {
repo_icons.extend(icons.clone());
}
}
LoadedKind::Homepage(site_icons) => {
if let Some(site_icons) = site_icons {
repo_icons.extend(site_icons.clone());
if site_icons.iter().any(|icon| {
matches!(
icon.kind,
RepoIconKind::AppIcon { .. } | RepoIconKind::SiteFavicon { .. }
)
}) && previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Blob(_)))
{
found_best_match = true;
}
}
}
}
previous_loads.push(loaded);
repo_icons.sort_by(|a, b| a.info.cmp(&b.info));
repo_icons.sort_by(|a, b| a.kind.cmp(&b.kind));
if best_matches_only && found_best_match {
break;
}
}
error?;
let repo_icons = repo_icons
.into_iter()
.unique_by(|icon| icon.url.clone())
.collect::<Vec<_>>();
let repo_icons: Vec1<RepoIcon> = repo_icons
.try_into()
.map_err(|_| "no icons found for repo")?;
Ok(RepoIcons(repo_icons))
}
sourcepub async fn load_user_avatar(owner: &str) -> Option<Self>
pub async fn load_user_avatar(owner: &str) -> Option<Self>
Examples found in repository?
src/repo_icons.rs (line 64)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
pub async fn load(
owner: &str,
repo: &str,
best_matches_only: bool,
) -> Result<Self, Box<dyn Error>> {
let mut repo_icons = Vec::new();
let readme = github_api::Readme::load(owner, repo).shared();
let mut futures: Vec<Pin<Box<dyn Future<Output = Result<LoadedKind, Box<dyn Error>>>>>> = vec![
async {
// Check if the repo contains the owner's username, and load the user's avatar
let docs = regex!("^(docs|documentation)$");
let icon = if repo.to_lowercase().contains(&owner_name_lowercase(owner))
|| docs.is_match(&repo.to_lowercase()).unwrap()
{
RepoIcon::load_user_avatar(owner).await
} else {
None
};
Ok(LoadedKind::UserAvatar(icon))
}
.boxed_local(),
// Try and find prefixed repos, and load icons for them on GitHub
async {
let repos = github_api::get_user_repos(owner).await?;
Ok(LoadedKind::PrefixedRepo(
join_all(
repos
.into_iter()
.filter(|possibly_prefixed_repo| {
possibly_prefixed_repo != &repo.to_lowercase()
&& repo.to_lowercase().contains(possibly_prefixed_repo)
})
.map(async move |repo| {
RepoIcons::load(owner, &repo, best_matches_only)
.await
.map(|icons| icons.0.into_vec())
.unwrap_or(Vec::new())
}),
)
.await
.into_iter()
.flatten()
.collect::<Vec<_>>()
.try_into()
.ok(),
))
}
.boxed_local(),
async {
let blob_icons = match github_api::get_blobs(owner, repo).await? {
Some((is_icon_field, blobs)) => Some(
try_join_all(
blobs
.into_iter()
.map(|blob| RepoIcon::load_blob(blob, is_icon_field)),
)
.await?
.try_into()
.unwrap(),
),
None => None,
};
Ok(LoadedKind::Blob(blob_icons))
}
.boxed_local(),
async {
let mut icons = Icons::new_with_blacklist(|url| is_blacklisted_homepage(url));
let homepage = readme.clone().await?.homepage;
if let Some(homepage) = &homepage {
warn_err!(
icons.load_website(homepage.clone()).await,
"failed to load website {}",
homepage
);
}
Ok(LoadedKind::Homepage(
icons
.entries()
.await
.into_iter()
.filter(|icon| !is_badge_url(&icon.url))
.map(|icon| {
RepoIcon::new(
icon.url,
(homepage.clone().unwrap(), icon.kind).into(),
icon.info,
)
})
.collect::<Vec<_>>()
.try_into()
.ok(),
))
}
.boxed_local(),
// Try and extract images from the readme website, or directly in it
async {
let image = readme
.clone()
.await?
.load_body()
.await?
.into_iter()
.find(|image| image.in_primary_heading);
let icon = match image {
Some(image) => Some(
RepoIcon::load_with_headers(image.src, image.headers, RepoIconKind::ReadmeImage)
.await?,
),
None => None,
};
Ok(LoadedKind::ReadmeImage(icon))
}
.boxed_local(),
];
let mut previous_loads = Vec::new();
let mut found_best_match = false;
let mut error = Ok(());
while !futures.is_empty() {
let (loaded, index, _) = select_all(&mut futures).await;
futures.remove(index);
let loaded = match loaded {
Err(err) => {
error = Err(err);
continue;
}
Ok(loaded) => loaded,
};
match &loaded {
LoadedKind::Blob(blob_icons) => {
if let Some(mut blob_icons) = blob_icons.clone() {
for blob_icon in &mut blob_icons {
blob_icon.set_repo_private(readme.clone().await?.private);
if matches!(blob_icon.kind, RepoIconKind::IconField(_)) {
found_best_match = true;
}
}
repo_icons.extend(blob_icons);
if previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Homepage(_)))
{
found_best_match = true;
}
}
}
LoadedKind::UserAvatar(user_avatar) => {
if let Some(blob_kinds) = previous_loads.iter().find_map(|loaded| {
if let LoadedKind::Blob(blob_icons) = loaded {
Some(blob_icons.as_ref().map(|blob_icons| {
blob_icons
.iter()
.map(|blob| blob.kind.clone())
.collect::<Vec<_>>()
}))
} else {
None
}
}) {
if let Some(blob_kinds) = blob_kinds {
for blob_kind in blob_kinds {
if matches!(blob_kind, RepoIconKind::RepoFile(_)) {
found_best_match = true;
}
}
} else {
found_best_match = true;
}
}
if let Some(user_avatar) = user_avatar {
repo_icons.push(user_avatar.clone());
}
}
LoadedKind::ReadmeImage(readme_image) => {
if let Some(readme_image) = readme_image {
if previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Blob(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Homepage(_)))
{
found_best_match = true;
}
repo_icons.push(readme_image.clone());
}
}
LoadedKind::PrefixedRepo(icons) => {
if let Some(icons) = icons {
repo_icons.extend(icons.clone());
}
}
LoadedKind::Homepage(site_icons) => {
if let Some(site_icons) = site_icons {
repo_icons.extend(site_icons.clone());
if site_icons.iter().any(|icon| {
matches!(
icon.kind,
RepoIconKind::AppIcon { .. } | RepoIconKind::SiteFavicon { .. }
)
}) && previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Blob(_)))
{
found_best_match = true;
}
}
}
}
previous_loads.push(loaded);
repo_icons.sort_by(|a, b| a.info.cmp(&b.info));
repo_icons.sort_by(|a, b| a.kind.cmp(&b.kind));
if best_matches_only && found_best_match {
break;
}
}
error?;
let repo_icons = repo_icons
.into_iter()
.unique_by(|icon| icon.url.clone())
.collect::<Vec<_>>();
let repo_icons: Vec1<RepoIcon> = repo_icons
.try_into()
.map_err(|_| "no icons found for repo")?;
Ok(RepoIcons(repo_icons))
}
sourcepub async fn load_blob(
blob: RepoBlob,
is_icon_field: bool
) -> Result<Self, Box<dyn Error>>
pub async fn load_blob(
blob: RepoBlob,
is_icon_field: bool
) -> Result<Self, Box<dyn Error>>
Examples found in repository?
src/repo_icons.rs (line 106)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
pub async fn load(
owner: &str,
repo: &str,
best_matches_only: bool,
) -> Result<Self, Box<dyn Error>> {
let mut repo_icons = Vec::new();
let readme = github_api::Readme::load(owner, repo).shared();
let mut futures: Vec<Pin<Box<dyn Future<Output = Result<LoadedKind, Box<dyn Error>>>>>> = vec![
async {
// Check if the repo contains the owner's username, and load the user's avatar
let docs = regex!("^(docs|documentation)$");
let icon = if repo.to_lowercase().contains(&owner_name_lowercase(owner))
|| docs.is_match(&repo.to_lowercase()).unwrap()
{
RepoIcon::load_user_avatar(owner).await
} else {
None
};
Ok(LoadedKind::UserAvatar(icon))
}
.boxed_local(),
// Try and find prefixed repos, and load icons for them on GitHub
async {
let repos = github_api::get_user_repos(owner).await?;
Ok(LoadedKind::PrefixedRepo(
join_all(
repos
.into_iter()
.filter(|possibly_prefixed_repo| {
possibly_prefixed_repo != &repo.to_lowercase()
&& repo.to_lowercase().contains(possibly_prefixed_repo)
})
.map(async move |repo| {
RepoIcons::load(owner, &repo, best_matches_only)
.await
.map(|icons| icons.0.into_vec())
.unwrap_or(Vec::new())
}),
)
.await
.into_iter()
.flatten()
.collect::<Vec<_>>()
.try_into()
.ok(),
))
}
.boxed_local(),
async {
let blob_icons = match github_api::get_blobs(owner, repo).await? {
Some((is_icon_field, blobs)) => Some(
try_join_all(
blobs
.into_iter()
.map(|blob| RepoIcon::load_blob(blob, is_icon_field)),
)
.await?
.try_into()
.unwrap(),
),
None => None,
};
Ok(LoadedKind::Blob(blob_icons))
}
.boxed_local(),
async {
let mut icons = Icons::new_with_blacklist(|url| is_blacklisted_homepage(url));
let homepage = readme.clone().await?.homepage;
if let Some(homepage) = &homepage {
warn_err!(
icons.load_website(homepage.clone()).await,
"failed to load website {}",
homepage
);
}
Ok(LoadedKind::Homepage(
icons
.entries()
.await
.into_iter()
.filter(|icon| !is_badge_url(&icon.url))
.map(|icon| {
RepoIcon::new(
icon.url,
(homepage.clone().unwrap(), icon.kind).into(),
icon.info,
)
})
.collect::<Vec<_>>()
.try_into()
.ok(),
))
}
.boxed_local(),
// Try and extract images from the readme website, or directly in it
async {
let image = readme
.clone()
.await?
.load_body()
.await?
.into_iter()
.find(|image| image.in_primary_heading);
let icon = match image {
Some(image) => Some(
RepoIcon::load_with_headers(image.src, image.headers, RepoIconKind::ReadmeImage)
.await?,
),
None => None,
};
Ok(LoadedKind::ReadmeImage(icon))
}
.boxed_local(),
];
let mut previous_loads = Vec::new();
let mut found_best_match = false;
let mut error = Ok(());
while !futures.is_empty() {
let (loaded, index, _) = select_all(&mut futures).await;
futures.remove(index);
let loaded = match loaded {
Err(err) => {
error = Err(err);
continue;
}
Ok(loaded) => loaded,
};
match &loaded {
LoadedKind::Blob(blob_icons) => {
if let Some(mut blob_icons) = blob_icons.clone() {
for blob_icon in &mut blob_icons {
blob_icon.set_repo_private(readme.clone().await?.private);
if matches!(blob_icon.kind, RepoIconKind::IconField(_)) {
found_best_match = true;
}
}
repo_icons.extend(blob_icons);
if previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Homepage(_)))
{
found_best_match = true;
}
}
}
LoadedKind::UserAvatar(user_avatar) => {
if let Some(blob_kinds) = previous_loads.iter().find_map(|loaded| {
if let LoadedKind::Blob(blob_icons) = loaded {
Some(blob_icons.as_ref().map(|blob_icons| {
blob_icons
.iter()
.map(|blob| blob.kind.clone())
.collect::<Vec<_>>()
}))
} else {
None
}
}) {
if let Some(blob_kinds) = blob_kinds {
for blob_kind in blob_kinds {
if matches!(blob_kind, RepoIconKind::RepoFile(_)) {
found_best_match = true;
}
}
} else {
found_best_match = true;
}
}
if let Some(user_avatar) = user_avatar {
repo_icons.push(user_avatar.clone());
}
}
LoadedKind::ReadmeImage(readme_image) => {
if let Some(readme_image) = readme_image {
if previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Blob(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Homepage(_)))
{
found_best_match = true;
}
repo_icons.push(readme_image.clone());
}
}
LoadedKind::PrefixedRepo(icons) => {
if let Some(icons) = icons {
repo_icons.extend(icons.clone());
}
}
LoadedKind::Homepage(site_icons) => {
if let Some(site_icons) = site_icons {
repo_icons.extend(site_icons.clone());
if site_icons.iter().any(|icon| {
matches!(
icon.kind,
RepoIconKind::AppIcon { .. } | RepoIconKind::SiteFavicon { .. }
)
}) && previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Blob(_)))
{
found_best_match = true;
}
}
}
}
previous_loads.push(loaded);
repo_icons.sort_by(|a, b| a.info.cmp(&b.info));
repo_icons.sort_by(|a, b| a.kind.cmp(&b.kind));
if best_matches_only && found_best_match {
break;
}
}
error?;
let repo_icons = repo_icons
.into_iter()
.unique_by(|icon| icon.url.clone())
.collect::<Vec<_>>();
let repo_icons: Vec1<RepoIcon> = repo_icons
.try_into()
.map_err(|_| "no icons found for repo")?;
Ok(RepoIcons(repo_icons))
}
sourcepub fn set_repo_private(&mut self, is_private: bool)
pub fn set_repo_private(&mut self, is_private: bool)
Examples found in repository?
src/repo_icons.rs (line 194)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
pub async fn load(
owner: &str,
repo: &str,
best_matches_only: bool,
) -> Result<Self, Box<dyn Error>> {
let mut repo_icons = Vec::new();
let readme = github_api::Readme::load(owner, repo).shared();
let mut futures: Vec<Pin<Box<dyn Future<Output = Result<LoadedKind, Box<dyn Error>>>>>> = vec![
async {
// Check if the repo contains the owner's username, and load the user's avatar
let docs = regex!("^(docs|documentation)$");
let icon = if repo.to_lowercase().contains(&owner_name_lowercase(owner))
|| docs.is_match(&repo.to_lowercase()).unwrap()
{
RepoIcon::load_user_avatar(owner).await
} else {
None
};
Ok(LoadedKind::UserAvatar(icon))
}
.boxed_local(),
// Try and find prefixed repos, and load icons for them on GitHub
async {
let repos = github_api::get_user_repos(owner).await?;
Ok(LoadedKind::PrefixedRepo(
join_all(
repos
.into_iter()
.filter(|possibly_prefixed_repo| {
possibly_prefixed_repo != &repo.to_lowercase()
&& repo.to_lowercase().contains(possibly_prefixed_repo)
})
.map(async move |repo| {
RepoIcons::load(owner, &repo, best_matches_only)
.await
.map(|icons| icons.0.into_vec())
.unwrap_or(Vec::new())
}),
)
.await
.into_iter()
.flatten()
.collect::<Vec<_>>()
.try_into()
.ok(),
))
}
.boxed_local(),
async {
let blob_icons = match github_api::get_blobs(owner, repo).await? {
Some((is_icon_field, blobs)) => Some(
try_join_all(
blobs
.into_iter()
.map(|blob| RepoIcon::load_blob(blob, is_icon_field)),
)
.await?
.try_into()
.unwrap(),
),
None => None,
};
Ok(LoadedKind::Blob(blob_icons))
}
.boxed_local(),
async {
let mut icons = Icons::new_with_blacklist(|url| is_blacklisted_homepage(url));
let homepage = readme.clone().await?.homepage;
if let Some(homepage) = &homepage {
warn_err!(
icons.load_website(homepage.clone()).await,
"failed to load website {}",
homepage
);
}
Ok(LoadedKind::Homepage(
icons
.entries()
.await
.into_iter()
.filter(|icon| !is_badge_url(&icon.url))
.map(|icon| {
RepoIcon::new(
icon.url,
(homepage.clone().unwrap(), icon.kind).into(),
icon.info,
)
})
.collect::<Vec<_>>()
.try_into()
.ok(),
))
}
.boxed_local(),
// Try and extract images from the readme website, or directly in it
async {
let image = readme
.clone()
.await?
.load_body()
.await?
.into_iter()
.find(|image| image.in_primary_heading);
let icon = match image {
Some(image) => Some(
RepoIcon::load_with_headers(image.src, image.headers, RepoIconKind::ReadmeImage)
.await?,
),
None => None,
};
Ok(LoadedKind::ReadmeImage(icon))
}
.boxed_local(),
];
let mut previous_loads = Vec::new();
let mut found_best_match = false;
let mut error = Ok(());
while !futures.is_empty() {
let (loaded, index, _) = select_all(&mut futures).await;
futures.remove(index);
let loaded = match loaded {
Err(err) => {
error = Err(err);
continue;
}
Ok(loaded) => loaded,
};
match &loaded {
LoadedKind::Blob(blob_icons) => {
if let Some(mut blob_icons) = blob_icons.clone() {
for blob_icon in &mut blob_icons {
blob_icon.set_repo_private(readme.clone().await?.private);
if matches!(blob_icon.kind, RepoIconKind::IconField(_)) {
found_best_match = true;
}
}
repo_icons.extend(blob_icons);
if previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Homepage(_)))
{
found_best_match = true;
}
}
}
LoadedKind::UserAvatar(user_avatar) => {
if let Some(blob_kinds) = previous_loads.iter().find_map(|loaded| {
if let LoadedKind::Blob(blob_icons) = loaded {
Some(blob_icons.as_ref().map(|blob_icons| {
blob_icons
.iter()
.map(|blob| blob.kind.clone())
.collect::<Vec<_>>()
}))
} else {
None
}
}) {
if let Some(blob_kinds) = blob_kinds {
for blob_kind in blob_kinds {
if matches!(blob_kind, RepoIconKind::RepoFile(_)) {
found_best_match = true;
}
}
} else {
found_best_match = true;
}
}
if let Some(user_avatar) = user_avatar {
repo_icons.push(user_avatar.clone());
}
}
LoadedKind::ReadmeImage(readme_image) => {
if let Some(readme_image) = readme_image {
if previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Blob(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Homepage(_)))
{
found_best_match = true;
}
repo_icons.push(readme_image.clone());
}
}
LoadedKind::PrefixedRepo(icons) => {
if let Some(icons) = icons {
repo_icons.extend(icons.clone());
}
}
LoadedKind::Homepage(site_icons) => {
if let Some(site_icons) = site_icons {
repo_icons.extend(site_icons.clone());
if site_icons.iter().any(|icon| {
matches!(
icon.kind,
RepoIconKind::AppIcon { .. } | RepoIconKind::SiteFavicon { .. }
)
}) && previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::UserAvatar(_)))
&& previous_loads
.iter()
.any(|loaded| matches!(loaded, LoadedKind::Blob(_)))
{
found_best_match = true;
}
}
}
}
previous_loads.push(loaded);
repo_icons.sort_by(|a, b| a.info.cmp(&b.info));
repo_icons.sort_by(|a, b| a.kind.cmp(&b.kind));
if best_matches_only && found_best_match {
break;
}
}
error?;
let repo_icons = repo_icons
.into_iter()
.unique_by(|icon| icon.url.clone())
.collect::<Vec<_>>();
let repo_icons: Vec1<RepoIcon> = repo_icons
.try_into()
.map_err(|_| "no icons found for repo")?;
Ok(RepoIcons(repo_icons))
}
pub async fn data(&self) -> Result<Vec<u8>, Box<dyn Error>>
Trait Implementations§
source§impl<'de> Deserialize<'de> for RepoIcon
impl<'de> Deserialize<'de> for RepoIcon
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl PartialEq<RepoIcon> for RepoIcon
impl PartialEq<RepoIcon> for RepoIcon
impl Eq for RepoIcon
Auto Trait Implementations§
impl RefUnwindSafe for RepoIcon
impl Send for RepoIcon
impl Sync for RepoIcon
impl Unpin for RepoIcon
impl UnwindSafe for RepoIcon
Blanket Implementations§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.