Rust lightningcss minify - rust

Trying to figure out how to use minify from lightningcss. Here's my code
let fs = FileProvider::new();
let mut bundler = Bundler::new(&fs, None, ParserOptions::default());
let stylesheet = bundler.bundle(Path::new("./assets/css/global.css")).unwrap();
stylesheet.minify(MinifyOptions::default());
println!("{}", stylesheet.to_css(PrinterOptions::default()).unwrap().code);
If I try to print the unwrapped version of the minified output that is empty.
taken more from here https://docs.rs/lightningcss/1.0.0-alpha.34/lightningcss/stylesheet/struct.StyleSheet.html
And the question: how do I use the minify output?

Related

Hitting upload limit on crates.io on a sys crate

I'm trying to publish a sys crate for libvmaf. Unfortunately I can not simply dynamically link to libvmaf because it's not distributed anywhere and I need to build it from source and include it in my library. Unfortunately libvmaf is absolutely huge and my .rlib file is ending up at 1.4 megabytes which is over the upload limit for crates.io. Am I boned here?
Here's my build.rs file
use meson_next;
use std::env;
use std::fs::canonicalize;
use std::path::PathBuf;
fn main() {
//env::set_var("RUST_BACKTRACE", "1");
let build_dir = PathBuf::from(env::var("OUT_DIR").unwrap()).join("build");
let lib_dir = build_dir.join("src");
let build_dir_str = build_dir.to_str().unwrap();
let lib_dir_str = lib_dir.to_str().unwrap();
meson_next::build("vmaf/libvmaf", build_dir_str);
println!("cargo:rustc-link-lib=static=vmaf");
println!("cargo:rustc-link-search=native={lib_dir_str}");
// Path to vendor header files
let headers_dir = PathBuf::from("vmaf/libvmaf/include");
let headers_dir_canonical = canonicalize(headers_dir).unwrap();
let include_path = headers_dir_canonical.to_str().unwrap();
// Generate bindings to libvmaf using rust-bindgen
let bindings = bindgen::Builder::default()
.header("vmaf/libvmaf/include/libvmaf/libvmaf.h")
.clang_arg(format!("-I{include_path}"))
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings");
// Write bindings to build directory
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
In general you should not be including compiled libraries in your package. Include the source code, and have your build script perform the build.
This will usually result in a smaller package, and also means that your package works on any target architecture (that is supported by the library).

Edit local yaml file in rust

I'm trying to edit yaml file using serde_yaml but using this i'm only able to edit in stdout but cannot write back to local file(/home/home/.kube/config)
let kubeconfig = "/home/home/.kube/config"
let contents = fs::read_to_string(kubeconfig)
.expect("Something went wrong reading the file");
let mut value: serde_yaml::Value = serde_yaml::from_str(&contents).unwrap();
*value.get_mut("current-context").unwrap() = "new_user".into();
// Below lines shows the edited file in stdout
serde_yaml::to_writer(std::io::stdout(), &value).unwrap();
I did tired as below an other method but had no luck.
let writer = serde_yaml::to_writer(std::io::stdout(), &value).unwrap();
println!("{:?}",writer); // shows ()
serde_yaml::to_writer(writer);
How do i write this edit back to /home/home/.kube/config ?
It seems that you are trying to use to_writer as to_string:
let writer = serde_yaml::to_string(&value).unwrap();
println!("{:?}", writer);
Then you can save the string how you usually would.
Or, alternatively, you could write directly to a file:
let mut file = File::create("/home/home/.kube/config").unwrap();
serde_yaml.to_writer(&mut file, &value);

Create web_sys::RtcPeerConnection with customized configuration

I am trying to figure how to create a RtcPeerConnection with the web-sys crate. In JavaScript I can write the following:
const pc = new RTCPeerConnection({'iceServers': [{'urls': ['stun:stun.l.google.com:19302']}]});
How does this translate to rust? I have tried the following:
let mut rtc = RtcConfiguration::new();
let config = RtcConfiguration::ice_servers(&mut rtc, &JsValue::from_serde(&json!({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})).unwrap());
let pc = RtcPeerConnection::new_with_configuration(&config).unwrap();
But it fails when creating pc. The json! macro is from serde_json::json.
For anyone with the same issue, the following works:
let mut rtc = RtcConfiguration::new();
let config = rtc.ice_servers(&JsValue::from_serde(&json!([{"urls": "stun:stun.l.google.com:19302"}])).unwrap());
let pc = RtcPeerConnection::new_with_configuration(&config).unwrap();

fltk: failed to resolve GlutWindow

I'm trying to follow example of fltk application which uses openGl, but the build is not functioning:
let app = app::App::default();
let mut win = window::GlutWindow::default().with_size(800, 600);
win.set_mode(enums::Mode::Opengl3);
win.end();
win.show();
unsafe {
let gl = glow::Context::from_loader_function(|s| {
win.get_proc_address(s) as *const _
});
I get: failed to resolve: could not find GlutWindow in window.
I'm using fltk version 1
Thank you
p.s. I'm using Rust
Check your imports which are missing from that snippet.
Also if you’re not enabling the enable-glwindow feature, you should, try changing your Cargo.toml to include the missing feature:
[dependencies]
fltk = { version = "1", features = ["enable-glwindow"] }

How to get path.join in Rust?

How do I write the path.join in Rust. I tried multiple examples but couldn't get it.
const exeDirectory = path.join(__dirname, '..', 'bin', 'openvpn.exe');
const processFile = path.join(__dirname, '..', '1');
I want to convert these lines of JS into Rust.
Use Path which has the .join method
Path::new("..").join("bin").join("openvpn.exe");
I may be missing something but have you looked at Path::join, and PathBuf::push linked from it?
let exe_directory = Path::new(dirname).join("..").join("bin").join("openvpn.exe");
println!("{:?}", exe_directory);
let mut exe_directory = PathBuf::new();
exe_directory.push(dirname);
exe_directory.push("..");
exe_directory.push("bin");
exe_directory.push("openvpn.exe");
println!("{:?}", exe_directory);
Playground link
Another option is collecting an iterator of string into a PathBuf:
let path: PathBuf = ["..", "bin", "openvpn.exe"].iter().collect();
This is equivalent to creating a new PathBuf and calling .push() for each string in the iterator. To add multiple new components to an existing PathBuf, you can use the extend() method:
let mut path = PathBuf::from(dir_name);
path.extend(&["..", "bin", "openvpn.exe"]);

Resources