Branchwell

Clearwake / app / crates / clearwake-core / src / lib.rs

1//! Clearwake's engine.
2//!
3//! Everything a Clearwake window needs that is not drawing: web addresses,
4//! the always-on ad and tracker filter, and a reader that turns a web page
5//! into clean blocks of text, links and pictures. No scripts ever run.
6//!
7//! The crate is `no_std` + `alloc` and uses integer math only, so the same
8//! code runs in the desktop, Android and web apps and inside the Glimmer
9//! kernel, which has no floating point unit.
10
11#![cfg_attr(not(any(feature = "std", test)), no_std)]
12
13extern crate alloc;
14
15pub mod bookmarks;
16pub mod filter;
17pub mod history;
18pub mod html;
19pub mod url;
20
21pub use filter::Filter;
22pub use html::{Block, Document, Inline};
23pub use url::Url;
24
25/// What the address bar should open for what the person typed.
26///
27/// A web address opens directly. Anything else becomes a search.
28pub fn resolve_input(input: &str) -> Option<Url> {
29 let s = input.trim();
30 if s.is_empty() {
31 return None;
32 }
33 if s == "home" || s == "about:home" {
34 return Url::parse("about:home");
35 }
36 if let Some(u) = Url::parse(s) {
37 if u.scheme == "http" || u.scheme == "https" || u.scheme == "about" {
38 return Some(u);
39 }
40 }
41 let looks_like_host = !s.contains(' ')
42 && (s.contains('.') || s.starts_with("localhost"))
43 && !s.starts_with('.')
44 && !s.ends_with('.');
45 if looks_like_host {
46 let mut full = alloc::string::String::from("https://");
47 full.push_str(s);
48 if let Some(u) = Url::parse(&full) {
49 return Some(u);
50 }
51 }
52 let mut q = alloc::string::String::from("https://html.duckduckgo.com/html/?q=");
53 q.push_str(&url::encode_component(s));
54 Url::parse(&q)
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn address_bar() {
63 assert_eq!(resolve_input("example.com").unwrap().to_string(), "https://example.com/");
64 assert_eq!(resolve_input("http://a.b/c?d").unwrap().to_string(), "http://a.b/c?d");
65 assert!(resolve_input("rust language").unwrap().to_string().starts_with("https://html.duckduckgo.com/html/?q=rust+language"));
66 assert_eq!(resolve_input("home").unwrap().to_string(), "about:home");
67 assert!(resolve_input(" ").is_none());
68 }
69}